ボールのバウンドアニメーション


ステージ上をマウスを押している間ボールが配置されバウンドします。


var loader = new smart.Loader();
loader.load({
	red   :"img/ballRed.png",
	blue  :"img/ballBlue.png",
	green :"img/ballGreen.png",
	yellow:"img/ballYellow.png"
},function(imgs)
{
	var stage = new smart.Stage("demo2");
	var colorCount = 0;
	var pressed = 0;
	
	// smart.Canvas を継承してバウンドするボールを定義
	var Ball = S.extend(smart.Canvas,{
		init:function(img,fn)
		{
			smart.Canvas.apply(this,[40, 40 ,img]);
			this.times = 300;
			this.complete = fn;
			var rad = Math.random()*Math.PI*2;
			this.speedX = Math.cos(rad) * 16;
			this.speedY = Math.sin(rad) * 16;
		},
		update:function()
		{
			if(--this.times)
			{
				this.x += this.speedX;
				this.y += this.speedY;
				if(this.y > stage.height - this.height)
				{
					this.y = stage.height - this.height;
					this.speedY *= -0.8;
				}
				this.speedX *= 0.98;
				this.speedY += 1;
			}
			else
			{
				this.complete();
			}
		}
	});
	var cX = 0;
	var cY = 0;
	stage.on("mousemove touchmove",function(e)
	{
		cX = e.layerX;
		cY = e.layerY;
	});
	stage.on("touchstart mousedown",function(e)
	{
		pressed = 1;
		e.preventDefault();//長押し時のメニューを出さないように
	});
	stage.on("touchend mouseup",function(e)
	{
		pressed = 0;
	});
	smart.ticker.on("tick",function(e)
	{
		if(pressed)
		{
			var color = ["red","blue","green","yellow"][colorCount++ % 4];
			var ball = stage.addChild(new Ball(imgs[color],function()
			{
				stage.removeChild(ball);
			}));
			ball.x = cX - ball.width/2;
			ball.y = cY - ball.height/2;
		}
		for(var i=0,len = stage.children.length;i<len;i++)
		{
			stage.children[i] && stage.children[i].update();
		}
	});
});

TOP