forked from: forked from: forked from: forked from: forked from: forked from: forked from: Yugop氏のようなBalls
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.utils.Timer;
public class Balls extends Sprite
{
private var balls:Array;
private var numBalls:Number = 10;
private var bounce:Number = -0.5;
private var spring:Number = 0.7;
private var gravity:Number = 1.3;
private var friction:Number = 0.9;
private var oldX:Number;
private var oldY:Number;
private var emTimer:Timer;
private var countNm:int = 0;
private var fastSp:Number = -100;
/*
/yugop氏のような感じで。本当はどうなっているのでしょうか?
/ボールはドラッグ、スローできます。
*/
public function Balls()
{
init();
}
private function init():void
{
balls = new Array();
for(var i:uint = 0; i < numBalls; i++)
{
var ball:Ball = new Ball(Math.random() * 20 + 25, 0x000000);
ball.x = Math.random() * stage.stageWidth;
ball.y = Math.random() * stage.stageHeight;
ball.vx = Math.random() * 6 - 3;
ball.vy = Math.random() * 6 - 3;
addChild(ball);
balls.push(ball);
ball.addEventListener(MouseEvent.MOUSE_DOWN, emDown);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
emTimer = new Timer(1000);
emTimer.addEventListener(TimerEvent.TIMER, reDuctBall);
emTimer.start();
}
private function emDown(evt:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, emUp);
evt.target.addEventListener(Event.ENTER_FRAME, throwBall);
}
private function emUp(evt:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, emUp);
evt.target.removeEventListener(Event.ENTER_FRAME, throwBall);
}
private function throwBall(evt:Event):void
{
evt.target.x = mouseX ;
evt.target.y = mouseY;
evt.target.vx = evt.target.x - oldX;
evt.target.vy = evt.target.y - oldY;
oldX = evt.target.x;
oldY = evt.target.y;
}
private function onEnterFrame(event:Event):void
{
for(var i:uint = 0; i < numBalls - 1; i++)
{
var ball0:Ball = balls[i];
for(var j:uint = i + 1; j < numBalls; j++)
{
var ball1:Ball = balls[j];
var dx:Number = ball1.x - ball0.x;
var dy:Number = ball1.y - ball0.y;
var dist:Number = Math.sqrt(dx * dx + dy * dy);
var minDist:Number = ball0.radius + ball1.radius;
if(dist < minDist)
{
var angle:Number = Math.atan2(dy, dx);
var tx:Number = ball0.x + dx / dist * minDist;
var ty:Number = ball0.y + dy / dist * minDist;
var ax:Number = (tx - ball1.x) * spring;
var ay:Number = (ty - ball1.y) * spring;
ball0.vx -= ax;
ball0.vy -= ay;
ball1.vx += ax;
ball1.vy += ay;
}
}
}
for(i = 0; i < numBalls; i++)
{
var ball:Ball = balls[i];
move(ball);
}
}
private function move(ball:Ball):void
{
ball.vx *= friction;
ball.vy *= friction;
ball.vy += gravity;
ball.x += ball.vx;
ball.y += ball.vy;
if(ball.x + ball.radius > stage.stageWidth)
{
ball.x = stage.stageWidth - ball.radius;
ball.vx *= bounce;
}
else if(ball.x - ball.radius < 0)
{
ball.x = ball.radius;
ball.vx *= bounce;
}
if(ball.y + ball.radius > stage.stageHeight)
{
ball.y = stage.stageHeight - ball.radius;
ball.vy *= bounce;
}
else if(ball.y - ball.radius < 0)
{
ball.y = ball.radius;
ball.vy *= bounce;
}
}
private function reDuctBall(evt:TimerEvent):void
{
var ball1:Ball = balls[countNm];
ball1.addEventListener(Event.ENTER_FRAME, reDuctoBall2);
}
private function reDuctoBall2(evt:Event):void
{
var reductNm:int = 3;
var targetBall:Object = evt.target;
targetBall.scaleX += (0 - evt.target.scaleX) / reductNm;
targetBall.scaleY += (0 - evt.target.scaleY) / reductNm;
if (targetBall.scaleX < 0.1)
{
replaceBall(targetBall);
targetBall.removeEventListener(Event.ENTER_FRAME, reDuctoBall2);
countNm++;
}
}
private function replaceBall(ball:Object):void
{
ball.scaleX = ball.scaleY = 1;
ball.x = Math.random() * stage.stageWidth;
ball.y = stage.stageHeight;
ball.vy = fastSp;
ball.alpha = Math.random() * 0.8 + 0.2;
if (countNm == balls.length - 1 )
{
countNm = 0;
}
}
}
}
import flash.display.*;
import flash.events.*;
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();
}
}