4 Replies Latest reply on Jan 5, 2007 8:19 PM by Newsgroup_User

    Assigning a behavior to a special part of an image 

    mohsena Level 1
      Hello all

      I have an image on the score that assigned to a sprite ,I want to assign a behavoir just to a special part of that image for examnple if this image is a pic of a human ,just by clicking on the hand of the pic ,a spcial behavior make it to play a sound and by clicking on his foot doesn't anything happen.
      Thank you very much for your attention Mohsena
        • 1. Re: Assigning a behavior to a special part of an image 
          the real POTMO Level 1
          you cant assign a bahaviour to just one part of a sprite.
          what you can do is something like hotspots.

          on mouseUp me
          -- get the mouseloc
          tLoc = the mouseLoc

          tHotspot = rect(0,0,10,20)
          if tLoc.inside(tHotspot) then
          end if

          • 2. Re: Assigning a behavior to a special part of an image
            Level 7
            You can also create hotspots with invisible blocks. You may want the
            image to change, though, like have the hand change color when the mouse
            is over it. In that case your image will look like one image, but will
            actually be made up of several different images. You can then simply
            attach a behavior to each one.
            • 3. Re: Assigning a behavior to a special part of an image 
              Chunick Level 3
              what you need to do is have two images. One image is the image of your human. The other image contains solid colours ("hotspots", if you like) of the areas that you want to be clickable. Then you use the mapStageToMember command to get the x,y coordinates from the human image and correlate that to the x,y coordinates on the "hotspot" image.

              Let's assume your human image is member("human") and it's in sprite channel 1 and your hotspot image is member("hotspot"). It's just going to sit in the cast and it should be the exact same dimensions as the human image so it'll map properly. Now, what we want is a behavior attached to the human sprite (sprite 1) that will do something when we click the human image with the mouse. The behavior might look something like this:

              -- behavior attached to sprite 1
              property sp, mem

              on beginSprite me
              -- set the sprite and member objects to variables
              -- so the code is smaller
              sp = sprite(me.spriteNum)
              mem = sp.member

              on mouseUp me
              -- get the x,y coords of where the mouse clicked on the member which
              -- will translate into the x,y pixel coordinates of the image
              memLoc = sp.mapStageToMember(the mouseLoc)
              -- get the pixel colour of the matching coordinates from the hotspot image
              -- and convert it to a hex string so we don't have to fool around with the colour type
              pixelColor = member("hotspot").getPixel(memLoc).hexString()
              -- choose the sound based on the colour of the pixel from the hotspot image
              -- that was clicked and mapped from the human image.
              case pixelColor of
              "#FF0000": -- red; hands
              sound(1) = member("handSound")
              "#00FF00": -- green; feet
              sound(1) = member("feetSound")
              end case

              you would have to make sure the hands in your "hotspot" image member were painted solid red and that the feet were painted solid green, in this example. Note that the code is partly untested so I'm not sure if the two lines for playing the sound will work, but everything else should be good.
              • 4. Re: Assigning a behavior to a special part of an image
                Level 7

                > "#FF0000": -- red; hands
                > sound(1) = member("handSound")
                > sound(1).play()

                I think what you want in order to play the sound is syntax like this:

                sound(1).play(member "handSound")

                instead of separating it into 2 lines. Apart from that, I really like
                the approach here. That makes it pretty easy to setup hotspots.