Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am creating a 2d flash game using Flixel 2.5 in Flash Builder. I am trying to reverse enemy movement on an interval so that they will move back and forth over a set space. I felt that a timer would be best for this situation. The enemy class extends FlxSprite. I have three class level variables to help with managing the sprite's direction:

private var _movementTimer:Timer;
private var _movementTimerEvent:TimerEvent;
private var _forward:Boolean;
public var _directionSet:int = 0;

In the constructor, I instantiate the timer like this:

//create timer for movement
_movementTimer = new Timer(3000);
//set listener for movement
//start timer
//set initial direction
_forward = true;

The function called on the timer interval is as follows:

/*Reverse direction of this sprite*/
        public function SetDirection():void
            _directionSet++; //increment direction set for debug purposes
            FlxG.log("direction set");

                this._forward = false;
                //set the direction the sprite is facing
                this._facing = RIGHT;
                this._forward = true;
                //set the direction the sprite is facing
                this._facing = LEFT;


Then, the update function evaluates the _forward flag to determine direction of movement:

/*Update this sprite*/
        override public function update():void

                this.acceleration.x = this.maxVelocity.x * .5;
                this.acceleration.x = -this.maxVelocity.x * .5;

However, my sprite is not reversing direction, it just continues forward. What am I missing?


I added additional variables and calls to FlxG.log for debugging purposes, and it seems that SetDirection is never called, but I am not sure why.

share|improve this question
Add some debug output, and see if SetDirection ever gets called. – Kylotan Feb 21 '13 at 12:36
I added some debug code, using a call to FlxG.log in the method: _directionSet++; //increment direction set for debug purposesFlxG.log("direction set"); FlxG.log(_directionSet); And it seems that it is never called. But I am not sure why. I assume there is maybe something with how the FlxG game loop is structured. – Christian Feb 21 '13 at 15:17
Unfortunately I don't know enough about Flash or Flixel to help further. It might be worth commenting out all the game code to see if Flixel and Timers are just fundamentally incompatible even if there's nothing else in the app to interfere. – Kylotan Feb 21 '13 at 16:22
Your signature for SetDirection() is incorrect. It should be SetDirection(e:TimerEvent):void. That said, I recall Flash being really forgiving about that so it probably isn't your problem. Are you removing the listener anywhere in your code? Maybe that's happening accidentally? – NoobsArePeople2 Feb 21 '13 at 17:26
@NoobsArePeople2 o.m.g. It was the signature for SetDirection(). Changing it to SetDirection(e:TimerEvent):void as you indicate corrected the issue. If you post your comment as an answer I will mark it as correct. Thank you for the help. – Christian Feb 21 '13 at 18:21
up vote 1 down vote accepted

Your signature for SetDirection() is incorrect. It should be SetDirection(e:TimerEvent). Flash can be forgiving about this sort of thing resulting in some infuriatingly subtle errors.

share|improve this answer

Using Flash's built inTimer isn't a great idea in Flixel. You should really use the Update function in conjunction with FlxG.elapsed, as this will respect pausing and resuming the game using Flixel.

Here's an overview of how to use Flixel's timing:

share|improve this answer
For the record, I took your advice and changed the implementation to a counter using FlxG.elapsed, however the previous answer came sooner and corrected the initial issue problem, so I marked his answer correct and gave yours an upvote for helpfulness. Thanks for the advice! – Christian Feb 21 '13 at 19:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.