Trying to create a soccer ball juggle game

Hello everyone, I am new to this AMAZING community and have been looking around to find my answer and this seems to be the place.

Theres actually a few things I’d like to learn how to do:

*Create a score function so when the user taps the ball they get a point but if the ball touches the ground the score resets

*Make an image I’ve created (Soccer ball) act like a soccer ball

*making a sound play (crowd cheering) every 20 points scored

I downloaded the source code from the 8 minute balloon game and have been messing with it and adding new things

Here’s what I have so far (Please don’t laugh this is my first time)

--\> Add physics engine, start up the engine, and apply gravity  
local physics = require("physics")   
physics.start()  
physics.setGravity(0, 4.9)   
  
system.activate( "multitouch" )   
  
-- physics.setDrawMode("hybrid")  
  
--\> Hide status bar  
display.setStatusBar( display.HiddenStatusBar )   
  
-- Create the bounds of the "arena"  
local background = display.newImage("soccersky.png")  
local floor = display.newRect(320, 0, 1, 480)  
local lWall = display.newRect(0, 480, 320, 1)  
local rWall = display.newRect(0, -1, 320, 1)  
local ceiling = display.newRect(-1, 0, 1, 480)  
  
staticMaterial = {density=2, friction=.3, bounce=.4}  
physics.addBody(floor, "static", staticMaterial)  
physics.addBody(lWall, "static", staticMaterial)  
physics.addBody(rWall, "static", staticMaterial)  
physics.addBody(ceiling, "static", staticMaterial)  
  
--\> Add balloons to stage and position  
local balloon = display.newImage("soccerball.png")  
balloon.x = display.contentWidth/2   
physics.addBody(balloon, {bounce = 0.4, friction = .2} )   
  
--\> Add floor image and position  
local floor = display.newImage("soccerfield.png")  
floor.y = display.contentHeight - floor.stageHeight/2  
physics.addBody(floor, "static", { friction = 1.0 })  
  
-- Define our touch event listener.  
function doImpulse(event)  
 balloon:applyLinearImpulse( 0, -0.2, event.x, event.y )   
end   
  
-- Add the listener to our balloon  
balloon:addEventListener("touch", doImpulse)  

The main problem I’ve noticed thus far is that the soccer ball still seems to act like a balloon and not a ball.
Any help/guidance would be greatly appreciated. Thank you for taking the time to read this.

-Andrew [import]uid: 72845 topic_id: 11839 reply_id: 311839[/import]

If you want it to act more like a ball, you might want to increase the gravity to something like (0, 12)

But before you do anything else, I suggest changing the names of all of the variables. INstead of ballon, make it just “ball” and for the doImpulse function, maybe rename it kickBall. It really doesn’t matter, but it makes it a bit easier to recognize what your code is doing.

To make a score feature, at the top of your code, add

local score = 0

then in the doImpulse function, add

score = score + 1  
if (score == 20) then  
--playsound  
end  

that means when the ball is touched, the ball is kicked up and the score increase by one.
For the code to play a sound, you can look in the api :slight_smile:
[import]uid: 7116 topic_id: 11839 reply_id: 43127[/import]

Hey fakemode,

Have you checked out http://Techority.com/ yet?

That’s my website - it’s pretty popular with newbies - and don’t worry, no one here will laugh at your code :slight_smile:

I’d suggest checking out my “Corona For Newbies” mini series; it has 4 parts and will help you understand how to do a lot of this.

Peach Pellen :slight_smile: [import]uid: 52491 topic_id: 11839 reply_id: 43217[/import]

Thank you both so much for the feedback but I’m still a little confused.

I tried to add

–at the top
local score = 0

and

score = score + 1 if (score == 20) then --playsound end

to the DoImpulse but when I do I just end up with a blank screen : (

And for the life of me I can’t figure out why my ball is still acting like a balloon. The code seems to be adding additional invisible dimensions to the ball image.

Here’s my newest code

local score = 0  
  
--\> Add physics engine, start up the engine, and apply gravity  
local physics = require("physics")   
physics.start()  
physics.setGravity(0, 12)   
  
system.activate( "multitouch" )   
  
-- physics.setDrawMode("hybrid")  
  
--\> Hide status bar  
display.setStatusBar( display.HiddenStatusBar )   
  
  
-- Create the bounds of the "arena"  
local background = display.newImage("soccersky.png")  
local floor = display.newRect(320, 0, 1, 480)  
local lWall = display.newRect(0, 480, 320, 1)  
local rWall = display.newRect(0, -1, 320, 1)  
local ceiling = display.newRect(-1, 0, 1, 480)  
  
staticMaterial = {density=2, friction=.3, bounce=.4}  
physics.addBody(floor, "static", staticMaterial)  
physics.addBody(lWall, "static", staticMaterial)  
physics.addBody(rWall, "static", staticMaterial)  
physics.addBody(ceiling, "static", staticMaterial)  
  
--\> Add balls to stage and position  
local ball = display.newImage("soccerball.png")  
ball.x = display.contentWidth/2   
physics.addBody(ball, {bounce = 0.4, friction = .2} )   
  
--\> Add floor image and position  
local floor = display.newImage("soccerfield.png")  
floor.y = display.contentHeight - floor.stageHeight/2  
physics.addBody(floor, "static", { friction = 1.0 })  
  
-- Define our touch event listener.  
function doImpulse(event)  
 ball:applyLinearImpulse( 0, -0.2, event.x, event.y )   
 score = score + 1  
if (score == 20) then  
--playsound  
 end   
-- Add the listener to our ball  
ball:addEventListener("touch", doImpulse)  
  

I’m guessing I didn’t add the score code in the right place which is why I’m getting a blank screen.

Thank you guys so much for taking the time to help me. It really does mean a lot to me. Hopefully once I have an understanding of all this I can help someone else out : )

[import]uid: 72845 topic_id: 11839 reply_id: 43235[/import]

hello fellow newbie) try this out, its not perfect, but its working the way you want
glad to help, wish to cooperate in future) we noobs need to stay together)))
if you need help, feel free to email portgas@mail.ru

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

score = 0

local ball = display.newCircle(0,0,15)
ball.x = display.contentWidth/2; ball.y = display.contentHeight/2
physics.addBody(ball, {density =1, friction =1, bounce = 0.3, radius = 15})
ball.name = “ball”

local floor = display.newRect(0,0,550,15)
floor.x = 10; floor.y = display.contentHeight
physics.addBody(floor, “static”, {density=1,friction=0.1,bounce=0})
floor.name = “floor”

local scoreTextfield = display.newText( "score: " … score, 40, 10, nil, 14 )
scoreTextfield:setTextColor(255,155,155)
ballFell = false

local function onCollision(e)
if(e.other.name == “floor”) and (e.phase == “ended”)then
print("!")
elseif(score > 0) then
score = 0
scoreTextfield.text = "score: " … score
end
end
local function kick_ball(e)
score = score + 1
scoreTextfield.text = "score: " … score
ball:applyLinearImpulse(0,30, ball.x, ball.y)
if(score == 10) then
media.playSound( “gamemusic.mp3”, true )
end
end

ball:addEventListener(“collision”, onCollision)
ball:addEventListener(“touch”, kick_ball)
[/code][/lua] [import]uid: 16142 topic_id: 11839 reply_id: 43267[/import]

You are AWESOME! this is almost exactly what I’m trying to make. I just need to tweak the UI a little bit with images I’ve created but this is VERY helpful. is their anyway to make it so the ball can move in any direction when you hit it but stay confined within the left and right walls?

Also when I first plugged in the code it wasn’t working. I added local physics to the first line and it worked out.

local physics = require("physics")

Don’t know if you had that problem or not, just a heads up.

Thanks a million,
Andrew [import]uid: 72845 topic_id: 11839 reply_id: 43275[/import]

i updated the code little bit, because its not behave properly on a device

[lua]local function onCollision(e) if(e.phase == "ended") then print("!") elseif(e.other.name == "floor") then if(score \> 0) then score = 0 scoreTextfield.text = "score: " .. score end end end [/lua]

for any direction you probably need to use some random digits with math.random or something like that
for staying inside the screen just setup some walls like you did for the floor

its better) [import]uid: 16142 topic_id: 11839 reply_id: 43276[/import]

You rock! [import]uid: 72845 topic_id: 11839 reply_id: 43280[/import]

For some reason when I click on the ball it bounces down instead of up. Any idea to make it bounce up when you click it? Also is their a way to control the velocity of the ball when you click it? (So when you click it the ball goes really high in the air or lower) [import]uid: 72845 topic_id: 11839 reply_id: 43298[/import]

problem is in
ball:applyLinearImpulse(0,30, ball.x, ball.y)
i think its not the thing you need, try different physics API’s [import]uid: 16142 topic_id: 11839 reply_id: 43369[/import]

@fakemode I had a similar issue, you need to negate the number you are applying as the force. See here http://stackoverflow.com/questions/6124909/corona-sdk-physics-apply-force-in-particular-direction/ [import]uid: 62042 topic_id: 11839 reply_id: 44760[/import]