3 Replies Latest reply on Nov 6, 2008 6:10 AM by rafa@mediatech

    How can I affect multiple sprites?

    rafa@mediatech Level 1
      Hi:
      I am trying to affect multiple sprites when I roll over a button. Basically it says:

      on mouseEnter me
      sprite(3).visibility = 1
      sprite(4).visibility = 1
      sprite(5).visibility = 1
      end
      on mouseLeave me
      sprite(3).visibility = 0
      sprite(4).visibility = 0
      sprite(5).visibility = 0
      end
      How can I tell it to affect the 3 of them in one line? I tried:

      on mouseEnter me
      sprite(3,4,5).visibility = 1
      end

      but this didn't work.
      It is not an issue when its is only a few sprites...but I might need to affect 10-15.
      Any help would be great...
      Thanks!
      Rafael.
        • 1. Re: How can I affect multiple sprites?
          Level 7
          You could use a repeat loop where you make a list of the sprite numbers
          then go through that list

          on mouseEnter me
          repeat with i in [3,4,5]
          sprite(i).visibility = 1
          end repeat
          end


          OR


          on mouseEnter me
          mySprites=[3,4,5]
          repeat with i in mySprites
          sprite(i).visibility = 1
          end repeat
          end

          That would be how to affect them directoly. You may not want to use a
          lot of tight repeat loops if there are large amounts of these, since
          that will tend to lock up Director until the repeat loop is finished.
          An easier way is to use sendAllSprites.

          In the script that is on the sprites that you want to control the
          visibility of, add a custom handler like this:

          on setVisible me, vVis
          sprite(me.spriteNum).visible=vVis
          end

          Then, on your button, you would have code like this:

          on mouseEnter me
          sendAllSprites(#setVisible,1)
          end

          on mouseLeave me
          sendAllSprites(#setVisible,0)
          end

          Now, when the mouse is over (or leaves) that sprite, any sprite which
          has that custom handler will turn its visibility on or off
          • 2. Re: How can I affect multiple sprites?
            Production Monkey Level 3
            Rafael,

            Please never write code like "sprite(3).visibility = 1" ever again. You are having a button sprite change a property of some other sprite. That is a big no no. Sprites should change their own properties. If some piece of code wants a sprite to change a property then it should call a handler on that sprite - as Mike demonstrates with "setVisible()".

            It may seem like more work up front, but following this rule will save you countless hours trying to debug what will appear to be unexplainable bugs.

            Mike provided a good answer. I will only add that "visible" is a channel property. Its setting persists after the sprite has long gone. It is a good practice to set it back to true on end sprite, otherwise you will find sprites inexplicably disappearing in other parts of your program.

            on endSprite me
            sprite(me.spriteNum).visible = true
            end
            • 3. Re: How can I affect multiple sprites?
              rafa@mediatech Level 1
              Mike:
              As always...thank you. I used "on setVisible me, vVis" like you suggested. It works great, and I can keep using the same code for the other buttons. Before I was having to create a custom behavior for each button (i hate that!). I am using this for a drop down menu effect (I should've mentioned that). I know I could do it dynamically in Director using lists, but I am having to create a custom look for the actual drop down. Anyway, this works great, thanks for your help.

              Production Monkey:
              LOL!! Sorry! I tried it that way (sprite(3).visibility = 1) and it worked! So I didn't see a reason why not to use it. I promise, I will not do it again. Thanks for calling out my improper director behavior...

              Thanks guys!
              Rafael.