Looking for a Friendly Object Drag/Hold logic

hi there,

i have gone through Corona demo and it’s using following code to hold an object

local function onStackObjectDrag( event )  
 local t = event.target  
  
 local phase = event.phase  
 if "began" == phase then  
 display.getCurrentStage():setFocus( t )  
 t.isFocus = true  
  
 -- Store initial position  
 t.x0 = event.x - t.x  
 t.y0 = event.y - t.y  
  
  
 elseif t.isFocus then  
 if "moved" == phase then  
 t.x = event.x - t.x0  
 t.y = event.y - t.y0  
  
 elseif "ended" == phase or "cancelled" == phase then  
 display.getCurrentStage():setFocus( nil )  
 t.isFocus = false  
  
  
 end  
 end  
  
 -- Stop further propagation of touch event!  
 return true  
end  

it’s not hard to understand for and it work perfectly for physic object which is in RECTANGLE shape.
But when i tested with a more complex shape (image below)
http://imageshack.us/photo/my-images/80/screenshot20120125atam1.png/

it doesn’t select correctly when i drag on the star shape. Sooner i realized it’s because the umbrella is actually ‘cover’ on top of the star. So even though i am dragging on the star, the program actually thinking i am touching on the umbrella.

In such case, how can i enhance my code?

[import]uid: 10373 topic_id: 20897 reply_id: 320897[/import]

You need to give your physics body a custom shape, see here; http://developer.anscamobile.com/reference/index/physicsaddbody

Peach :slight_smile: [import]uid: 52491 topic_id: 20897 reply_id: 82422[/import]

hi Peach,

Any sample code? the custom shape is for physic and not sure how to implement to a complex shape (star, umbrella) [import]uid: 10373 topic_id: 20897 reply_id: 82435[/import]

Oh apologies I thought it was a physics game - I’m not sure why I jumped to that conclusion, my apologies.

You could use a mask for this purpose if not using physics :slight_smile:

Peach [import]uid: 52491 topic_id: 20897 reply_id: 82441[/import]

Hi peach, possible to provide a sample workable code?
Coz all i understand is, add event listener func is applied to an object, but things become complex for an image with complex shape.

Appreciate if someone can provide sample :slight_smile: [import]uid: 10373 topic_id: 20897 reply_id: 82475[/import]

Take a look here; http://developer.anscamobile.com/reference/index/objectishittestmasked

I believe that will help, there is some code to check out :slight_smile:

Peach [import]uid: 52491 topic_id: 20897 reply_id: 82642[/import]