AS3 让敌人向鼠标移动

发布于 2024-12-07 10:43:32 字数 1100 浏览 1 评论 0原文

package {

import enemies.Enemy;
import flash.display.Sprite;
import flash.events.*;

public class Main extends Sprite {

    // a place to store the enemy
    public var enemy:Enemy;

    private function handleEnterFrame(e:Event):void {
        tweenIt(enemy.x, mouseX, 2);
    }

    private function tweenIt(variable:Number, target:Number, speed:Number):void{
        if (variable < target) {
            variable += speed;
        }

        if (variable > target) {
            variable -= speed;
        }
    }

    // this is the first code that is run in our application
    public function Main():void {

        addEventListener(Event.ENTER_FRAME, handleEnterFrame);
        // we create the enemy and store him in our variable
        enemy = new Enemy();

        // we add the enemy to the stage 
        addChild(enemy)

        enemy.x = Math.random() * stage.stageWidth;
        enemy.y = Math.random() * stage.stageHeight;

    }

}

}

敌人类中嵌入了一个位图。我正在使用FlashDevelop来编程。当我做一些像敌人.x+=1这样的事情时,它可以工作,但是当我尝试使用我的补间脚本时,无论鼠标的位置如何,敌人都会保持静止。 谢谢你, 布洛布斯塔

package {

import enemies.Enemy;
import flash.display.Sprite;
import flash.events.*;

public class Main extends Sprite {

    // a place to store the enemy
    public var enemy:Enemy;

    private function handleEnterFrame(e:Event):void {
        tweenIt(enemy.x, mouseX, 2);
    }

    private function tweenIt(variable:Number, target:Number, speed:Number):void{
        if (variable < target) {
            variable += speed;
        }

        if (variable > target) {
            variable -= speed;
        }
    }

    // this is the first code that is run in our application
    public function Main():void {

        addEventListener(Event.ENTER_FRAME, handleEnterFrame);
        // we create the enemy and store him in our variable
        enemy = new Enemy();

        // we add the enemy to the stage 
        addChild(enemy)

        enemy.x = Math.random() * stage.stageWidth;
        enemy.y = Math.random() * stage.stageHeight;

    }

}

}

The enemy class has a bitmapped embeded into it. I am using FlashDevelop to program. When I do something like enemy.x+=1 it works, but when I try using my tween it script the enemy stands still no matter what the position of the mouse.
Thank you,
Blobstah

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许仙没带伞 2024-12-14 10:43:32

我不是 AS3 开发人员,因此如果您的代码有任何问题,我无法帮助您,但如果您不确定如何以数学方式将敌人移向鼠标,请按以下方法操作。 (这不是代码,只是您想要计算的一般要点。我相信您可以将其转换为 AS3。)

首先,找到敌人和您的鼠标之间的距离。

xDistance = enemyPositionX - mousePositionX;
yDistance = enemyPositionY - mousePositionY;

然后,找到将敌人指向鼠标所需的旋转。

rotation = atan2(yDistance, xDistance);

最后,这是您想要放入 tweenIt 函数中的内容,以将敌人移向鼠标(每个函数调用 3 个像素)。

enemyPositionX -= 3 * cos(rotation);
enemyPositionY -= 3 * sin(rotation);

应该就是这样!我认为递归 因为这是我学会如何做到这一点的地方。

I'm not an AS3 developer so I can't help you if anything's wrong with your code, but if you're unsure of how to mathematically move an enemy towards the mouse, here's how. (This isn't the code, just the general jist of what you want to calculate. I'm sure you can convert it to AS3.)

First, find the distance between the enemy and your mouse.

xDistance = enemyPositionX - mousePositionX;
yDistance = enemyPositionY - mousePositionY;

Then, find the rotation needed to point the enemy towards the mouse.

rotation = atan2(yDistance, xDistance);

And lastly, here is what you want to put inside your tweenIt function to move the enemy towards the mouse (at 3 pixels per function call).

enemyPositionX -= 3 * cos(rotation);
enemyPositionY -= 3 * sin(rotation);

And that should be it! I give credit to Be Recursive because it's where I learned how to do this.

随梦而飞# 2024-12-14 10:43:32

您将敌人的 x 位置的传递给您的 tweenIt 函数,更改该值,然后丢弃结果。

换句话说,variable 是一个与 enemy.x不同的变量,尽管它的起始值来自 enemy.x< /代码>。

解决此问题的一种方法是将参数更改为实际敌人的引用:

private function handleEnterFrame(e:Event):void {
    tweenIt(enemy, mouseX, 2);
}

private function tweenIt(anEnemy:Enemy, target:Number, speed:Number):void{
    if (anEnemy.x < target) {
        anEnemy.x += speed;
    }

    // ...
}

You're passing in the value of the enemy's x position to your tweenIt function, changing that value, then throwing the result away.

In other words, variable is a different variable than enemy.x, even though it got its starting value from enemy.x.

One way to fix this is by changing the parameter to be a reference the the actual enemy:

private function handleEnterFrame(e:Event):void {
    tweenIt(enemy, mouseX, 2);
}

private function tweenIt(anEnemy:Enemy, target:Number, speed:Number):void{
    if (anEnemy.x < target) {
        anEnemy.x += speed;
    }

    // ...
}
迷爱 2024-12-14 10:43:32

因此,为了添加卡梅伦的答案,

您可以创建一个更通用的函数来更改变量。我将在下面演示一个小示例

private function tweenIt(anEnemy:Enemy, variableName:String, value:Number):void
{
    anEnemy[variableName] = value;
}

上面的函数将更新您想要的变量的当前值,因此如果您输入以下内容:

tweenIt(enemy, "width", 200);

这会将敌人对象的宽度更新为 200 :) 这应该可以解决问题: )

So, To add to the answer of Cameron

you can make a more general function to change variables. I will demonstrate a small example below

private function tweenIt(anEnemy:Enemy, variableName:String, value:Number):void
{
    anEnemy[variableName] = value;
}

The above function will update the current value of the variable you want, so if you would type the following:

tweenIt(enemy, "width", 200);

this would update the width of your enemy-object to 200 :) And this should do the trick :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文