how i can change object position after a collision?

i want to change the position of an object but i get and this error:
cannot translate an object before collision is resolved.
i try many times trying to resolved it but i can’t. i just want this to change the player object to a different position when hits the ground 3 times… help please

code i use
local collisionNum = 0
function restartposition(self,event)
collisionNum = collisionNum + 1
if(self.name == “ground” and event.other.name ==“player” and collisionNum ==3) then
player.x=40;
end
end

ground.collision = restartposition
ground:addEventListener(“collision”,ground) [import]uid: 138440 topic_id: 24683 reply_id: 324683[/import]

Please post your code in code brackets. Try something like this:

[code]
local function onCollideObject(event)
if ( event.phase == “began” ) then
if event.object1.myName == “ground” and event.object2.myName == “player” then
collisionNum = collisionNum + 1
if collisionNum == 3 then
player.x = 40
end
end
end
end

Runtime:addEventListener( “collision”, onCollideObject)
[/code] [import]uid: 77199 topic_id: 24683 reply_id: 100007[/import]

The original error is due to modifying an object during its collision calculations. You must use timer.performWithDelay(). Here is a small addition to the above provided code to handle it:

[code]
local function onCollideObject(event)
if ( event.phase == “began” ) then
if event.object1.myName == “ground” and event.object2.myName == “player” then
collisionNum = collisionNum + 1
if collisionNum == 3 then
timer.performWithDelay(1, function()
player.x = 40
end
)
end
end
end
end

Runtime:addEventListener( “collision”, onCollideObject)
[/code] [import]uid: 21331 topic_id: 24683 reply_id: 100067[/import]

I used the perform delay as you say and it works great but now i want to stop the player object because when i change the position it keeps the velocity, i try with physics.pause() but when i start it, the objects still with velocity, so how i can do that? any idea.

[lua]function restartposition(self,event)
collisionNum = collisionNum + 1
print (collisionNum)
if(self.name == “ground” and event.other.name ==“player” and collisionNum ==3) then
timer.performWithDelay(1, function ()
player.x=60; player.y = 600;
collisionNum = 0;

end
)
–leftlives()
end
end

ground.collision = restartposition
ground:addEventListener(“collision”,ground)
[lua] [import]uid: 138440 topic_id: 24683 reply_id: 100129[/import]

I’m not 100% sure on what your doing, but you can always set the bodyType in your collision handler:

player.bodyType = "static"  

see if that’s what you’re looking for :slight_smile: [import]uid: 21331 topic_id: 24683 reply_id: 100186[/import]

thanks it works :)… thanks for your time… and i have one issue again, do you know a way to display an arrow or something in the same direction, the object, like angry birds, i want to display something that permit to the user know at which direction is shoot,i use this code for the object

[lua]function shoot(event)
if (event.phase == “began”) then
display.getCurrentStage():setFocus(player)
–shotArrow.isVisible = true

elseif (event.phase == “ended”)then

player:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y)
display.getCurrentStage():setFocus(nil)

end

end
player:addEventListener (“touch”, shoot) [import]uid: 138440 topic_id: 24683 reply_id: 100218[/import]