gameUi.dragBody() bug

Not sure if I should post it here or in bugs section. Also, I was searching for same problems on forum and could not find it.

gameUi.dragBody function has a bug that can be observed easily in multipuck example. Start the multipack example, and make two pucks on table, move them so that they are exactly next to each other. Now click exactly between them (or try to drag them by clicking exactly between them). One of the pucks will be stuck on the table, and it can't be moved any more.

Reason for this bug is that "touch" event is sent to both pucks. Returning true in dragBody() function does not stop propagation of this event to another puck.

[I am not really sure if this is bug or a feature of event propagation mechanism. I guess it is feature, since I think returning true in handler only prevents event propagating to parent nodes in a tree, and since pucks are just siblings I guess this is expected behavior. Although it would be very nice if there is a way to stop event propagation to any other object. I hope someone can confirm if this is expected behavior of event propagation.]

So our dragBody() function is called for both pucks. Two joints will be created (one for each puck), but only the one puck will get the focus (the last one that received the event). Because only one puck has the focus, only that puck will receive the event when touch is over. So, only one physics joint gets removed, while the other puck is stuck on table forever, since its joint is never removed.

The best fix for this bug would be if we are able to stop propagation of events to any other objects. If this is not possible, another quick but ugly fix is to put global boolean variable in gameUi file, something like:

local isFocusSet = false

and to check this variable during "began" event phase in dragBody(). If it is set, then just return from dragBody(), since it means we are already dragging another object. If it is not set, then we add new joint, and set isFocusSet to true. Also, at the "ended" event phase we must reset isFocusSet to false.

In the case of the Air Hockey 2 Template, the pucks are quarantined to an upper and lower half of the table and MUST move concurrently. In this case, the gameUI.lua dragBody() function works great.

I'm working on a game with multiple pucks and rather than creating a Touch Joint temporarily, I make the touch Joint permanently associated with the object. This approach solved a number of problems for me. I can change focus between objects as needed and maintain position settings.

Probably lots of was to approach a solution.

-David

I don't see air hockey code in my sample code directory :) I only have multipuck code at ~\Ansca\Corona SDK\Sample Code\Physics\MultiPuck

The gameUI code used in that example has above mentioned bug :)

The point with the Air Hockey 2 examples was everyone has different intended purposes, and the focus issues you pointed out is not a bug since it was created for it's intended purpose. Your usage is a variance from the original intended needs.

Good luck with your project.

-David

Ok, I get your point now. But what about event propagation? Do you know if touch events should propagate to another sibling objects if we return true in handler? Corona docs says that returning true prevents propagating to another objects, but in my case it seems it only prevents propagating to parent objects :)

Thanks in advance.

views:1586 update:2011/11/8 8:33:25
corona forums © 2003-2011