This should be easy but I am missing it. I am dropping a ball from the top of the screen, when it disappears off the bottom I want it to reappear at the top and drop from the top again. I want the ball to be in a continious loop. I am just not sure how to handle a physics controlled image like the ball. [import]uid: 23256 topic_id: 22715 reply_id: 322715[/import]
Try running this code
[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 9 )
local ball = display.newCircle( 160, 0, 30 )
physics.addBody(ball)
local catch = display.newRect( 0, 480, 320, 20 )
physics.addBody(catch, “static”, {isSensor = true})
local function catchCollision (event)
if event.phase == “ended” then
timer.performWithDelay(100, function() ball.y = 0 ball:setLinearVelocity(0,0) end, 1)
end
end
ball:addEventListener(“collision”, catchCollision)[/lua] [import]uid: 52491 topic_id: 22715 reply_id: 90659[/import]
if you do not have a floor then this is another way you can do it.
[lua]local physics = require (“physics”)
physics.start ()
local ball = display.newCircle (0, 0, 50, 50)
ball.x = display.contentWidth/2; ball.y = -50;
–Function to check the balls location
local function checkPos ()
if ball.y > 370 then
ball.y = -50
ball:setLinearVelocity(0,0)
end
end
–Add a runtime listener or a timer
Runtime: addEventListener (“enterFrame”, checkPos)
–or timer.performWithDelay ( 1, checkPos, -1)[/lua]
hope this helps [import]uid: 126161 topic_id: 22715 reply_id: 90698[/import]
We have success! I can actually use both of those methods depending how I set this thing up. Thanks.
I am teaching Corona to a high school Programming II class. Not knowing Corona very well adds to the excitement of teaching the class. This forum is a god-send to the programmingly challenged Corona CS teacher. [import]uid: 23256 topic_id: 22715 reply_id: 90753[/import]
Glad to hear it!
I love hearing about Corona classes - I wish they existed when I was in high school [import]uid: 52491 topic_id: 22715 reply_id: 90864[/import]
Peach,
If you have time I could use a couple of things clarified. This is just a learning exercise for me so I can explain it to the kids.
I am tiptoeing through your code sample to try and figure out all the things I do not understand and combining it with what I want to do. I am having the ball hit a floor (floor is a BAD choice for a variable name, took a while to remember that is a reserved function name) and then go back to the top of the screen. I am just modifying your code to fit this scenario. I can get everything working just fine; I am just not sure why it works. What has me going are lines 12 thru 14. I understand a collision sensor and begin and end phase (sort of). Why is the timer necessary? I have been playing with the code and things definitely do not work without it but why? Is a delay required to get the ball to bounce off the surface before the ball position can be reset? Without the ball bouncing away from the floor surface I get an “ERROR: Cannot translate an object before collision is resolved”. Is this because the ball and the floor are still in contact?
Is there a way of having the ball reset on immediate contact?
The function() inside line 13 was a real puzzler until I realized that it just a way of calling the function the syntax required there.
Making things work is not a big problem (sometimes), it is being able to explain why things work that is tricky.
1 require ( “physics” )
2 physics.start()
3 physics.setGravity( 0, 9 )
4
5 local ball = display.newCircle( 160, 0, 30 )
6 physics.addBody(ball)
7
8 local catch = display.newRect( 0, 480, 320, 20 )
9 physics.addBody(catch, “static”, {isSensor = true})
10
11 local function catchCollision (event)
12 if event.phase == “ended” then
13 timer.performWithDelay(100, function() ball.y = 0 ball:setLinearVelocity(0,0) end, 1)
14 end
15 end
16 ball:addEventListener(“collision”, catchCollision)
[import]uid: 23256 topic_id: 22715 reply_id: 91314[/import]
With collisions, the solution is to “wait a bit”.
In my game, I have a goal and a player who reaches it, before it would work sometimes and crash other times. Now I have a delay of about 300 (if I go any lower it gives same error).
Also, when posting code use the code block (look at bottom of screen where it says syntax highlighting)
Sample code, notice
there
is
no
line
numbers INSIDE the codee block?
...and you can copy the code to a txt editor, and the line numbers
don't follow :) == WINNNNN!
Try delaying the after collision stuff with a high number like 1000, then start trimming it down and see when it breaks. Then pad it with slightly higher number which will give you stability on device builds.
That is my experience with it, I’m sure others have a different experience as well, this is life after all!
ng
**EDIT*
Ok I grabbed your code and in the simulatar I am not getting any errors on build 760 (what build are you on?) and using a value 10 for delay
require ( "physics" )
physics.start()
physics.setGravity( 0, 9 )
local ball = display.newCircle( 160, 0, 30 )
physics.addBody(ball)
local catch = display.newRect( 0, 480, 320, 20 )
physics.addBody(catch, "static", {isSensor = true})
local function catchCollision (event)
if event.phase == "ended" then
print ("function:CatchCollision - began!!")
timer.performWithDelay(10, function() ball.y = 0 ball:setLinearVelocity(0,0) end, 1)
print ("function:CatchCollision - ENDED!!")
end
end
ball:addEventListener("collision", catchCollision)
I also put print statements so I could see before and after (I LOVE print statements to test functions first, before I get all nutty with it).
I tried the above on an earlier build and did get the error (but not always).
Also, more information about sensors is found here:
http://techority.com/2011/08/31/lets-talk-about-sensors/
It’s by PEACH PELLEN of the epic world FTW.
ng
[import]uid: 61600 topic_id: 22715 reply_id: 91315[/import]