Platform teleportation on collision

Hey all,
 
I’m currently making a game where a ball bounces on a small platform and when they collide, the platform will teleport to a random position and the ball must land on that platform again.
 
Here is the code:
 

local function platformCollision(self, event) if event.phase == "ended" then if event.target.type == "platform" and event.other.type == "ball" then timer.performWithDelay( 10, platMove() ) end end end local platform = display.newRect( 100, 100, 50, 5 ) physics.addBody( platform, "static" ) platform.type = "platform" platform.collision = platformCollision platform:addEventListener( "collision", platform ) function platMove() print( "ok" ) platform.x = math.random( 0, 300 ) platform.y = math.random( 50, 300 ) end local ball = display.newCircle( 100, 10, 10 ) ball.type = "ball" ball:setFillColor( 1,1,1 ) physics.addBody( ball, "dynamic", { bounce = 1 } )

When the ball and the platform collide: the console prints “ok” then prints “ERROR: Cannot translate an object before collision is resolved.” I checked for solutions online and mosts answers were about adding a timer.PerformWithDelay() line. I tried it but unfortunately with no positive results. I then tried postCollision instead of collision, the console didn’t print the error message but the platform did not move either. A third solution was to replace “ended” with “began” but obviously it didn’t work.
Please help
Thank you
Joe :slight_smile:

wait to move for one frame by using timer.performWithDelay()

Like this:

timer.performWithDelay(1, -- wait 1 ms, which really ends up being 1-frame function() -- code to move platform here end )

worked like a charm! you’re a life saver !  :smiley:

curious question though: why didn’t it work with 10 or 1000 ms ??

wait to move for one frame by using timer.performWithDelay()

Like this:

timer.performWithDelay(1, -- wait 1 ms, which really ends up being 1-frame function() -- code to move platform here end )

worked like a charm! you’re a life saver !  :smiley:

curious question though: why didn’t it work with 10 or 1000 ms ??