4 Replies Latest reply on Jul 1, 2010 1:44 AM by Jhon Carlo

    Increase the number of sprite buy clicking over a button

    Jhon Carlo Level 1

      Hallo I need an help about the following problem.
      I have several sprite out of the stage and a need to bring this sprite, one at time, inside the stage, without  write a long script like this:


      on mouseUp me
          If sprite(5).locH = 512 then
            sprite(6).locH = 512
      and so on ...


      I must use the script several time with several sprite, so I'm looking for another solution.
      Is there a way (more efficent)  to increase by one number of the sprite for every click over a button?
      For more information: I need of another button that decrease the number of sprite, but I supose if there is the solution of increase I can arrange the script.
      I've tried with my very little lingo knowlege, but from now I'm without solution.
      Thanks

       

       

        • 1. Re: Increase the number of sprite buy clicking over a button
          Production Monkey Level 3

          Generally when you want to share information across multiple objects you use a global variable. To keep the code clean I also suggest using movie level functions to handle bounds checking and any other details that might crop up.  A movie script would be:

           

          --  movie script

          global gCurSpriteNum, gMinSpriteNum


          on prepareMovie
            gMinSpriteNum = 10 -- use the starting sprite number minus 1.
            gCurSpriteNum = gMinSpriteNum
          end prepareMovie


          on getNextSprite
            gCurSpriteNum = gCurSpriteNum + 1
           
            if gCurSpriteNum > _movie.lastChannel then
              gCurSpriteNum = _movie.lastChannel
              put "getNextSprite() attempted to exceed maximum available sprite channels."
            end if
           
            return sprite(gCurSpriteNum)
          end getNextSprite


          on decrementNextSprite
            gCurSpriteNum = gCurSpriteNum - 1
           
            if gCurSpriteNum < gMinSpriteNum then gCurSpriteNum = gMinSpriteNum
          end decrementNextSprite

           

           

          You then create two behavior scripts that call the two functions:

           

          -- Increment behavior

           

          on mouseUp me
            NextSp = getNextSprite()
            put NextSp
          end

           

          And...

           

          -- decrement behavior

           

          on mouseUp me
            decrementNextSprite()
          end

          • 2. Re: Increase the number of sprite buy clicking over a button
            Jhon Carlo Level 1

            Thanks, Thank you  very much.
            I am completely surprised by my novice perspective of your  elegant solution.
            But you must be patient because for me it's still hard  to imagine the extensions of your script.
            How can I turn the  current script to another that inserts in a button the following  behavior?

            on mauseUp me
                 (Sprite 2). LochH = 512
            end

             

            that with the second  click will be

             

            mauseUp on me
                 (Sprite 3). LocH = 512
            end

            Or something that serves to this  purpose.

            Still Many, many thanks.

             

            • 3. Re: Increase the number of sprite buy clicking over a button
              Production Monkey Level 3

              In the Movie script you set the global variable "gMinSpriteNum" to "1"...

               

              on prepareMovie
                gMinSpriteNum = 1  -- use the starting sprite number minus 1.
                gCurSpriteNum =  gMinSpriteNum
              end prepareMovie

               

              Then you modify the increment behavior...

               

              --  Increment behavior

               

              on mouseUp me
                NextSp = getNextSprite()
                NextSp.locH = 512
              end

               


              If you want to be slick, then you can write it thus..

               

              --  Increment behavior

               

              on mouseUp  me
                getNextSprite()
              .locH = 512
              end

              • 4. Re: Increase the number of sprite buy clicking over a button
                Jhon Carlo Level 1

                Many thanks
                Your help is magic for me.

                Now  everything runs smoothly as slick and I'm more slick too.
                Really Really many thanks