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.
Bookmarks