写了好久没写出来,网上找到了,谢谢作者
原文:http://hi.baidu.com/sl19880127sl/blog/item/4e37f48fcfbac9e2f11f3680.html
AS3减速运动在做游戏开发时很重要,今天我们就介绍一种标准的AS3减速运动,其实就是高中物理知识。废话少说,先看效果:
下面我们就介绍使用flashdevelop来做这个程序,打开flashdevelop,新建一个as3项目(工程->新建工程->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);
}
}
}
}
这里我们需要一个Ball类,在Main.as同目录下新建一个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
{
/**
* 开始拖动时的小球x坐标
*/
private var dragStartX:int = 0;
/**
* 开始拖动时的小球y坐标
*/
private var dragStartY:int = 0;
/**
* 开始拖动时的鼠标x坐标
*/
private var dragStartMouseX:int = 0;
/**
* 开始拖动时的鼠标y坐标
*/
private var dragStartMouseY:int = 0;
/**
* 上一帧小球x坐标
*/
private var lastX:Number = 0;
/**
* 上一帧小球y坐标
*/
private var lastY:Number = 0;
/**
* 小球的x轴速度,单位(像素/帧)
*/
private var speedX:Number = 0;
/**
* 小球的y轴速度
*/
private var speedY:Number = 0;
/**
* 指定当前是否为正在拖动
*/
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调试我们的程序,就可以看到效果了,可以用鼠标拖动小球试试。当然如果你电脑上没有安装flashdevelop,用flash也可以编译这些文件,将flash的场景绑定Main类就可以编译了。