Physics/Transition ?

Once i saw a game that is kind of a endless runner with a box being the main character.

the box starts in the floor (make jumps, run, like any other of this style), but when touches, it glues on the top wall. If touch happen again, the box will go back to the floor.

How can i do that?

Only have the simple code:

local floor = display.newRect( display.contentWidth/2, 0, display.contentWidth, 40 ); physics.addBody( floor, "static"); local wallTop = display.newRect( display.contentWidth/2, display.contentHeight - 5, display.contentWidth, 40 ); physics.addBody( wallTop, "static"); local rect = display.newRect( display.contentCenterX, display.contentCenterY, 16, 16 ); rect.strokeWidth = 3 rect:setFillColor( 0.5 ) rect:setStrokeColor( 1, 0, 0 ); physics.addBody( rect, "dynamic", {bounce = 0, density = 1, friction = 0});

Do i use transition? what if the top wall is not always covering the entire top screen? and i have to know the exactly point to move the box?

Like random platform: 

[           ]   (blank)      [                 ]

If you’re using the physics engine you would want to use a touch joint to control the position of the box. You can then use a transition to move the position of the box. You would need a display group which is not a physics body. Move the display group around using your transition or whatever and update the touch joint attached to the box to the same location as the display group. Directly transitioning the physics body is not good because it will fight the physics engine and will break.

Alright, so i tried with no success. It got really weird haha

That’s what i tried:

local group = display.newGroup( ); local function touchListener() group.y = - (wallTop.y - 60); group.x = display.contentWidth/2; local touchJoint = physics.newJoint( "touch", rect, .5, 0); touchJoint:setTarget( group.x, group.y ); end Runtime:addEventListener( "touch", touchListener )

sometimes the box is boucing as if he had been held by a chain,

I suppose that’s not what you told me to do, right? what did i do wrong?

If you’re using the physics engine you would want to use a touch joint to control the position of the box. You can then use a transition to move the position of the box. You would need a display group which is not a physics body. Move the display group around using your transition or whatever and update the touch joint attached to the box to the same location as the display group. Directly transitioning the physics body is not good because it will fight the physics engine and will break.

Alright, so i tried with no success. It got really weird haha

That’s what i tried:

local group = display.newGroup( ); local function touchListener() group.y = - (wallTop.y - 60); group.x = display.contentWidth/2; local touchJoint = physics.newJoint( "touch", rect, .5, 0); touchJoint:setTarget( group.x, group.y ); end Runtime:addEventListener( "touch", touchListener )

sometimes the box is boucing as if he had been held by a chain,

I suppose that’s not what you told me to do, right? what did i do wrong?