4 Replies Latest reply on Jan 19, 2009 11:57 AM by augustusjoe

    swap sprite with if/then

    augustusjoe
      Hi,
      How would I write a script that would when sprite clicked would swap sprite then when clicked on again would swap back to original sprite.
      This is the script I am doing - and not working. I am obviously a beginner but learning.

      on mouseUp

      if the memberNum of (clickOn) = 3 then
      memberNum = 4

      If the memberNum of (clickOn) = 4 then
      memberNum = 3

      end

      Thanks for the help.

        • 1. Re: swap sprite with if/then
          Level 7
          Here is how I would do it


          --------------------------------------------------------
          property spriteNum, pOriginalMember, pSwapMember

          on beginSprite me
          pOriginalMember = sprite(spriteNum).member
          end

          on mouseUp me
          if sprite(spriteNum).member = pOriginalMember then
          sprite(spriteNum).member = pSwapMember
          else
          sprite(spriteNum).member = pOriginalMember
          end if
          end

          on getPropertyDescriptionList
          description = [:]
          addProp description,#pSwapMember, [#default:member(0),
          format:#member, #comment:"The other member"]
          return description
          end
          --------------------------------------------------------


          augustusjoe wrote:
          > Hi,
          > How would I write a script that would when sprite clicked would swap sprite
          > then when clicked on again would swap back to original sprite.
          > This is the script I am doing - and not working. I am obviously a beginner
          > but learning.
          >
          > on mouseUp
          >
          > if the memberNum of (clickOn) = 3 then
          > memberNum = 4
          >
          > If the memberNum of (clickOn) = 4 then
          > memberNum = 3
          >
          > end
          >
          > Thanks for the help.
          >
          >
          >
          • 2. Re: swap sprite with if/then
            Level 7

            "augustusjoe" <webforumsuser@macromedia.com> wrote in message
            news:gl28e8$d7g$1@forums.macromedia.com...
            > Hi,
            > How would I write a script that would when sprite clicked would
            > swap sprite
            > then when clicked on again would swap back to original sprite.
            > This is the script I am doing - and not working. I am obviously a
            > beginner
            > but learning.
            >
            > on mouseUp
            >
            > if the memberNum of (clickOn) = 3 then
            > memberNum = 4
            >
            > If the memberNum of (clickOn) = 4 then
            > memberNum = 3
            >
            > end
            >
            > Thanks for the help.
            >

            Hi there,
            first: do you want to learn how to do this, or do you jus want to have
            a working script?

            Second: there is a standard behaviour in the library for this if I
            remember well, its called "toggle button"

            Third: what does "does not work" mean here? Do you get en error
            message? Or is something happening that your dont expect (including
            "nothing" :) )?

            Richard


            • 3. Re: swap sprite with if/then
              Level 7
              I suppose I should offer a bit of explanation, so that you can learn
              from it. First of all to be more accurate, you are not swapping a
              sprite. You are swapping the sprite's member property.

              There is no reason to use the clickOn, as the mouseUp event is received
              by the sprite, and he already knows who he is (in a manner of speaking).
              Also you are using 2 if/then statements, where an if/then/else makes
              more sense.

              You will want to avoid hard coding of things like a membernum. Why? Well
              suppose that you need this same "toggling" behavior on 10 different
              sprites. With the hard coding, you will have to have 10 different
              scripts, one for each sprite. They will all be identical, with the
              exception of the membernum. So one would have 3 & 4, the next 5 & 6, 7 &
              8, etc.

              What happens if you decide to move things around in your cast? You just
              broke those 10 scripts. What happens if you decide that you want to add
              something, like a "beep" sound when the image changes. Well now you have
              to add that new code to 10 scripts.

              One behavior can handle the job, so there is no good reason to not to do
              it with one. The key is in the declaring of your own sprite properties.
              In the code, I defined a property called "pOriginalMember". Notice that
              this gets set during the beginSprite event. Thus I can attaqch this
              behavior to any number of sprites. Each will automatically remember the
              member that it started with.

              The other property is "pSwapMember". This will hold a reference to the
              member which is to be used for the toggle. The property is declared in
              the getPropertyDescriptionList handler. You will want to read up on that
              as it is quite useful. The property has to be set using Director's
              property inspector, under the behavior tab, double clicking the
              behavior. A dialog box will appear and let you set the value of
              pSwapMember for a particular instance.

              This is how one behavior can be utilized over and over. It will also not
              be broken by rearranging the cast. You can update the script if needed,
              without having to do so in multiple behaviors. Taking this approach will
              save you lots of time and effort over the long run.

              There is
              • 4. Re: swap sprite with if/then
                augustusjoe Level 1
                Hi,

                Thank you DaveC and LOOPING_Richard for all of your information and help. I am going to study, learn and apply the information you gave me today.

                Thank you again.
                Augustusjoe