旋转多个物体
1.构建小球精灵
Ball.as
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
public var radius:Number;
public var color:uint;
public function Ball (radius:Number,color:uint)
{
this.radius=radius;
this.color=color;
Init ();
}
public function Init ():void
{
graphics.beginFill (color);
graphics.drawCircle (0,0,radius);
graphics.endFill ();
}
}
}
2. 构建多物体旋转
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
public var radius:Number;
public var color:uint;
public function Ball (radius:Number,color:uint)
{
this.radius=radius;
this.color=color;
Init ();
}
public function Init ():void
{
graphics.beginFill (color);
graphics.drawCircle (0,0,radius);
graphics.endFill ();
}
}
}
MultiRotation.as
package
{
import flash.display.Sprite;
import flash.events.Event;
public class MultiRotation extends Sprite
{
private var balls:Array;
private var numBalls:int=10;
public function MultiRotation ()
{
balls=new Array();
for (var i:Number=0; i<numBalls; i++)
{
var ball:Ball=new Ball(20,Math.random()*0xFFFFFF);
addChild (ball);
balls.push (ball);
ball.x=Math.random()*stage.stageWidth;
ball.y=Math.random()*stage.stageHeight;
}
addEventListener (Event.ENTER_FRAME,EnterFrame);
}
private function EnterFrame (e:Event):void
{
var angle:Number=(mouseX-stage.stageWidth/2)*0.001;
for (var i:Number=0; i<numBalls; i++)
{
var ball:Ball=balls[i];
var x1:Number=ball.x-stage.stageWidth/2;
var y1:Number=ball.y-stage.stageHeight/2;
var x2:Number=x1*Math.cos(angle)-y1*Math.sin(angle);
var y2:Number=y1*Math.cos(angle)+x1*Math.sin(angle);
ball.x=x2+stage.stageWidth/2;
ball.y=y2+stage.stageHeight/2;
}
}
}
}
package
{
import flash.display.Sprite;
import flash.events.Event;
public class MultiRotation extends Sprite
{
private var balls:Array;
private var numBalls:int=10;
public function MultiRotation ()
{
balls=new Array();
for (var i:Number=0; i<numBalls; i++)
{
var ball:Ball=new Ball(20,Math.random()*0xFFFFFF);
addChild (ball);
balls.push (ball);
ball.x=Math.random()*stage.stageWidth;
ball.y=Math.random()*stage.stageHeight;
}
addEventListener (Event.ENTER_FRAME,EnterFrame);
}
private function EnterFrame (e:Event):void
{
var angle:Number=(mouseX-stage.stageWidth/2)*0.001;
for (var i:Number=0; i<numBalls; i++)
{
var ball:Ball=balls[i];
var x1:Number=ball.x-stage.stageWidth/2;
var y1:Number=ball.y-stage.stageHeight/2;
var x2:Number=x1*Math.cos(angle)-y1*Math.sin(angle);
var y2:Number=y1*Math.cos(angle)+x1*Math.sin(angle);
ball.x=x2+stage.stageWidth/2;
ball.y=y2+stage.stageHeight/2;
}
}
}
}