2 Replies Latest reply on Apr 25, 2011 2:37 AM by AnnaP1983

    Turning random sprites invisible?




      I am trying to work out a script to fade five sprites, one at a time, in a random order, any suggestions? The sprites should stay faded until all others have been faded. I can turn the sprite invisible using the script below rather than fading, but not sure how to do this random on 5 different sprites?

      sprite(31).blend = 0






      Im a beginner so apologies if this is a silly question!!



        • 1. Re: Turning random sprites invisible?
          Production Monkey Level 3

          The basic idea behind fading a sprite is to change its blend over time using an "enterFrame" event. Controlling when this happens to a set of sprites is best done using a global control object.


          You will need to use one each of the three different script types - ie Parent, Behavior, and Movie.


          I wrote all the code for you. If you take the time to read through it and to step through it while it is running, you will be able to learn quite a lot.


          Copy and paste the following code into a Behavior script. You can then drag this script onto as many sprites that you want to fade out.



          --  Sprite FadeOut Behavior

          global  gFadeMaster

          property  pMe  -- this sprite
          property  pInc  -- amount to change blend with each "enterFrame" event. Negative float
          property  pCurBlend  -- current value to set the sprite blend to. float
          property  pFading  -- is the sprite currently fading?  true or false

          On GetBehaviorDescription me
            return "Sprite FadeOut."
          end GetBehaviorDescription

          On GetPropertyDescriptionList me
            Props = [:]
            Props[#pInc] = [#default:-1.5, #format:#symbol, #Range:[#min:0, #max:-10.0], #comment:"Blend Change each EnterFrame."]
            return Props
          end GetPropertyDescriptionList

          on beginSprite me
            pMe = sprite(me.spriteNum)  -- store reference to this sprite
            pFading = false  -- not fading yet
            gFadeMaster.registerSprite(pMe)  -- tell the "Fade Master" that this sprite should be faded.
          end beginSprite

          on mouseDown me
            gFadeMaster.mouseDown(pMe)  -- tell "Fade Master" that this sprite was clicked.  Used to start fading sequence.
          end mouseDown

          on setBlend me, Amount  -- used by "Fade Master" to set all the sprites back to 100% blend when sequence is done.
            pMe.blend = Amount
          end setBlend

          on startFadeOut me  -- begins a fade out for this sprite
            if pFading = true then exit  -- already fading so we leave the handler
            -- not currently fading so we begin the process by setting pFading = true
            pFading = true
            pCurBlend = pMe.blend  -- need to know the starting blend value
          end startFadeOut

          on enterFrame me
            if pFading = false then exit  -- not currently fading so leave the handler
            pCurBlend = pCurBlend + pInc  -- add blend increment
            pMe.blend = pCurBlend
            if pCurBlend <= 0 then
              pMe.blend = 0  -- set the blend to zero. setting the blend to a negtive value causes blend to reset to 100
              pFading = false -- stop fading this sprite
              gFadeMaster.doneFading(pMe)  -- tell the "Fade Master" that this sprite is done fading.
            end if
          end enterFrame




          Copy and paste the following code into a Parent script named "Fade Master".  This script controls which random sprite will fade next.




          -- Fade Master Parent Script

          property  pSpritesToFade  -- list of sprite to fade out
          property  pCurSpritesToFade  -- list of sprites that we currently need to fade out
          property  pFading  -- is a fade sequence currently happening. true or false

          on new me
            -- setup default values
            pSpritesToFade = []
            pCurSpritesToFade = []
            pFading = false
            return me
          end new

          on registerSprite me, aSprite
            pSpritesToFade.add(aSprite)    -- add "aSprite" to list of sprites to fade
          end registerSprite

          on mouseDown me, aSprite  -- called when a fading sprite is clicked
            if pFading = true then exit  -- already running a fading sequence. exit handler
            pFading = true  -- begin new fading sequence
            pCurSpritesToFade = me.getRandomList(pSpritesToFade)  -- grab a fresh randomized list of sprites to fade out
            pCurSpritesToFade[1].startFadeOut()   -- tell the first sprite in the list to begin fading out
          end mouseDown

          on doneFading me, aSprite  -- when a sprite is done fading it calls this handler
            pCurSpritesToFade.deleteOne(aSprite)  -- delete the sprite from the list that just completed its fade out
            -- check if there are any more sprites to fade
            if pCurSpritesToFade.count then
              pCurSpritesToFade[1].startFadeOut()  -- tell the first sprite in the list to begin fading out
            else  -- done fading sequence
              pFading = false
              call(#setBlend, pSpritesToFade, 100)  -- set all of the sprites back to 100 blend by calling their "setBlend" handler.
            end if
          end doneFading

          on getRandomList me, aList  -- returns a randomized list without affecting "alist".
            TmpList = aList.duplicate()
            RandomList = []
            -- pull random values from TmpList. Delete these values after pulling them
            repeat while TmpList.count
              RndIndex = random(1, TmpList.count)
            end repeat
            return RandomList
          end getRandomList





          We need a short Movie script to create a global instance of the "Fade Master" script. Paste the following into a Movie script.


          global  gFadeMaster

          on prepareMovie
            gFadeMaster = script("Fade Master").new()  -- create instance of the "Fade Master" parent script.



          At this point you are ready to go other than you will need a "go the frame" script to loop on frame. Click any fading sprite to start the sequence. Once all the sprites have faded out they are reset to blend=100 and you can click a sprite again. This time the sequence should be different.


          I wrote lots of comments in the scripts to help explain how the scripts work. If you have questions ask.

          • 2. Re: Turning random sprites invisible?
            AnnaP1983 Level 1

            Wow thats really helpful, thanks so much!