Changing Sprites on the fly


[ Zettels Traum ] [ search / suche ]


You need what I call the 'card shuffle' script. Attach this script to each
sprite you want the user to click on and bring to the front:

on mouseDown
  global topSprite
  set thisOne = the member of sprite (the clickOn)
  set thisLoc = the loc of sprite (the clickOn)

  repeat with x = topSprite down to the clickOn
    set thatOne = the member of sprite x
    set thatLoc = the loc of sprite x
    set the member of sprite x = thisOne
    set the loc of sprite x = thisLoc
    set thisOne = thatOne
    set thisLoc = thatLoc
  end repeat

  set offsetH = the locH of sprite topSprite - the mouseH
  set offsetV = the locV of sprite topSprite - the mouseV
  repeat while the mouseDown
    set the locH of sprite topSprite = the mouseH + offsetH
    set the locV of sprite topSprite = the mouseV + offsetV
    updateStage
  end repeat
end

This script depends on the fact that sprites don't really exist. They are
just bundles of properties read (in effect) from a spreadsheet. Therefore
if you give all the properties of a sprite in (for example) channel 57 to a
sprite in channel 66 it will effectively "become" that sprite.

The script is in three parts. The first part sets up all the sprite
properties you want to shuffle. You can add or vary these.

The second part (the repeat loop) does the actual shuffling.

The third part (set offsetH...) is an extra bit that enables you to drag
big sprites like playing cards by the corner without them centring
themselves under the mouse. You can dump that if its not necessary (or use
it on its own elsewhere).

The TOPSPRITE global is there because I wanted to be able to change the
sprites that were being shuffled on the fly. If you don't need to do that
then its not necessary because you can hardwire the top channel, as in:

on mouseDown
  set thisOne = the member of sprite (the clickOn)
  set thisLoc = the loc of sprite (the clickOn)

  repeat with x = 23 down to the clickOn
  etc...

where 23 is the number of the highest channel containing a piece the user
can select and bring tothe front.

You definitely DON'T need any extra channels. In fact using an extra
channel is going to cause problems where none need exist!

Hope this is useful.

Cheers
Owen




Dazu:























D. Plänitz