How to re-spawn object?

I`m creating a simple app (actually my first app ever) which has an object (a ball) that you can throw by dragging and releasing . However sometimes it goes out of the screen and I want to know how I can make it sort of re-spawn (appear again) at its original position when that happens. Also, I don’t want any physical walls. [import]uid: 23613 topic_id: 5466 reply_id: 305466[/import]

Somebody please help, I tried to create a conditional function, but as I am new to Corona I don’t know how to do it correctly.

function ballRespawn()
if ball.y < 0 then
ball.x = 50 and ball.y = 200
end
end

Runtime:addEventListener(“enterFrame”, ballRespawn)

When I try to run it I only get a black screen. Please someone help.
[import]uid: 23613 topic_id: 5466 reply_id: 18403[/import]

I would say give this a shot and see if it works for what you need:
http://developer.anscamobile.com/sample-code/transition

RD [import]uid: 7856 topic_id: 5466 reply_id: 18411[/import]

the correct syntax for your code is

[lua]local function ballRespawn()
if ball.y < 0 then
ball.x = 50
ball.y = 200
end
end

Runtime:addEventListener(“enterFrame”, ballRespawn)[/lua]

personally i would call it something like checkBall() though, giving each function one job to do:
[lua]local function ballRespawn()
ball.x = 50
ball.y = 200
end

local function checkBall()
if ball.y < 0 then
ballRespawn()
end
end

local function onEnterFrame()
checkBall()
– otherStuff()
end

Runtime:addEventListener(“enterFrame”, onEnterFrame)[/lua]
[import]uid: 6645 topic_id: 5466 reply_id: 18416[/import]

@ jmp909

Wow! Thanks a lot, it really solved it (I can’t believe it was only a syntax problem), but this also brought me another problem. When the ball re-spawns it still keeps its speed and movement. How can I make it stop moving as soon as the ballRespawn function happens? Please help me,

Alex

I’m using this function to track its velocity after the drag is finished:

[lua]local speedX = 0
local speedY = 0
local prevTime = 0
local prevX = 0
local prevY = 0

function trackVelocity(event)
local timePassed = event.time - prevTime
prevTime = prevTime + timePassed

speedX = (ball.x - prevX)/(timePassed/500)
speedY = (ball.y - prevY)/(timePassed/500)

prevX = ball.x
prevY = ball.y
end

Runtime:addEventListener(“enterFrame”, trackVelocity) [import]uid: 23613 topic_id: 5466 reply_id: 18441[/import]

Somebody please help, how can I make the ball stop moving as soon as it appears again on the screen?

  • Alex [import]uid: 23613 topic_id: 5466 reply_id: 18672[/import]

set it to sleep, set some new properties (like linear velocity etc), wake it up again.

actually you probably dont need to sleep it first [import]uid: 6645 topic_id: 5466 reply_id: 18827[/import]