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

Jonh Taco
  • 41
  • 3

3 Answers3

3

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.

inline
  • 695
  • 1
  • 7
  • 17
  • I just want the enemy to move coordinate wise. My math in this function is Ok. I believe the problem is with actionscript, but thank you for your help anyways. – Jonh Taco Oct 01 '11 at 16:56
1

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

    // ...
}
Cameron
  • 96,106
  • 25
  • 196
  • 225
  • would there be away to do this in a more general fashion? Like, make it work with whatever variable I throw at it? thanx – Jonh Taco Oct 01 '11 at 16:55
  • @Jonh: Well, no. You'll have to think of other ways to abstract this (interfaces and polymorphism come to mind). `blah += 4` is just shorthand for `blah = blah + 4`; clearly, the `blah + 4` part will yield the correct value, but instead of being assigned to the property of the object, it's being assigned to `blah` (so you're changing the value of the variable, but that variable has absolutely nothing to do with the property of the object -- the only link is that the property was used to determine the original value to pass in to the function) – Cameron Oct 01 '11 at 17:22
  • this can actually be done.. anEnemy["variableName"] = value; If you use this, it should do the trick :) – Michiel Standaert Oct 01 '11 at 18:19
  • 1
    @Michiel: Ooh, I forgot about that. You should add that as an answer. – Cameron Oct 01 '11 at 18:58
0

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

Michiel Standaert
  • 4,096
  • 7
  • 27
  • 49