Hi Corona developers,
I have many draggable objects, they are non-rectangular. What is the best way to touch and drag (hitTestPoint?) those non-rectangular objects. This is not physics based but if that is the only way let me know. What I did in Flash is rough-trace my transparent images and use that polygon as the hitArea. How would I do that in Corona?
Thanks!
Hi thanks very much. What do you mean by overlay area with an alpha property of 0.01? My sprite is transparent, a triangular shape, with transparency, the touch events go to the bounding box only, so it seems alpha has no effect on the hit area. Sorry I'm a newbie :)
your image is transparent, I understand, however the boundingbox for hitText is the rectangular area, not the non-transparent area.
Now if you were creating a boundingBox that was a primitive then you could be using a roundedrect, or a circle, etc which you can set the alpha to 0.01 this overlays onto your graphic and handles the touch events rather than your image.
You will understand this the more you play around with CoronaSDK.
cheers,
?:)
Ok but rectangles and circles are not an option in my case. So it seems like I won't be able to use polygons or image transparency for hit areas. Thanks!
try using bitmap mask.
http://developer.anscamobile.com/reference/bitmap-mask
I already tried that. Unless I used it wrong, it has no effect on the hit area, only effects the graphics.
it should work. I have tried it once and got it working
1 2 3 4 5 6 7 8 9 | local img=display.newRect(40,40,85,85) local mask = graphics.newMask("mask.png") img:setMask(mask) local function moveObject(event) event.target.x = event.x event.target.y =event.y end img:addEventListener("touch",moveObject) |
I still can't get it to work. I have the latest build and the simulator always crashes when I try to drag something that has a mask. Maybe this is a bug? Could you please download this and let me know what I'm doing wrong? Basically I want to drag the star only by its hitarea/mask, not its bounding box.
http://suriyathep.com/dl/hitsample.zip
The star and its mask are divisible by 4, not sure if thats needed anymore. Thanks!
Did you ever come up with a solution for this? I am running into a similar problem and hoping that bitmap mask will be the answer. Thanks!
Update: I got this working using the code that was provided a few comments earlier. Thanks guys! I am not dragging the object though. I can definitely see how buggy it is for that from my testing. All I had to do was check if the user was hitting a complex polygon hit area.
You are a Flash developer, so you can use math to determine if a point is in a polygon. You can still have an overlay area that has the alpha property of 0.01 to emit touch events.
cheers,
?:)