flash on 2010-1-24
♥0 |
Line 90 |
Modified 2010-01-24 17:35:11 |
MIT License
archived:2017-03-20 13:32:35
ActionScript3 source code
/**
* Copyright _ryotaros ( http://wonderfl.net/user/_ryotaros )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/fGEL
*/
package {
import flash.accessibility.Accessibility;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public var centerBall:ball;
public var centerRadius:int = 50;
public var centerDists:Array;
public var ballNum:int = 10;
public var bounce:int = -1;
public var balls:Array;
public function FlashTest() {
init();
}
public function init():void{
centerBall = new ball(centerRadius, 0xFF0000);
addChild(centerBall);
centerBall.x = stage.stageWidth / 2;
centerBall.y = stage.stageHeight / 2;
balls = [];
for (var i:int = 0; i < ballNum;i++){
balls[i] = new ball(Math.random()*10+10, 0xFFFF00);
addChild(balls[i]);
balls[i].x = Math.random()*stage.stageWidth;
balls[i].y = Math.random()*stage.stageHeight;
balls[i].dx = Math.random()*20-10;
balls[i].dy = Math.random()*20-10;
}
addEventListener(Event.ENTER_FRAME, enterframe);
}
public function enterframe(e:Event):void{
for (var i:int = 0; i < ballNum; i++ ){
balls[i].x += balls[i].dx;
balls[i].y += balls[i].dy;
if (balls[i].x + balls[i].radius > stage.stageWidth){
balls[i].x = stage.stageWidth - balls[i].radius;
balls[i].dx *= bounce;
balls[i].dx *= 0.6;
}
if (balls[i].x < balls[i].radius){
balls[i].x = balls[i].radius;
balls[i].dx *= bounce;
balls[i].dx *= 0.6;
}
if (balls[i].y + balls[i].radius > stage.stageHeight){
balls[i].y = stage.stageHeight - balls[i].radius;
balls[i].dy *= bounce;
balls[i].dy *= 0.6;
}
if (balls[i].y < balls[i].radius){
balls[i].y = balls[i].radius;
balls[i].dy *= bounce;
balls[i].dy *= 0.6;
}
var dx:int = centerBall.x - balls[i].x;
var dy:int = centerBall.y - balls[i].y;
var dist:int = Math.sqrt(dx * dx + dy * dy );
if (dist < centerRadius + balls[i].radius){
balls[i].dx *= 1.1;
balls[i].dy *= 1.1;
var angle:Number = Math.atan2(dy,dx);
if (angle < 90){
balls[i].dx *= bounce;
}else if (angle < 180){
balls[i].dy *= bounce;
} else if (angle < 240) {
} else {
}
}
}
}
}
}
import flash.display.Sprite;
class ball extends Sprite {
public var radius:int;
public var color:uint;
public var dx:Number;
public var dy:Number;
public function ball (rad:int =10,clr:uint=0x000000):void {
this.radius = rad;
this.color = clr;
init();
}
public function init():void{
graphics.beginFill(color);
graphics.drawCircle(0,0,radius);
graphics.endFill();
}
}