Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
_movementTimer.addEventListener(TimerEvent.TIMER,SetDirection);
//start timer
_movementTimer.start();
//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");
            FlxG.log(_directionSet);

            if(this._forward)
            {
                this._forward = false;
                //set the direction the sprite is facing
                this._facing = RIGHT;
            }
            else
            {
                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
        {
            super.update();

            if(this._forward)
            {
                this.acceleration.x = this.maxVelocity.x * .5;
            }
            else
            {
                this.acceleration.x = -this.maxVelocity.x * .5;
            }
        }

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

EDIT:

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
1  
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
1  
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
show 1 more comment

2 Answers 2

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
add comment

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:

http://flashgamedojo.com/wiki/index.php?title=Timer_(Flixel)

share|improve this answer
1  
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
add comment

Your Answer

 
discard

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.