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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
--> 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)

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

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

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 :)

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 :)

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<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)

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

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

1
2
3
4
5
6
7
8
9
10
11
12
<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

You rock!

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)

problem is in
ball:applyLinearImpulse(0,30, ball.x, ball.y)
i think its not the thing you need, try different physics API's

@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/

views:2457 update:2011/9/17 18:17:57
corona forums © 2003-2011