Skip to main content
Participant
June 1, 2010
Answered

questions about OOP

  • June 1, 2010
  • 1 reply
  • 611 views

good day to everyone, I'm working on a enemy class now the idea i have is to give it a enemyUpdate methode to make it move on it's own using a while loop

<code>

        public function updateEnemy():void
        {
            var i:int =0;
            while(i > 30)
            {
                this.PositionX = +10;
            }
        }

</code>

Ok so i figured out its partly caused because i'm not increasing "i" in my loop

Now my problem is i the line of code above doesnt properly move the enemy why so?
Thanks for the info

This topic has been closed for replies.
Correct answer

If there won't be too much enemy objects at once on the stage, try this - in the enemy class add event listener for enter frame event:

        this.addEventListener(Event.ENTER_FRAME, updateEnemy);

and update position in the handler.


        private function updateEnemy(e:Event):void

        {
            this.x += 5;
        }

To decide if move enemy or not you can use if statement in the updateEnemy function.

1 reply

Correct answer
June 1, 2010

If there won't be too much enemy objects at once on the stage, try this - in the enemy class add event listener for enter frame event:

        this.addEventListener(Event.ENTER_FRAME, updateEnemy);

and update position in the handler.


        private function updateEnemy(e:Event):void

        {
            this.x += 5;
        }

To decide if move enemy or not you can use if statement in the updateEnemy function.

elexion22Author
Participant
June 1, 2010

so to make the enemy move i should use if else statement rather en a while loop?

if so would it be something like this
<code>

            if(this.x <= 0)
            {
                this.x += 1;
            }

</code>

June 1, 2010

if statement would be more porper here than while.

As you mentioned before, while is a loop - it executes the code as the condition is satisfied, but it do not corresponds in any way with time or timeline, you don't have contoll on it's frequency, that is important in animating. I.e. flash player executes code in while statement as fast as the computing power allows, that's much quicker than e.g. 24 times per second.

Enter frame event handler executes with each frame, so it updates state of an object once per frame.

Your code with if is perfectly fine.