forked from: flash on 2010-3-9
forked from flash on 2010-3-9 (diff: 14)
跳ね返るたびにボールの色を変えたかったんですが...。これじゃだめなんですね。
ActionScript3 source code
/**
* Copyright meat18 ( http://wonderfl.net/user/meat18 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/x8AO
*/
// forked from 084's flash on 2010-3-9
//跳ね返るたびにボールの色を変えたかったんですが...。これじゃだめなんですね。
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.media.Video;
public class Bouncing extends Sprite {
private var ball:Ball;
private var vx:Number;
private var vy:Number;
public function Bouncing() {
init();
}
/*
オチから先に言うと、ボールを新しい色で塗りなおしてないだけです。
ball.init()しなおすだけでいいです。
*/
private function init():void {
//stage.scaleMode = StageScaleMode.NO_SCALE;
//stage.align = StageAlign.TOP_LEFT;
ball = new Ball();
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
vx = Math.random() * 10 - 5;
vy = Math.random() * 10 - 5;
addChild(ball);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
ball.x += vx;
ball.y += vy;
var left:Number = 0;
var right:Number = stage.stageWidth;
var top:Number = 0;
var bottom:Number = stage.stageHeight;
if (ball.x + ball.radius > right) {
ball.x = right - ball.radius;
ball.color = Math.random() * 0xffffff;
ball.init();//普通に描画しなおせばおk
vx *= -1;
}else if (ball.x - ball.radius < left) {
ball.x = left + ball.radius;
ball.color = Math.random() * 0xffffff;
ball.init();//普通に描画しなおせばおk
vx *= -1;
}
if (ball.y + ball.radius > bottom) {
ball.y = bottom - ball.radius;
ball.color = Math.random() * 0xffffff;
ball.init();//普通に描画しなおせばおk
vy *= -1;
}else if (ball.y - ball.radius < top) {
ball.y = top + ball.radius;
ball.color = Math.random() * 0xffffff;
ball.init();//普通に描画しなおせばおk
vy *= -1;
}
}
}
}
import flash.display.Sprite;
class Ball extends Sprite {
public var radius:Number;
public var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public function Ball(radius:Number = 40,color:uint=0xff0000 ):void {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
this.graphics.clear();//念のため消してから描く
this.graphics.beginFill(color);
this.graphics.drawCircle(0, 0, radius);
//graphics.endFill();
}
}
