複数のボールを境界で跳ね返す
♥0 |
Line 72 |
Modified 2011-04-05 01:33:27 |
MIT License
archived:2017-03-20 13:50:14
ActionScript3 source code
/**
* Copyright Nowloading_ ( http://wonderfl.net/user/Nowloading_ )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ghiC
*/
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
public class Bouncing2 extends Sprite{
private var count:int=10;
private var balls:Array;
public function Bouncing2(){
init();
}
private function init():void{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
balls = new Array();
for(var i:int=0;i<count;i++){
var color:uint = Math.random()*0xffffff;
var ball:Ball = new Ball(10,color);
ball.x = Math.random()*stage.stageWidth;
ball.y = Math.random()*stage.stageHeight;
ball.vx = Math.random()*10-1;
ball.vy = Math.random()*10-1;
addChild(ball);
balls.push(ball);
}
addEventListener(Event.ENTER_FRAME,onEnterFlame);
}
private function onEnterFlame(e:Event):void{
for(var i:Number=0;i<balls.length;i++){
var ball:Ball = Ball(balls[i]);
ball.x+=ball.vx;
ball.y+=ball.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.vx *= -1;
}else if(ball.x - ball.radius<left){
ball.x = left + ball.radius;
ball.vx *= -1;
}
if(ball.y + ball.radius>bottom){
ball.y = bottom-ball.radius;
ball.vy *= -1;
}else if(ball.y - ball.radius<top){
ball.y = top + ball.radius;
ball.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){
this.radius = radius;
this.color = color;
init();
}
public function init():void{
graphics.beginFill(color);
graphics.drawCircle(0,0,radius);
graphics.endFill();
}
}