How do I keep a dynamic object on a specific axis?

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
 

To be honest I think going the physics route, while giving you more instant results in terms of things not overlapping, collisions etc, will be more trouble than it’s worth in the long run. You’re fighting against a system which is designed to operate freely to give realistic interactions based on the forces applied, not direct user control.

However, in order to ‘force’ an object to stay on a particular axis, you could use an enterFrame listener. This runs every frame and will ‘correct’ any changes to position you don’t like. Whether or not this will result in graphical glitches if you are ‘pushing’ against an object that should not move, I’m not sure. In addition to the code below you should also check the objects exist (and the property you want to change exists) before doing anything in enterFrame.

  local runsEveryFrame = function ()     itemHorz3xBrickA.y = display.contentCenterY - 47    anotherItem.x = display.contentCenterX + 34       end     Runtime:addEventListener("enterFrame",runsEveryFrame)  

Hi Nick,
Thanks for replying.
I have scrapped the physics route, one of the reasons I did so, was that when my Key or brick did reach the edge, because of manual dragging, if I kept pushing the object, I could eventually break it out of the grid. It also shock/vibrated the image as the physics was fighting the players dragging interaction.
So maths/logic it ahs to be.

Ange