Skip to main content
April 14, 2006
Question

Random time events

  • April 14, 2006
  • 12 replies
  • 1306 views
Hello!
Im building a small action shooter for a client(this is a fun client, for once..) Im having trouble figure out how to randomly 'drop item's from the top of the screen. The levels last for two minuets, so within that period, i would like to drop a few items.

I am not sure where to start with this one!

thanks for the help-
thanks.
This topic has been closed for replies.

12 replies

kglad
Community Expert
Community Expert
April 20, 2006
i found him and presented the issue to him in the thread "how to get fps of a movieclip".
kglad
Community Expert
Community Expert
April 18, 2006
well, i'm not so sure. it's likely that i'm partially correct (or partially incorrect). jeckyll has more expertise in this area. i just haven't seen any posts from him lately.
Inspiring
April 18, 2006
Me neither. Well, let's keep an eye out for him...
Have a good day.
Inspiring
April 17, 2006
I think we can safely assume you're correct.
kglad
Community Expert
Community Expert
April 17, 2006
it's documented that named functions are compiled first. the rest of my conclusions (ie, when a named function is encountered later during code execution it is ignored) are inferences and may be incorrect.
kglad
Community Expert
Community Expert
April 16, 2006
i was just typing an answer and was explaining that i found nothing to explain the issue, when i realized that i had found the explaination.

named functions are compiled before the other actionscript in a frame. if a named function is within a block of code (like an if-statement or another function) it is not compiled before the other actionscript and when it is encountered during execution of the conditional block of code, flash probably figures it has already been compiled and fails to compile it.
Inspiring
April 16, 2006
>>it is not compiled before the other actionscript
If I understand correctly, that's the conclusion of what is happening or is that documented?
kglad
Community Expert
Community Expert
April 15, 2006
that's correct, you cannot define a function inside an if-statment. if drop() were ever defined it would always be defined unless and until it is redefined or set to null.

i had assummed it would be defined in the first pass through that if-statement and no longer would the code defining drop() need to be encountered.
Inspiring
April 15, 2006
Paging Jeckyl...

My best guess is:
The initial setInterval receives a random time to wait to call drop(). During that time codeExecuted gets set to true so that when execution of the code starts over... drop() doesn't get defined anymore.
Inspiring
April 15, 2006
I'm beginning to think you can't define a function in an if() all together...
kglad
Community Expert
Community Expert
April 15, 2006
ok, it's related to the two different ways you can define functions - either named or anonymous. i've got to check further, but the following works:
kglad
Community Expert
Community Expert
April 15, 2006
well, you're right luigi. that doesn't work.

but why doesn't it? why is drop() never defined?

even though the code defining drop() is encountered by flash, at no time is drop() defined.
kglad
Community Expert
Community Expert
April 15, 2006
the function doesn't have to be outside the if. it's better if it's left inside because there's no need to repeatedly define a function and it's a waste of resources to repeatedly execute the code to define a function.
Inspiring
April 15, 2006
Don't get that. The thing you need to cancel out is initializing the first dropInterval on the timeline. So, this would also work:
if(!dropInterval){
dropInterval=setInterval(drop,dropTime);
}
With the function inside the if, the function won't get called since codeExecuted is true...
April 14, 2006
yes, the game runs from one frame...just an actionscript loop.
kglad
Community Expert
Community Expert
April 14, 2006
that's a problem. that code given by luigi should only be executed once. try:

Inspiring
April 14, 2006
With the function outside of the if...