This content has been marked as final. Show 4 replies
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
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.
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")
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.
> "#FF0000": -- red; hands
> sound(1) = member("handSound")
I think what you want in order to play the sound is syntax like this:
instead of separating it into 2 lines. Apart from that, I really like
the approach here. That makes it pretty easy to setup hotspots.