Skip to main content
Inspiring
April 16, 2013
Answered

Class Enter Frame Problem

  • April 16, 2013
  • 1 reply
  • 2567 views

Hello,

I am trying to make a simple game where an enemy chases the player if they get to close, and stops if the player moves a certain distance away.

I have a script for the enemy that looks like this :

------------------------------------------------------------------------------------------------------------------------------------------------

package  {

    import flash.display.MovieClip;

    import flash.events.Event;

    import flash.geom.Point;

    public class Enemy extends MovieClip

    {

        var positionX:int;

        var positionY:int;

        public function Enemy()

        {

            addEventListener(Event.ENTER_FRAME, enemyChase);

        }

        public function setPosition (playerX:int, playerY:int)

        {

            positionX = playerX;

            positionY = playerY;

        }

        public function enemyChase (event:Event):void

        {

            var des:Point = new Point(positionX, positionY);

            var loc:Point = new Point(x, y);

            var speed:Number = 1

            if (((Math.abs(positionX - x)) < 90) && ((Math.abs(positionY - y)) < 90))

            {

                var vel:Point = des.subtract(loc);

                vel.normalize( speed );

                x += vel.x;

                y += vel.y;

            }

        }

    }

}

------------------------------------------------------------------------------------------------------------------------------------------------

In my Main script I create a variable of the Enemy class like so :

var enemyScript:Enemy = new Enemy();

and on the Enter Frame function in the Main script I update the player's position for the Enemy class, like so :

enemyScript.setPosition (player.x, player.y);

However it isn't working. If I put something small into the Enemy class Enter Frame function like :

x += 1;

Then the enemies on the screen move every frame, so I know the class is linked properly. And I'm not getting any errors when I compile. What am I missing?

Thanks

P.S. the enemyChase function works when I put it in the Main function and link it directly using instance names, it's just getting it to work from a different class that's the problem

This topic has been closed for replies.
Correct answer moccamaximum

Awesome, finally got the position working!! It gives the correct position of the enemy on the stage .

Now unfortunately there is one final step haha. How do I get the playerX (positionX) and playerY (positionY) values from the setPosition function into the enemyChase function?


so in enemys init() function you call setPosition() without argument and then inside setPosition()

you get

positionX = MovieClip(root).player.x;

positionY = MovieClip(root).player.y;

1 reply

Inspiring
April 16, 2013

I am not sure I understood exactly what the problem is but if you attempt to update enemy position from outside the enemy instance - you don't need its internal enter frame event listener.

Try this:

package

{

          import flash.display.MovieClip;

          import flash.events.Event;

          import flash.geom.Point;

 

          public class Enemy extends MovieClip

          {

 

                    private var positionX:Number;

                    private var positionY:Number;

 

                    public function Enemy()

                    {

 

                    }

 

                    public function setPosition(playerX:Number, playerY:Number):void

                    {

                              positionX = playerX;

                              positionY = playerY;

                              enemyChase();

                    }

 

                    private function enemyChase():void

                    {

                              if (Math.abs(positionX - x) < 90 && Math.abs(positionY - y) < 90)

                              {

                                        var des:Point = new Point(positionX, positionY);

                                        var loc:Point = new Point(x, y);

                                        var speed:Number = 1

                                        var vel:Point = des.subtract(loc);

                                        vel.normalize(speed);

                                        x += vel.x;

                                        y += vel.y;

                              }

 

                    }

 

          }

}

BawlarAuthor
Inspiring
April 16, 2013

The problem was that the enemies weren't moving when I got close.

Thanks for the advice about not needing the internal event listener. Made things a bit neater, but unfortunately it still doesn't work. The enemies just don't move when I get close to them.

What I meant was, if I take the code from the enemyChase function and put it in my Main function (and use the instances to get the Point information) e.g. :

var des:Point = new Point(player.x, player.y);

var loc:Point = new Point(enemy1.x, enemy1. y);

Then it works. And also I know the Enemy Class is linked to the Enemy Symbol in the library cos I tested it.

But when I put the enemyChase code into another class, the enemies don't move when I get near them.

Still not getting any errors when I test either. Any ideas?

Thanks

Inspiring
April 16, 2013

First of all I am not sure what your usage of points tries to accomplish.

What do you see when enemyChase function is written like this?

package

{

          import flash.display.MovieClip;

          import flash.events.Event;

          import flash.geom.Point;

 

          public class Enemy extends MovieClip

          {

 

                    private var positionX:Number;

                    private var positionY:Number;

 

                    public function Enemy()

                    {

 

                    }

 

                    public function setPosition(playerX:Number, playerY:Number):void

                    {

                              positionX = playerX;

                              positionY = playerY;

                              enemyChase();

                    }

 

                    private function enemyChase():void

                    {

                              if (Math.abs(positionX - x) < 90 && Math.abs(positionY - y) < 90)

                              {

                                        var speed:Number = 1

                                        x += speed;

                                        y += speed;

                              }

 

                    }

 

          }

}