2 Replies Latest reply on Feb 3, 2009 12:41 PM by Newsgroup_User

    Referring to a sprite

    bmohnsen3
      I have a script that reads:
      on mousedown()
      sprite(14).moveablesprite = true
      end mousedown

      But I want this script attached to several sprites - what can I replace 14 with ---- "self" doesn't seem to work nor does "me"
        • 1. Re: Referring to a sprite
          Level 7
          property spriteNum

          on mousedown me
          sprite(spriteNum).moveablesprite = true
          end mousedown



          bmohnsen wrote:
          > I have a script that reads:
          > on mousedown()
          > sprite(14).moveablesprite = true
          > end mousedown
          >
          > But I want this script attached to several sprites - what can I replace 14
          > with ---- "self" doesn't seem to work nor does "me"
          >
          >
          • 2. Re: Referring to a sprite
            Level 7
            > property spriteNum
            >
            > on mousedown me
            > sprite(spriteNum).moveablesprite = true
            > end mousedown

            I often shortcut this process (especially when a sprite needs to reference
            itself many times in the script), using the following code (I've written
            these 4 lines of code so many times I should make them a macro on my
            keyboard or something):

            property my
            on beginSprite me
            my = sprite(me.spriteNum)
            end

            Then every time I want to access it, I use something like the following:

            on mouseDown me
            my.moveablesprite = true
            end

            Although, since we're going there, allow me to recommend an alternate
            method - I don't usually use the moveablesprite property because it has some
            annoying features I don't like very much, so I code it by hand, like so:

            property my,active
            on beginSprite me
            my = sprite(me.spriteNum)
            active = false
            end
            on mouseDown me
            active = true
            end
            on mouseUp me
            active = false
            end
            on exitFrame me
            if active then my.loc = the mouseLoc
            end

            This has the advantage that: A.) It snaps the center of the sprite to the
            mouse as soon as you click it, so you're not dragging it by the corner or
            edge. B.) You have more control over its movement - you can have it snap to
            a location, or return it to where it started when you let go, with just a
            few extra lines of code. You can also always tell if it's currently being
            moved or if the user has put it down. I've also noticed some minor bugs
            with the moveablesprite property where it doesn't go away when it should and
            carries over to sprites in other frames and such. All very annoying, and
            easily avoidable if you roll your own code.