小球回弹模拟
1.构建小球

 Ball.as
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.构建回弹动画

 Bouncing.as
Bouncing.aspackage
{
import flash.events.Event;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
public class Bouncing extends Sprite
{
private var ball:Ball;
private var vx:Number;
private var vy:Number;
private var radius:Number=20;
public function Bouncing ()
{
Init ();
}
public function Init ():void
{
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
ball=new Ball(radius,0xff0000);
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
vx=20*Math.random()-10;
vy=20*Math.random()-10;
addChild (ball);
addEventListener (Event.ENTER_FRAME,Active);
}
private function Active (e:Event):void
{
ball.x+=vx;
ball.y+=vy;
if (ball.x<radius)
{
ball.x=radius;
vx*=-1;
}
if (ball.x>stage.stageWidth-radius)
{
ball.x=stage.stageWidth-radius;
vx*=-1;
}
if (ball.y<radius)
{
ball.y=radius;
vy*=-1;
}
if (ball.y>stage.stageHeight-radius)
{
ball.y=stage.stageHeight-radius;
vy*=-1;
}
}
}
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号