精灵的抛运动
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 ();
}
}
}
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.构建小球的抛运动
Throwing.as
package
{
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
public class Throwing extends Sprite
{
private var ball:Ball;
private var vx:Number;
private var vy:Number;
private var bounce:Number=-0.7;
private var gravity:Number=0.5;
private var oldX:Number;
private var oldY:Number;
public function Throwing ()
{
Init ();
}
public function Init ():void
{
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
ball=new Ball(20,0x00FFF0);
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
vx=Math.random()*20-10;
vy=-10;
addChild (ball);
ball.addEventListener (MouseEvent.MOUSE_DOWN,MouseDown);
addEventListener (Event.ENTER_FRAME,EnterFrame);
}
public function EnterFrame (e:Event):void
{
vy+=gravity;
ball.x+=vx;
ball.y+=vy;
if (ball.x<ball.radius)
{
ball.x=ball.radius;
vx*=bounce;
}
else if (ball.x+ball.radius>stage.stageWidth)
{
ball.x=stage.stageWidth-ball.radius;
vx*=bounce;
}
if (ball.y<ball.radius)
{
ball.y=ball.radius;
vy*=bounce;
}
else if (ball.y+ball.radius>stage.stageHeight)
{
ball.y=stage.stageHeight-ball.radius;
vy*=bounce;
}
}
public function MouseDown (e:MouseEvent):void
{
stage.addEventListener (MouseEvent.MOUSE_UP,MouseUp);
removeEventListener (Event.ENTER_FRAME,EnterFrame);
addEventListener (Event.ENTER_FRAME,TrackVelocity);
ball.startDrag ();
oldX=ball.x;
oldY=ball.y;
}
public function MouseUp (e:MouseEvent):void
{
stage.removeEventListener (MouseEvent.MOUSE_UP,MouseUp);
addEventListener (Event.ENTER_FRAME,EnterFrame);
removeEventListener (Event.ENTER_FRAME,TrackVelocity);
ball.stopDrag ();
}
public function TrackVelocity (e:Event):void
{
vx=ball.x-oldX;
vy=ball.y-oldY;
oldX=ball.x;
oldY=ball.y;
}
}
}
package
{
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
public class Throwing extends Sprite
{
private var ball:Ball;
private var vx:Number;
private var vy:Number;
private var bounce:Number=-0.7;
private var gravity:Number=0.5;
private var oldX:Number;
private var oldY:Number;
public function Throwing ()
{
Init ();
}
public function Init ():void
{
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
ball=new Ball(20,0x00FFF0);
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
vx=Math.random()*20-10;
vy=-10;
addChild (ball);
ball.addEventListener (MouseEvent.MOUSE_DOWN,MouseDown);
addEventListener (Event.ENTER_FRAME,EnterFrame);
}
public function EnterFrame (e:Event):void
{
vy+=gravity;
ball.x+=vx;
ball.y+=vy;
if (ball.x<ball.radius)
{
ball.x=ball.radius;
vx*=bounce;
}
else if (ball.x+ball.radius>stage.stageWidth)
{
ball.x=stage.stageWidth-ball.radius;
vx*=bounce;
}
if (ball.y<ball.radius)
{
ball.y=ball.radius;
vy*=bounce;
}
else if (ball.y+ball.radius>stage.stageHeight)
{
ball.y=stage.stageHeight-ball.radius;
vy*=bounce;
}
}
public function MouseDown (e:MouseEvent):void
{
stage.addEventListener (MouseEvent.MOUSE_UP,MouseUp);
removeEventListener (Event.ENTER_FRAME,EnterFrame);
addEventListener (Event.ENTER_FRAME,TrackVelocity);
ball.startDrag ();
oldX=ball.x;
oldY=ball.y;
}
public function MouseUp (e:MouseEvent):void
{
stage.removeEventListener (MouseEvent.MOUSE_UP,MouseUp);
addEventListener (Event.ENTER_FRAME,EnterFrame);
removeEventListener (Event.ENTER_FRAME,TrackVelocity);
ball.stopDrag ();
}
public function TrackVelocity (e:Event):void
{
vx=ball.x-oldX;
vy=ball.y-oldY;
oldX=ball.x;
oldY=ball.y;
}
}
}