AS3減速運(yùn)動(dòng)在做游戲開(kāi)發(fā)時(shí)很重要,今天我們就介紹一種標(biāo)準(zhǔn)的AS3減速運(yùn)動(dòng),其實(shí)就是高中物理知識(shí)。廢話少說(shuō),先看效果:
下面我們就介紹使用flashdevelop來(lái)做這個(gè)程序,打開(kāi)flashdevelop,新建一個(gè)as3項(xiàng)目(工程->新建工程->AS3 Project),然后在生成的Main.as中輸入以下代碼:
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* @author Jaja as-max.cn
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
for (var i:int = 0; i < 5; i++) {
var ball:Ball = new Ball;
ball.start(int(Math.random() * 60), int(Math.random() * 60));
addChild(ball);
}
}
}
}
這里我們需要一個(gè)Ball類,在Main.as同目錄下新建一個(gè)Ball.as文件,在其中輸入以下代碼:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
/**
* ...
* @author Jaja as-max.cn
*/
public class Ball extends Sprite
{
/**
* 開(kāi)始拖動(dòng)時(shí)的小球x坐標(biāo)
*/
private var dragStartX:int = 0;
/**
* 開(kāi)始拖動(dòng)時(shí)的小球y坐標(biāo)
*/
private var dragStartY:int = 0;
/**
* 開(kāi)始拖動(dòng)時(shí)的鼠標(biāo)x坐標(biāo)
*/
private var dragStartMouseX:int = 0;
/**
* 開(kāi)始拖動(dòng)時(shí)的鼠標(biāo)y坐標(biāo)
*/
private var dragStartMouseY:int = 0;
/**
* 上一幀小球x坐標(biāo)
*/
private var lastX:Number = 0;
/**
* 上一幀小球y坐標(biāo)
*/
private var lastY:Number = 0;
/**
* 小球的x軸速度,單位(像素/幀)
*/
private var speedX:Number = 0;
/**
* 小球的y軸速度
*/
private var speedY:Number = 0;
/**
* 指定當(dāng)前是否為正在拖動(dòng)
*/
private var isDraging:Boolean = false;
/**
* 小球加速度,單位(像素/幀/幀);
*/
private var acceleration:Number = 0.2;
public function Ball() :void
{
//build ui
graphics.beginFill(0x999999);
graphics.lineStyle(2);
graphics.drawCircle(15, 15, 15);
this.addEventListener(Event.ADDED_TO_STAGE, addThis);
}
private function addThis(event:Event):void {
this.addEventListener(Event.REMOVED_FROM_STAGE, removeThis);
this.addEventListener(MouseEvent.MOUSE_DOWN, downThis);
stage.addEventListener(MouseEvent.MOUSE_UP, upStage);
}
private function removeThis(event:Event):void {
this.removeEventListener(Event.REMOVED_FROM_STAGE, removeThis);
this.removeEventListener(Event.ADDED_TO_STAGE, addThis);
this.removeEventListener(MouseEvent.MOUSE_DOWN, downThis);
stage.removeEventListener(MouseEvent.MOUSE_UP, upStage);
this.removeEventListener(Event.ENTER_FRAME, enterFrame);
}
private function downThis(event:MouseEvent):void {
dragStartX = this.x;
dragStartY = this.y;
dragStartMouseX = this.parent.mouseX;
dragStartMouseY = this.parent.mouseY;
isDraging = true;
start(speedX, speedY);
}
private function enterFrame(event:Event):void {
if(isDraging){
this.x = dragStartX + this.parent.mouseX - dragStartMouseX;
this.y = dragStartY + this.parent.mouseY - dragStartMouseY;
speedX = this.x - lastX;
speedY = this.y - lastY;
lastX = this.x;
lastY = this.y;
}else {
this.x += speedX;
this.y += speedY;
if (afterSpeedX == 0 && afterSpeedY == 0) {
this.removeEventListener(Event.ENTER_FRAME, enterFrame);
}
var afterSpeedX:Number = Math.abs(speedX) - acceleration;
var afterSpeedY:Number = Math.abs(speedY) - acceleration;
if (afterSpeedX < 0) {
afterSpeedX = 0;
}
if (afterSpeedY < 0) {
afterSpeedY = 0;
}
speedX = speedX >= 0?afterSpeedX: -afterSpeedX;
speedY = speedY >= 0?afterSpeedY: -afterSpeedY;
}
if (this.x < 0) {
speedX = Math.abs(speedX);
}
if (this.x > stage.stageWidth - this.width) {
speedX = -Math.abs(speedX);
}
if (this.y < 0) {
speedY = Math.abs(speedY);
}
if (this.y > stage.stageHeight - this.height) {
speedY = -Math.abs(speedY);
}
}
private function upStage(event:MouseEvent):void {
isDraging = false;
}
public function start(speedx:int, speedy:int):void {
speedX = speedx;
speedY = speedy;
if(!this.hasEventListener(Event.ENTER_FRAME)){
this.addEventListener(Event.ENTER_FRAME, enterFrame);
}
}
}
}
保存文件,按F5或者Ctrl+Enter調(diào)試我們的程序,就可以看到效果了,可以用鼠標(biāo)拖動(dòng)小球試試。當(dāng)然如果你電腦上沒(méi)有安裝flashdevelop,用flash也可以編譯這些文件,將flash的場(chǎng)景綁定Main類就可以編譯了。