Articles Archive
Articles Search
Director Wiki
 

Turn off sprites on a mouseDown

December 3, 1998
by Pat McClellan

Dear Multimedia Handyman,

I have some sprites which I want to make invisible when the user does a mouseDown. For some reason, it doesn't work because the sprites don't disappear until the mouseUp. Here's my button script. Can you help?


on mouseDown
  set the visible of sprite 1 to False
  set the visible of sprite 2 to False
  set the visible of sprite 3 to False
  set the visible of sprite 4 to False
  set the visible of sprite 5 to False
  set the visible of sprite 6 to False
end

Beatrice Kay

Dear Beatrice,

I think the reason you're having problems is that while the mouse is down, no other events -- such as redrawing the stage -- are taking place. You can force the stage to redraw by simply adding the Lingo command "updateStage" as the last line of your script.

Now, let me give you a couple of other quick tips which will clean up your code and help you avoid some quirky pitfalls. I'll start with the repeat statement. In the script you sent, you have six lines which are virtually identical -- except for which sprite number they refer to. There's a really quick and easy way to accomplish the same task:


on mouseDown
  repeat with whichSprite = 1 to 6
    set the visible of sprite whichSprite ¬
      to FALSE
  end repeat
        
  updateStage
        
end

In this script, I'm setting a variable (which I called "whichSprite") to increment, starting with 1 and going up to 6. Each time the variable increments, the line(s) of code within the repeat statement are executed, but each time with a new value for the variable assigned. Easy, and extremely fast to execute. Fast because while the processor is working on a repeat loop, it shuts out all other processes. This is important to understand, because if you happen to create a very long repeat cycle... or one which has no logical end, then your program can lock up the system. For examples like this, though, a repeat loop is a big help.

I used to use "the visible of sprite" quite a lot in programs, until I discovered one of the pitfalls. You're not making the sprite itself invisible. In reality, you're setting the sprite channel to invisible. What's the distinction? When you set the sprite channel to invisible, you're not only affecting the sprite which is currently in that channel at that frame. You're essentially turning of ALL sprites in that channel throughout the whole movie. That means that you have to remember to turn the visible of that channel back ON so that other sprites are not affected. Anytime that my scripts start to rely on my memory, then I know it's not good code!

So, here's an option. Instead of making the sprite visible or invisible, set the locH (or locV) of the sprite so that it is moved onto or off of the stage. This has exactly the same appearance, but it doesn't affect the sprite channel -- and therefore doesn't screw up any sprites which appear in that channel later. The only tricky part is that you need to know the correct onstage locH of each sprite. I generally bring my sprites into my program from Photoshop files, using Photocaster with registration preserved. That means that, usually, the locH (horizontal regPoint) of all my sprites is centered on the stage (320 on a 640 x 480 stage). That makes it easy to make the sprites reappear at the correct spot.

Here's the same script we used before, but modified to use locH instead of visible:


on mouseDown
  repeat with whichSprite = 1 to 6
    set the locH of sprite whichSprite to 900
  end repeat
  updateStage
end

I used 900 as the offstage locH, but it can be any number which is sure to be offstage and allowing for the width of your sprite.

To make the sprites reappear (I'll assume it happens on mouseUp), you'll simply reverse the process. In this case, I'll assume that all of the sprites share the same onstage locH of 160.


on mouseUp
  repeat with whichSprite = 1 to 6
    set the locH of sprite whichSprite to 160
  end repeat
end

In this handler, you'll note that I didn't use the updateStage command. It's not necessary here, because once you've released the mouse, other events proceed normally. The play head will move to the next frame, automatically updating the stage.

I hope these tips have helped you with your program, as well as your general understanding of updateStage, repeat loops and visible.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.