Flash Advisor logo
:: Desktop Shortcut
:: Flash Help
Advice from Experts

+ Reply to Thread
Results 1 to 2 of 2

Thread: Timer help

  1. #1

    Default Timer help

    I encountered a problem regarding timers while programming my game. I wanted the player to be blown by the wind for 2 seconds every 2 seconds if the variable shows the corresponding number. I hit a wall, for the "blow" function doesn't get executed unless the "dir" variable is equal to 2 from the start. Any help would be appreciated. Here's the related code:

    Code:
    private function Wind()
    {
        dir = Math.floor( Math.random() * 3 );
        blowTime = new Timer( 1, 2000 );
        if ( dir == 2 )
        {
            blowTime.addEventListener( TimerEvent.TIMER, blow );
        }
        blowTime.start();
        
        blowTime.addEventListener( TimerEvent.TIMER_COMPLETE, callWind );
    
    }
    
    private function blow( e:TimerEvent ):void
    {
        xspeed += 10;
    }
    
    private function callWind( e:TimerEvent )
    {
        blowTime.stop();
        blowTime.removeEventListener( TimerEvent.TIMER, blow );
        blowTime.removeEventListener( TimerEvent.TIMER_COMPLETE, callWind );
        Wind();
    }
    Last edited by Vlykarye; 08-24-2011 at 02:10 PM.

  2. # ADS
    Join Date
    Always
    Posts
    Many
     
  3. #2
    Join Date
    Jun 2009
    Location
    Houston, Tx
    Posts
    485

    Default

    First off, the problem is very simple:
    Code:
     blowTime = new Timer( 1, 2000 );
    This code says, "Make timer event go off every .001 seconds, 2000 times."
    Swap the numbers.

    Second, your code logic can be optimized. Consider the following instead:
    Code:
    private function Wind():void
    {
        if ( Math.floor( Math.random() * 3 ) == 2 )
        {
            blow();
        }
        
        blowTime = new Timer( 2000, 1 );
        blowTime.addEventListener( TimerEvent.TIMER_COMPLETE, callWind );
        blowTime.start();
    }
    
    private function blow():void
    {
        xspeed += 10;
    }
    
    private function callWind( event:TimerEvent ):void
    {
        blowTime.stop();
        blowTime.removeEventListener( TimerEvent.TIMER_COMPLETE, callWind );
        
        Wind();
    }
    • Take out the "dir" variable. It simply isn't needed here, and the logic could be better. From looking at this little code, I'm guessing that you want the wind to randomly get stronger every time it blows. The code says that you want the speed to change before the wind blows, so instead of putting another event on your timer, just call the "blow" function up front if the random number is 2. There is a chance that I'm wrong here, since you haven't given me much code to go off of, and even less description in your post. If you want the wind to get stronger just before the wind blows, then move the if statement to the "callWind" function, before or after "Wind();", depending on when you want the wind to get stronger.
    • The code for your random number will generate 0, 1, and 2, randomly. There is a 1/3 chance of function "blow" being called.
    Finally. Let me give you some pointers:
    • If you ever use a variable in the future to hold a direction: Instead of using "dir", which sounds like directory, change it to "direction", which is much clearer. If anyone ever looks at your code for any reason ever, they won't get the feeling that a newbie programmer wrote the code.
    • Change "e" to "event", even though you will probably never use the event object. It sounds more professional than simple "e".
    • Change "blowTime" to "blowTimer" or even "windTimer" perhaps. I'm sure you spent a while thinking about which one to use, so consider my suggestion here as well.
    • Change the function name "blow" to something else. Right now, it sounds like this is the function that makes the wind blow. However, that is not the case, so the name is very misinforming.
    • Don't forget your :void or whatever return type after each function.
    Last edited by Vlykarye; 08-24-2011 at 02:13 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Sponsors
Create Speaking Characters for your website and Flash movies. 15 Day Free Trial