开发者

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, sp开发者_高级运维eed: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


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.


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

    // ...
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜