Macromedia Director: Using Stepframe instead of EnterFrame with spritesDec-27-05, 4:30 pm by HanfordFile under: Shockwave, Adobe, Director, Macromedia I often use state machines inside of ExitFrame events in sprites. For example, I might have an ExitFrame event or EnterFrame event in a sprite to handle the occasional fade in and fade out:
On ExitFrame Me
case mode of
#off:-- Do nothing
#fadein:
-- Do my fade in code
blend = blend + 10
-- Etc. Etc.
#fadeout:
-- Do my fade out code
blend = blend - 10
-- Etc. Etc.
end case
end
In the state machine above, the states switch based on the value of "mode". It can be #off, #fadein, or #fadeout. And I'd use an Exitframe script like that to have a sprite fade in, then sit there silently until it's told to fade out. So 99% of the sprite's life is spent with mode = #off and therefore not doing anything. But Exitframe is still being triggered every frame. There's a little bit of overhead in this. Multiply it by 100s of sprites and it results some significant slowdown. But I wanted better results. I didn't like the thought of sprites having their ExitFrame event called every single frame when 99% of the time they were'nt doing anything. What I wanted was a way to turn ExitFrame on and off. And so I decided to try the StepFrame event. I had been using StepFrame for years in parent scripts, but never in sprite scripts. The great thing about StepFrame is it can be turned on and off at will. To trigger Stepframe events in an object, do this: add the actorlist, me and to stop the triggering of stepframe events in an object, do this: deleteone the actorlist,me The default mode for a sprite like this would be to not have Stepframe events, and to only turn on StepFrame events when they're needed. For example, let's say we wanted to have the sprite fade out. We could use Stepframe in the following way:
property mode
on FadeOut me
if mode <> #fade then
add the actorlist, me
end if
mode = #fadeout
end
on StepFrame me
case mode of
#fadeout:
currentBlend = sprite(me.spritenum).blendlevel
sprite(me.spritenum).blendlevel = max(0,currentBlend - 10)
if sprite(me.spritenum).blendlevel = 0 then
deleteone the actorlist, me
mode = #off
end if
end case
end
The FadeOut function adds the current object (me) to the Actorlist. This will cause the sprite to start receiving StepFrame events. It also sets a Mode variable to be "fadein". This keeps the object from being added to the Actorlist twice. The Stepframe code itself includes a case statement to check the mode. It's not needed in the example above since there's only one mode, but it would be used if I were to add a "fadeIn" state as well. The Stepframe code also reduces the sprite's blendlevel value by 10 every frame, until it reaches zero. When it does, it deletes itself from the actorlist and turns the mode to #off. Once it's deleted from the Actorlist, it will stop getting Stepframe events. I've been using it in sprites as a replacement for EnterFrame and so far it's been working great. I am sure it has it's own set of issues. There is a bit of upkeep in keeping close track of the state to make sure the object doesn't get added to the actorlist multiple times. And it's also important to have a sprite remove itself from the actorlist in an EndSprite event. Like I wrote earlier, I've been using StepFrame for a while now but I'm new to using it within a sprite itself, so I don't know all the pros and cons yet. As I discover more, I'll post here. Update: Make sure you see my next post, which follows up on the caveats of dealing with Stepframe in sprites. Feedback - 4 responsesDisplayed newest to oldest. Leave a comment.vintage car insurance ireland wrote: 2 days ago, 11:43 pm I'm keen on this considerably. You recognize precisely what that you are talking about, exactly where people are originating from about this concern. Now i'm delighted we had the bundle to be able to stagger over your blog. Its definitely a vital difficulty that doesn't plenty of consumers are speaking about and i am happy i obtained the opportunity to see many the aspects. vintage car insurance ireland blog.hanfordlemoore » Blog Archive » Macromedia Director: Stepframe wrote: Jan-8-06, 4:35 pm [...] User Interface , Art, Design, Games « Macromedia Director: Using Stepframe instead of EnterFrame with sprites [...] Rachel Goldstein wrote: Jan-8-06, 9:49 am Wow, I never used StepFrame that way. Very informative blog entry, thanks! Leave a commentComments are displayed on posts and visible to all site visitors. |
|
I make things. From consumer electronics, to video games, to theme park attractions. Perhaps I can make things for you! Check out my portfolio. When I'm not making things for other people, I'm usually experimenting.
Follow me on Twitter. Message me on Facebook. Email me using my contact form.
|
|
Thanks for the informative and also detailed investigation as of this post. This kind of nutrients providing us all target info, anyone perfectly confirmed advice in this post, My spouse and i follow to know this tender of stuff.