Hi all,
How do I prevent dynamic objects from being pushed off their X or Y axis.
EG I have a brick that is only allowed to be dragged by the player left/right (Y AXIS).
But if another dynamic object pushes it from the top or bottom it can push/move the brick higher or lower across the screen, how do I prevent this?
Any ideas?
Here is my code for my brick creation:
itemHorz3xBrickA = display.newImageRect( groupPuzzle, “images-ui/GP30PUZ/3xBrick_Vertical.png”, 47, 140 )
itemHorz3xBrickA.x = display.contentCenterX
itemHorz3xBrickA.y = display.contentCenterY - 47
itemHorz3xBrickA.myName = “Horizontal 3 x Brick A”
– itemHorz3xBrickA.fill = { 1, 0, 0 }
itemHorz3xBrickA:toFront()
itemHorz3xBrickA.rotation = 90
itemHorz3xBrickA.isVisible = true
physics.addBody( itemHorz3xBrickA, “dynamic”, { density=5.0 } )
itemHorz3xBrickA.isFixedRotation = true – locks against (prevents) rotation
This allows the left/right drag to take place.
local function dragHorz3xBrickA(event)
itemHorz3xBrickA = event.target
local phase = event.phase
if ( “began” == phase ) then
display.currentStage:setFocus( itemHorz3xBrickA )
itemHorz3xBrickA.touchOffsetX = event.x - itemHorz3xBrickA.x
elseif ( “moved” == phase ) then
itemHorz3xBrickA.x = event.x - itemHorz3xBrickA.touchOffsetX
elseif ( “ended” == phase or “cancelled” == phase ) then
display.currentStage:setFocus( nil )
end
return true – Prevents touch propagation to underlying objects
end
But it can currently be pushed (against it’s will) higher or lower on screen by other bricks.
I don’t want this.
In the example above, no matter what happens I need the brick shown to remain on the Y axis I set it on, regardless of how far left or right the player may drag it.
itemHorz3xBrickA.y = display.contentCenterY - 47
I’m totally stuck, I’ve tried so many things now, nothing has worked.
Thanks
Angela