【问题标题】:Move projectile to mouse-click position AS3将弹丸移动到鼠标点击位置 AS3
【发布时间】:2017-10-22 21:20:58
【问题描述】:

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

我尝试了不同的解决方案,但它们似乎都不适合我,所以我稍微清除了一下。

是的,我知道这很丑。

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);
}

【问题讨论】:

  • 向我们展示一些您已经尝试过的代码。最后我检查了一下,SO 是为了寻求帮助和建议,而不是为了生成现成的代码:)
  • @GurtejSingh 如果我给你看你会恨我的,说真的,我最好不要这样做,它太丑了,哦,好吧......
  • 顺便说一句,您的代码看起来不错。您可能可以使用 Tween 将大炮动画到 mouseX 和 mouseY 位置,并在完成补间时让它爆炸。我通常将 Tweenlite 用于我所有的动画,但那是第三方,所以如果你觉得它很酷,那就试试吧。这很容易。干杯。

标签: actionscript-3 flash animation actionscript


【解决方案1】:

您应该考虑要实施的流程。 首先它不是即时的,炮弹需要一些时间才能移动到鼠标点击的位置。 让我们从创建炮弹的函数开始:

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

【讨论】:

    猜你喜欢
    • 2019-03-12
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多