将射弹移动到鼠标单击位置AS3

时间:2017-05-23 04:52:43

标签: actionscript-3 flash animation actionscript

我有一个大炮和炮弹。如何使炮弹从大炮移动到鼠标点击位置并停止/消失/激活爆炸动画?

我尝试了不同的解决方案,但似乎没有一个能为我工作,所以我清理了一下。

是的,我知道它很难看。



import flash.events.MouseEvent;
import flash.media.Sound;
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Mouse;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.utils.Timer;
import flash.display.Sprite;

addEventListener(Event.ENTER_FRAME, enterFr);

function enterFr(e:Event)
{
	aims.x = mouseX;
	aims.y = mouseY;
}

Mouse.hide();

zamok.addEventListener(MouseEvent.CLICK, fire);

function fire(m:MouseEvent)
{
	var s:Sound = new cannonFire();
	s.play();
	var explo:boom = new boom();
	explo.x = mouseX;
	explo.y = mouseY;
	addChild(explo);
}




1 个答案:

答案 0 :(得分:2)

那么你应该想到你想要实现的过程。 首先,它不是即时的,炮弹需要一些时间才能移动到鼠标点击的位置。 让我们从一些将创建炮弹的功能开始:

private function fireCannonBall(target: Point):void 
{
    const cannonBall:CannonBall = new CannonBall(); // you need to implement this class, or just use some MovieClip from library;

    cannonBall.x = initialPosition.x; // initial position is a point where your cannon is located.
    cannonBall.y = initialPosition.y; 

    addChild(cannonBall);

    // I suggest using TweenNano, but it has some limitations, read the License Agreement carefully
    TweenNano.to(cannonBall, 15 /* animation duration */, {x: target.x, y: target.y, onComplete: makeExplosion, onCompleteParams: [cannonBall]});
}

private function makeExplosion(cannonBall: CannonBall):void 
{
    /* I leave this part to you, here you might want to launch some explosion animation */
}

现在我们需要处理点击:

private function onMouseClick(e: MouseEvent):void 
{
    const target: Point = new Point(stage.mouseX, stage.mouseY);
    //and launch the cannonBall:
    fireCannonBall(target);
}

就是这样,粗略。

要了解有关TweenNano的更多信息,请点击以下链接: https://greensock.com/tweennano-as