English 中文(简体)
AS3 促使敌人朝 mo方向移动
原标题:AS3 Make enemy move towards mouse
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

问题回答

我不是AS3开发商,因此,如果与你的法典有任何错误,我可以帮助你,但如果你不掌握如何从数学上把敌人推向 mo子的话,我会怎样做。 (这在法典上,只是你想要计算的东西的一般 j。) 我确信你可以将其转换成S3。

首先,发现敌人与你们的摩擦之间的距离。

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

之后,找到了把敌人点向 mo点所需的轮换。

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.

页: 1

In other words, variable is a different variable than enemy.x, even though it got its starting value from 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;
    }

    // ...
}

So, To add to the answer of Cameron

您可以发挥更一般性的作用,改变变量。 我将举一个小例子,说明如下:

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

这将更新贵国敌人的宽度,但以200为限: 这应当做到:





相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

Red5 Security Tutorial

I am looking for a step by step tutorial on securing Red5 from intrusion. This seems to be a question that comes up alot in a google search, but is never really answered in a way that makes sense to ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

visible property of DisplayObject

For example I have a hierarchy of movie clips. mc1 is a child of mc, and mc2 is a child of mc1. Turns out that when I set mc1.visible = false; mc2.visible stays true. Is that supposed to happen?...

热门标签