A ball falls into a hole

Hello, all!

I’m a college student and a new member of the corona community. I only downloaded the SDK 2 weeks ago, but I’ve already been thrilled with the ease and strength of it. Props to the development team. During my time using the SDK, I’ve decided to make a game with a ball that bounces around a box, but I want to have a target to bounce the ball into. What I want to do is create a new circle the sits somewhere on the screen. When the ball enters the circle, I want to transition it to fade out. I know how to write the transition, and create the circle, but I’m having trouble triggering everything. I don’t have my code with me currently, but I was trying to use an onCollision method. Could someone show me what a correct version of that code might look like?

Thanks in advance! Looking forward to learning the ins and outs of Corona and creating some fun stuff.

-Zach [import]uid: 191020 topic_id: 32682 reply_id: 332682[/import]

Welcome to Corona,
I’m not sure if I fully understand here, do you want the ball to look as if it’s sitting in the box, or disappear when it enters the box while spewing a new ball some where on the screen? [import]uid: 114389 topic_id: 32682 reply_id: 129925[/import]

Thank you!

Sorry, that I didn’t make it clear. The player will drop a ball into the screen (or box), which will bounce off various structures, in the spirit of an old school pinball game. However, if it enters the black circle (the hole) it falls out of the map, and you lose a life. [import]uid: 191020 topic_id: 32682 reply_id: 129932[/import]

This sounds like you’d be using physics - have you looked at Corona For Newbies part 4? You’d likely have the hole be a sensor and on collision scale the ball down to make it look like it fell in. [import]uid: 52491 topic_id: 32682 reply_id: 129937[/import]

Welcome to Corona,
I’m not sure if I fully understand here, do you want the ball to look as if it’s sitting in the box, or disappear when it enters the box while spewing a new ball some where on the screen? [import]uid: 114389 topic_id: 32682 reply_id: 129925[/import]

I have been through quite a few of the tutorials, but I don’t think I am using them correctly. I tried looking up some examples, but a lot of the references here on the site, don’t actually have examples. I figured a forum was probably the better route anyway to get an answer tailored for the question.

Do you think you could should me some sample code? When I create my circle, I would make set its isSensor value to true, correct? But how do i write an onCollision method? Thanks again for your help. I’ll try to locate newbies 4 and give it a look. [import]uid: 191020 topic_id: 32682 reply_id: 129942[/import]

Thank you!

Sorry, that I didn’t make it clear. The player will drop a ball into the screen (or box), which will bounce off various structures, in the spirit of an old school pinball game. However, if it enters the black circle (the hole) it falls out of the map, and you lose a life. [import]uid: 191020 topic_id: 32682 reply_id: 129932[/import]

Ok, I got the file. Thank you.

I would still love if you could write a little bit of code for me to show me how my onCollision might work with a sensor. I won’t be able to mess with the tutorial until tomorrow. [import]uid: 191020 topic_id: 32682 reply_id: 129945[/import]

This sounds like you’d be using physics - have you looked at Corona For Newbies part 4? You’d likely have the hole be a sensor and on collision scale the ball down to make it look like it fell in. [import]uid: 52491 topic_id: 32682 reply_id: 129937[/import]

I have been through quite a few of the tutorials, but I don’t think I am using them correctly. I tried looking up some examples, but a lot of the references here on the site, don’t actually have examples. I figured a forum was probably the better route anyway to get an answer tailored for the question.

Do you think you could should me some sample code? When I create my circle, I would make set its isSensor value to true, correct? But how do i write an onCollision method? Thanks again for your help. I’ll try to locate newbies 4 and give it a look. [import]uid: 191020 topic_id: 32682 reply_id: 129942[/import]

Ok, I got the file. Thank you.

I would still love if you could write a little bit of code for me to show me how my onCollision might work with a sensor. I won’t be able to mess with the tutorial until tomorrow. [import]uid: 191020 topic_id: 32682 reply_id: 129945[/import]

You can also have the ball fade out, or transistion to something else, by having a timer check if the ball passes a pre-set location every 50 milisec. So if your hole is at y=300, you can do;

[lua]local function fade(event)

if (ball.y > 380) then
transition.to( ball, { time=300, alpha=0.0, } )
print(“fade out ball”)

end
end

local fadetimer
fadetimer = timer.performWithDelay(50, fader, 0)[/lua]

Somehting like that… [import]uid: 187595 topic_id: 32682 reply_id: 129975[/import]

You can also have the ball fade out, or transistion to something else, by having a timer check if the ball passes a pre-set location every 50 milisec. So if your hole is at y=300, you can do;

[lua]local function fade(event)

if (ball.y > 380) then
transition.to( ball, { time=300, alpha=0.0, } )
print(“fade out ball”)

end
end

local fadetimer
fadetimer = timer.performWithDelay(50, fader, 0)[/lua]

Somehting like that… [import]uid: 187595 topic_id: 32682 reply_id: 129975[/import]

We all have v limited time, so you wont always be able to get plug and play snippets - but try running this;

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 9.5 )

local hole = display.newCircle( 160, 450, 30 )
physics.addBody(hole, “static”, {radius=20})

local ball = display.newCircle( 160, 50, 20 )
ball.name = “ball”
ball:setFillColor(255,0,0)
physics.addBody(ball, {radius=20, isSensor=true})

local function sinkBall(event)
if event.phase == “began” and event.other.name == “ball” then
transition.to(ball, {time=1000, x=hole.x, y=hole.y, xScale=0.1, yScale=0.1, onComplete=function()ball:removeSelf()end})
end
end
hole:addEventListener(“collision”, sinkBall)[/lua] [import]uid: 52491 topic_id: 32682 reply_id: 130045[/import]

We all have v limited time, so you wont always be able to get plug and play snippets - but try running this;

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 9.5 )

local hole = display.newCircle( 160, 450, 30 )
physics.addBody(hole, “static”, {radius=20})

local ball = display.newCircle( 160, 50, 20 )
ball.name = “ball”
ball:setFillColor(255,0,0)
physics.addBody(ball, {radius=20, isSensor=true})

local function sinkBall(event)
if event.phase == “began” and event.other.name == “ball” then
transition.to(ball, {time=1000, x=hole.x, y=hole.y, xScale=0.1, yScale=0.1, onComplete=function()ball:removeSelf()end})
end
end
hole:addEventListener(“collision”, sinkBall)[/lua] [import]uid: 52491 topic_id: 32682 reply_id: 130045[/import]

I understand that everyone is very busy, perhaps you more than anyone here, so I really do appreciate it. I wasn’t meaning to be lazy by having someone else write my code, I just haven’t had luck running an onCollision type event, and I wanted to make sure I understood. Thanks for your time, everyone. The ideas and code are both immensely helpful. [import]uid: 191020 topic_id: 32682 reply_id: 130846[/import]

I do have another question. On line 9, you change the ball name. Is that just good form, or does any of the code hinge on that? For instance, if you changed the line to ball.name = “redBall”, would you have to edit any of the following code to reference redBall rather than just ball?

Is line 14 dependent on having a name for ball, or could you write this another way? Just curious. [import]uid: 191020 topic_id: 32682 reply_id: 130848[/import]

I understand that everyone is very busy, perhaps you more than anyone here, so I really do appreciate it. I wasn’t meaning to be lazy by having someone else write my code, I just haven’t had luck running an onCollision type event, and I wanted to make sure I understood. Thanks for your time, everyone. The ideas and code are both immensely helpful. [import]uid: 191020 topic_id: 32682 reply_id: 130846[/import]

I do have another question. On line 9, you change the ball name. Is that just good form, or does any of the code hinge on that? For instance, if you changed the line to ball.name = “redBall”, would you have to edit any of the following code to reference redBall rather than just ball?

Is line 14 dependent on having a name for ball, or could you write this another way? Just curious. [import]uid: 191020 topic_id: 32682 reply_id: 130848[/import]

Sorry to continuously bump this topic up. I’m now using the code that Peach so graciously provided (Thanks again), however, now that my ball is a sensor, it falls through the “floor” of my game. Is there a way to still have it interact with certain Rects, while remaining a sensor? Thanks again [import]uid: 191020 topic_id: 32682 reply_id: 130947[/import]