I need an add balloon button that adds more balloons with the same physics into my game, I have 3 balloons so far with no button. Is there anyway to add this + button to do this? [import]uid: 226740 topic_id: 35917 reply_id: 335917[/import]
Firstly, if you’re not already I’d suggest adding your balloons to a table so you can keep track of them.
Here’s how I’d probably do it:
[lua]
local balloons = {}
local buttons = {}
local touchButton = function (event) – function to handle touch events
local obj = event.target – localise the button pressed
if event.phase == “ended” then
if obj.id == 1 then – if button with ID=1 is pressed
– put your add balloons code here
–remember to add each one to the balloons table
end
end
end
local i = display.newImageRect(“button.png”, 60, 30) – load a button image, make it 60 pixels wide and 30 high
i.x = 40; i.y = 40 – place it in the top corner of the screen
i:addEventListener(“touch”,touchButton) – add the touch event listener
i.id = 1 – give it an ID - this way we can re-use the touch listener for multiple buttons
buttons[1]= i – add this button to the buttons table
localGroup:insert(i) – insert the button into your main display group
local t = display.newText("+", 0, 0, native.systemFont, 16) – create the ‘+’ text object
t.x = 40; t.y = 40 – position the text on the button
buttons[1].text = t – add the text as a child of the button
localGroup:insert(t) – insert the text into main display group
[/lua] [import]uid: 93133 topic_id: 35917 reply_id: 142750[/import]
Can you explain how I add my balloons to a table and in the space for my balloon code do I include my physics and gravity code for it as well? [import]uid: 226740 topic_id: 35917 reply_id: 142782[/import]
Can you post the code you have so far? [import]uid: 93133 topic_id: 35917 reply_id: 142791[/import]
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 4.9)
– physics.setDrawMode(“hybrid”)
local background = display.newImage(“background.jpg”)
local balloon = display.newImage(“red_balloon.png”)
balloon.x = display.contentWidth/2
physics.addBody(balloon, { bounce = 0.5, radius = 45, friction = 1.0} )
local balloon2 = display.newImage(“red_balloon.png”)
balloon2.x = balloon.x + 105
physics.addBody(balloon2, { bounce = 0.5, radius = 45, friction = 1.0} )
local balloon3 = display.newImage(“red_balloon.png”)
balloon3.x = balloon.x - 105
physics.addBody(balloon3, { bounce = 0.5, radius = 45, friction = 1.0} )
local leftWall = display.newRect(0, 0, 1, display.contentHeight )
local rightWall = display.newRect(display.contentWidth, 0, 1, display.contentHeight )
local ceiling = display.newRect(0, 0, display.contentWidth, 1 )
physics.addBody(leftWall, “static”, { bounce = 0.1} )
physics.addBody(rightWall, “static”, { bounce = 0.1} )
physics.addBody(ceiling, “static”, { bounce = 0.1} )
local floor = display.newImage(“floor.png”)
floor.y = display.contentHeight - floor.stageHeight/2
physics.addBody(floor, “static”, { bounce = 0.2, friction = 0.3})
display.setStatusBar( display.HiddenStatusBar )
local function moveBalloon(event)
local balloon = event.target
balloon:applyLinearImpulse( 0, -0.2, event.x, event.y )
end
balloon:addEventListener(“touch”, moveBalloon)
balloon2:addEventListener(“touch”, moveBalloon)
balloon3:addEventListener(“touch”, moveBalloon)
[import]uid: 226740 topic_id: 35917 reply_id: 142793[/import]
Firstly, if you’re not already I’d suggest adding your balloons to a table so you can keep track of them.
Here’s how I’d probably do it:
[lua]
local balloons = {}
local buttons = {}
local touchButton = function (event) – function to handle touch events
local obj = event.target – localise the button pressed
if event.phase == “ended” then
if obj.id == 1 then – if button with ID=1 is pressed
– put your add balloons code here
–remember to add each one to the balloons table
end
end
end
local i = display.newImageRect(“button.png”, 60, 30) – load a button image, make it 60 pixels wide and 30 high
i.x = 40; i.y = 40 – place it in the top corner of the screen
i:addEventListener(“touch”,touchButton) – add the touch event listener
i.id = 1 – give it an ID - this way we can re-use the touch listener for multiple buttons
buttons[1]= i – add this button to the buttons table
localGroup:insert(i) – insert the button into your main display group
local t = display.newText("+", 0, 0, native.systemFont, 16) – create the ‘+’ text object
t.x = 40; t.y = 40 – position the text on the button
buttons[1].text = t – add the text as a child of the button
localGroup:insert(t) – insert the text into main display group
[/lua] [import]uid: 93133 topic_id: 35917 reply_id: 142750[/import]
Can you explain how I add my balloons to a table and in the space for my balloon code do I include my physics and gravity code for it as well? [import]uid: 226740 topic_id: 35917 reply_id: 142782[/import]
Can you post the code you have so far? [import]uid: 93133 topic_id: 35917 reply_id: 142791[/import]
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 4.9)
– physics.setDrawMode(“hybrid”)
local background = display.newImage(“background.jpg”)
local balloon = display.newImage(“red_balloon.png”)
balloon.x = display.contentWidth/2
physics.addBody(balloon, { bounce = 0.5, radius = 45, friction = 1.0} )
local balloon2 = display.newImage(“red_balloon.png”)
balloon2.x = balloon.x + 105
physics.addBody(balloon2, { bounce = 0.5, radius = 45, friction = 1.0} )
local balloon3 = display.newImage(“red_balloon.png”)
balloon3.x = balloon.x - 105
physics.addBody(balloon3, { bounce = 0.5, radius = 45, friction = 1.0} )
local leftWall = display.newRect(0, 0, 1, display.contentHeight )
local rightWall = display.newRect(display.contentWidth, 0, 1, display.contentHeight )
local ceiling = display.newRect(0, 0, display.contentWidth, 1 )
physics.addBody(leftWall, “static”, { bounce = 0.1} )
physics.addBody(rightWall, “static”, { bounce = 0.1} )
physics.addBody(ceiling, “static”, { bounce = 0.1} )
local floor = display.newImage(“floor.png”)
floor.y = display.contentHeight - floor.stageHeight/2
physics.addBody(floor, “static”, { bounce = 0.2, friction = 0.3})
display.setStatusBar( display.HiddenStatusBar )
local function moveBalloon(event)
local balloon = event.target
balloon:applyLinearImpulse( 0, -0.2, event.x, event.y )
end
balloon:addEventListener(“touch”, moveBalloon)
balloon2:addEventListener(“touch”, moveBalloon)
balloon3:addEventListener(“touch”, moveBalloon)
[import]uid: 226740 topic_id: 35917 reply_id: 142793[/import]
Firstly, if you’re not already I’d suggest adding your balloons to a table so you can keep track of them.
Here’s how I’d probably do it:
[lua]
local balloons = {}
local buttons = {}
local touchButton = function (event) – function to handle touch events
local obj = event.target – localise the button pressed
if event.phase == “ended” then
if obj.id == 1 then – if button with ID=1 is pressed
– put your add balloons code here
–remember to add each one to the balloons table
end
end
end
local i = display.newImageRect(“button.png”, 60, 30) – load a button image, make it 60 pixels wide and 30 high
i.x = 40; i.y = 40 – place it in the top corner of the screen
i:addEventListener(“touch”,touchButton) – add the touch event listener
i.id = 1 – give it an ID - this way we can re-use the touch listener for multiple buttons
buttons[1]= i – add this button to the buttons table
localGroup:insert(i) – insert the button into your main display group
local t = display.newText("+", 0, 0, native.systemFont, 16) – create the ‘+’ text object
t.x = 40; t.y = 40 – position the text on the button
buttons[1].text = t – add the text as a child of the button
localGroup:insert(t) – insert the text into main display group
[/lua] [import]uid: 93133 topic_id: 35917 reply_id: 142750[/import]
Can you explain how I add my balloons to a table and in the space for my balloon code do I include my physics and gravity code for it as well? [import]uid: 226740 topic_id: 35917 reply_id: 142782[/import]
Can you post the code you have so far? [import]uid: 93133 topic_id: 35917 reply_id: 142791[/import]
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 4.9)
– physics.setDrawMode(“hybrid”)
local background = display.newImage(“background.jpg”)
local balloon = display.newImage(“red_balloon.png”)
balloon.x = display.contentWidth/2
physics.addBody(balloon, { bounce = 0.5, radius = 45, friction = 1.0} )
local balloon2 = display.newImage(“red_balloon.png”)
balloon2.x = balloon.x + 105
physics.addBody(balloon2, { bounce = 0.5, radius = 45, friction = 1.0} )
local balloon3 = display.newImage(“red_balloon.png”)
balloon3.x = balloon.x - 105
physics.addBody(balloon3, { bounce = 0.5, radius = 45, friction = 1.0} )
local leftWall = display.newRect(0, 0, 1, display.contentHeight )
local rightWall = display.newRect(display.contentWidth, 0, 1, display.contentHeight )
local ceiling = display.newRect(0, 0, display.contentWidth, 1 )
physics.addBody(leftWall, “static”, { bounce = 0.1} )
physics.addBody(rightWall, “static”, { bounce = 0.1} )
physics.addBody(ceiling, “static”, { bounce = 0.1} )
local floor = display.newImage(“floor.png”)
floor.y = display.contentHeight - floor.stageHeight/2
physics.addBody(floor, “static”, { bounce = 0.2, friction = 0.3})
display.setStatusBar( display.HiddenStatusBar )
local function moveBalloon(event)
local balloon = event.target
balloon:applyLinearImpulse( 0, -0.2, event.x, event.y )
end
balloon:addEventListener(“touch”, moveBalloon)
balloon2:addEventListener(“touch”, moveBalloon)
balloon3:addEventListener(“touch”, moveBalloon)
[import]uid: 226740 topic_id: 35917 reply_id: 142793[/import]
Firstly, if you’re not already I’d suggest adding your balloons to a table so you can keep track of them.
Here’s how I’d probably do it:
[lua]
local balloons = {}
local buttons = {}
local touchButton = function (event) – function to handle touch events
local obj = event.target – localise the button pressed
if event.phase == “ended” then
if obj.id == 1 then – if button with ID=1 is pressed
– put your add balloons code here
–remember to add each one to the balloons table
end
end
end
local i = display.newImageRect(“button.png”, 60, 30) – load a button image, make it 60 pixels wide and 30 high
i.x = 40; i.y = 40 – place it in the top corner of the screen
i:addEventListener(“touch”,touchButton) – add the touch event listener
i.id = 1 – give it an ID - this way we can re-use the touch listener for multiple buttons
buttons[1]= i – add this button to the buttons table
localGroup:insert(i) – insert the button into your main display group
local t = display.newText("+", 0, 0, native.systemFont, 16) – create the ‘+’ text object
t.x = 40; t.y = 40 – position the text on the button
buttons[1].text = t – add the text as a child of the button
localGroup:insert(t) – insert the text into main display group
[/lua] [import]uid: 93133 topic_id: 35917 reply_id: 142750[/import]
Can you explain how I add my balloons to a table and in the space for my balloon code do I include my physics and gravity code for it as well? [import]uid: 226740 topic_id: 35917 reply_id: 142782[/import]
Can you post the code you have so far? [import]uid: 93133 topic_id: 35917 reply_id: 142791[/import]
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 4.9)
– physics.setDrawMode(“hybrid”)
local background = display.newImage(“background.jpg”)
local balloon = display.newImage(“red_balloon.png”)
balloon.x = display.contentWidth/2
physics.addBody(balloon, { bounce = 0.5, radius = 45, friction = 1.0} )
local balloon2 = display.newImage(“red_balloon.png”)
balloon2.x = balloon.x + 105
physics.addBody(balloon2, { bounce = 0.5, radius = 45, friction = 1.0} )
local balloon3 = display.newImage(“red_balloon.png”)
balloon3.x = balloon.x - 105
physics.addBody(balloon3, { bounce = 0.5, radius = 45, friction = 1.0} )
local leftWall = display.newRect(0, 0, 1, display.contentHeight )
local rightWall = display.newRect(display.contentWidth, 0, 1, display.contentHeight )
local ceiling = display.newRect(0, 0, display.contentWidth, 1 )
physics.addBody(leftWall, “static”, { bounce = 0.1} )
physics.addBody(rightWall, “static”, { bounce = 0.1} )
physics.addBody(ceiling, “static”, { bounce = 0.1} )
local floor = display.newImage(“floor.png”)
floor.y = display.contentHeight - floor.stageHeight/2
physics.addBody(floor, “static”, { bounce = 0.2, friction = 0.3})
display.setStatusBar( display.HiddenStatusBar )
local function moveBalloon(event)
local balloon = event.target
balloon:applyLinearImpulse( 0, -0.2, event.x, event.y )
end
balloon:addEventListener(“touch”, moveBalloon)
balloon2:addEventListener(“touch”, moveBalloon)
balloon3:addEventListener(“touch”, moveBalloon)
[import]uid: 226740 topic_id: 35917 reply_id: 142793[/import]