えんちゃんと
夜中に現実逃避でenchant.jsで遊んでた.
enchant(); var game; var arr = []; window.onload = function() { game = new Game(320, 320); game.fps = 24; game.onload = function() { for (var i = 0; i < 50; i++) { var ball = new Ball(160, 160, 2.5 - Math.random() * 5 , 2.5 - Math.random() * 5, Math.random() * 1.5); arr[i] = ball; } this.addEventListener(Event.ENTER_FRAME,onLoop); game.rootScene.backgroundColor = '#ffffff'; }; game.start(); } function Ball(x2,y2,vx,vy,ay) { //Ballクラス Ball.prototype = new Sprite(); Sprite.call console.log(this); this.xx = x2; this.yy = y2; this.vx = vx; this.vy = vy; this.b_vy = vy; this.ay = ay; this.color_red = Math.floor(Math.random() * 255); this.color_blue = Math.floor(Math.random() * 255); this.color_green = Math.floor(Math.random() * 255); this.color_alpha = Math.random()*1; this.color = "rgba(" + this.color_red + "," + this.color_blue + "," + this.color_green + "," + this.color_alpha + ")"; ///console.log(this.color); this.sp = new Sprite(20,20); this.surf = new Surface(20,20); this.surf.context.beginPath(); this.surf.context.fillStyle = this.color; this.surf.context.arc(10,10,10,0,Math.PI*2, false); this.surf.context.fill(); this.sp.image = this.surf; this.sp.x = this.xx; this.sp.y = this.yy; game.rootScene.addChild(this.sp); this.update = function() { //Ball.prototype.update = function() { this.sp.x += this.vx; this.sp.y += this.vy; this.vy += this.ay; console.log("hoge-" + this.sp.x); if ( (this.sp.x) > 320 || (this.sp.x) < 0 || (this.sp.y) > 320 || (this.sp.y) < 0) { this.sp.x = this.xx; this.sp.y = this.yy; this.vy = this.b_vy; } }; }; var xx = 0; var yy = 0; function onLoop (e) { for (var i = 0; i < 50; i++) { arr[i].update(); } }
感想
jsよくわからん
理想はBallクラスの中で,
this.sp.addEventListener(Event.ENTER_FRAME,onAnimation);
function onAnimation() {
this.sp.x += this.vx;
this.sp.y += this.vy;
}
みたいなことしたいけど,イベントリスナ内から自クラスのプロパティを参照するのがよーわからん.
あと,Ball.prototype = new Sprite(); でSprite継承してるけど,
Ball.addEventListener()って出来ないのがよくわからん.
Ball.prototype = new EventTraget(); //addEventListenerメソッドの定義元
でもうまくいかん感じ.
プロトタイプベースオブジェクト指向もっと勉強せんとあかんね
うん
こういうの作るんだったら,easeljsの方が楽やね
つか
なんでもjsで作れば正義みたいな風潮どうにかなんらんのか