I am drawing shapes out of small brick sized particles, grouping then and adding a ‘move’ event handler for the group object. Works fine in that I can move the object around the drawing surface.
However if another object flies through a path that includes the original position of the group object, then my group object receives the collision in its new location. Interesting behaviour but in this case I was expecting to move the group out of ‘harms way’.
The reverse is true. If I created the object outside of the path and move it in, then no collision takes place. The colliding object appears to flies in ‘a layer’ over the group and no impact takes place.
Here’s the block that draws the group object:
[blockcode]
local function drawAsset(brickx,bricky,startx,starty,r,g,b)
local asset = display.newGroup()
for i=1,10 do
for j=1,10 do
local obj = display.newRect( startx+(j*(brickx)+j), starty+(i*(bricky)+i) , brickx, bricky )
obj:setFillColor(r,g,b)
physics.addBody(obj, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.2})
asset:insert(obj)
end
end
return asset
end
[/blockcode]
As you see i’m wiring the physics in at the ‘brick’ level allowing the group object to ‘explode’ when in harms way.
[blockcode]
asset = drawAsset(4,4,320,200,202,131,70)
asset:addEventListener( “touch”, makeOnTouchShapeEvent() )
[/blockcode]
Here’s the block that implements the event handler
[blockcode]
local function makeOnTouchShapeEvent()
return function ( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true
– Store initial position
t.x0 = event.xStart - t.x
t.y0 = event.yStart - t.y
elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
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
return true
end
end
[/blockcode]
Clearly I miss and understanding of what’s happening.
Help please. [import]uid: 114225 topic_id: 25948 reply_id: 325948[/import]
