Macromedia Director: Using Stepframe instead of EnterFrame with sprites




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 - 2 responses

Displayed newest to oldest. Leave a comment.
blog.hanfordlemoore » Blog Archive » Macromedia Director: Stepframe wrote:   
[...] User Interface , Art, Design, Games « Macromedia Director: Using Stepframe instead of EnterFrame with sprites [...]
Rachel Goldstein wrote:   
Wow, I never used StepFrame that way. Very informative blog entry, thanks!

Leave a comment

Name:
Website:
Comment:

Email:
Captcha:
Choose the reel-to-reel tape image from below:

           

           

 

Video Game Design

User Interface Design

Creative & fun stuff

 

I'm Hanford Lemoore. My parking skills are unparalleled.

I design things in Silicon Valley; mostly consumer electronics media players. Perhaps I can design things for you. Check out my UI Portfolio.

When I'm not making things for other people, I'm usually making video games. more

 

Please use our contact form.

 

RSS 2.0

 

monolux.com

tikiroom.com

junkyardclubhouse.com

 


 

   


Copyright 2010 Hanford Lemoore | Blog | About | Portfolio | Contact
Powered by Olark