A few questions about the language?

I know how to create objects like this:

local Balloon = display.newImage("balloon.png")

and I can do other stuff with it, but what I want to learn how to do is make a game where balloons fall to the ground, and when they do they pop and you loose points, and hear a sound. If you pop them BEFORE they touch the ground however, than they will give you points. How can I make it so that:

A. I hear the sound when I call upon the function that pops ballons, subtracting/adding points depending on the function/event

B. I change the balloons to have a image called “balloon_pop.png” appear for a split second and than dissapears inside the function that the balloon pops in.

C. Have a score displayed in the corner that can increase/decrease

D. (this is the last one!) make it so that when the balloon touches the object defined with this code:

local ground = display.newImage("floor.png")  
ground.y = display.contentHeight - ground.stageHeight/2  
physics.addBody(ground, "static", {bounce = 0.2})  

it calls on a function that pops the balloon.

Other than that I watched the “Physics in 5 minutes” video and learned how to do the rest, plus I’ve watched some basic tutorials.

Thanks for helping me out,
Nighthawk0973

Edit: forgot the /code tag in one of the code boxes, the whole message was code. Fixed it! [import]uid: 58008 topic_id: 9747 reply_id: 309747[/import]

hey Nighthawk,

A. – you can do this with touch and collision function

B. – you can create the pop effect in a function then
call that function during the pop event. In your case, you have
two potential pop events.

  1. the player pop the balloon by touching
    – this can be done by using touch function

  2. the balloon hits the ground
    – this can be done using collision function

C. – you can use text to display the score, I recommend checking out the
Ghosts vs. Monsters demo by Jonathan Beebe.

D. – refer back to B collision function
[import]uid: 12455 topic_id: 9747 reply_id: 35543[/import]

Anyways, here’s a little example

[lua]-- define the pop effect function
local popBalloon = function (x,y)
local pop = display.newImageRect(“whatever.png”, 50,50)
pop.x = x – you will feed the x to the x coord later
pop.y = y – you will feed the y to the y coord later
pop.alpha = 0

– use this to remove the pop effect
local removePop = function (obj)
obj:removeSelf()
end

if popTran then transition.cancel(popTran) end

local changeBack = function ()
– this transition will hide the pop effect this remove
local undoPop = transition.to(pop, {time = 500, alpha = 0, onComplete = removePop})
end

– this transition will show the pop effect
local popTran = transition.to(pop, {time = 500, alpha = 1, onComplete = changeBack})
end
– now define your collision function
– you want it to collide with the ground
– make sure you define the collision function before you define the balloon or it won’t work
local onCollide = function (self, event)
if event.phase == “began” then
– show pop effect by calling the pop function
popBalloon(self.x,self.y) – self.x,self.y is the coord of the balloon being pop
self:removeSelf() – now remove the balloon

– reduce score here
score = score - 1
end
end

– then put the balloon code down here
local balloon = display.newImageRect(“balloon.png”, 40,40)
balloon.x = 160
balloon.y = 40
balloon.name = “balloon”
physics.addBody(balloon, “dynamic”, {density = 0.5, bounce = 0.3, friction = 0.2})
balloon.collision = onCollide
balloon:addEventListener(“collision”, balloon)
– now define your touch function
local onTouch = function (event)
if event.phase == “began” then
if event.object.name == “balloon” then
– show pop effect
popBalloon(event.object.name.x,event.object.name.y)
self:removeSelf() – now remove the balloon

– add score
score = score + 1
end
end
end

balloon:addEventListener(“touch”, onTouch)[/lua]

I have not tested the code above, so i hope they do work.

check out my game:
Touch Cannon – a **FREE** game

please rate :slight_smile: [import]uid: 12455 topic_id: 9747 reply_id: 35545[/import]