i want to destroy the balls with touch !!

i want to destroy the balls when i touch… this is my code (i am very noob)[code]
– add physics engine, start up the engine, and apply gravity
local physics = require(“physics”)
physics.start()
– Set gravity to act “down” (ie, toward the bottom of the device )
physics.setGravity (0, 9)
–set background image
local background = display.newImage( “background.jpg” )
–set ball to stage and position
local function spawnBall()
local ball =display.newImage(“bead2.png”)
ball.x = math.random ( 320 )
ball.y = -100
– turn ball into physics body
physics.addBody(ball,{bounce = 0.1 } )
end
timer.performWithDelay( 500,spawnBall,50)

–add floor image and position
local floor = display.newImage(“bar.png”)
floor.y = display.contentHeight/1.03
– turn floor into physics body
physics.addBody(floor,“static”) [import]uid: 179948 topic_id: 31552 reply_id: 331552[/import]

I’d definitely check out Dr. Rafael Hernandez’s video tutorials. It sounds like his "Orb Smasher game is similar to what you’re trying to do. Here it is:

http://www.youtube.com/watch?v=6TsDdLY7VXk

He makes the function to destroy his orbs around 8 minutes in. I’d recommend watching the whole thing though.

Hope this helps!

Brandon [import]uid: 136211 topic_id: 31552 reply_id: 126052[/import]

Do you want to destroy the balls when you touch the screen?

If so you’d need to add event listeners to each ball when they are created.

http://docs.coronalabs.com/api/type/EventListener/addEventListener.html [import]uid: 163581 topic_id: 31552 reply_id: 126057[/import]

i try to do it but didn’t work ,thank very much brandon
this is my code (sorry for the bad english i am from venezuela and i have 13 years old)
if you can tell me what is wrong i appreciate very very much

[code]-- add physics engine, start up the engine, and apply gravity
local physics = require(“physics”)
physics.start()
– Set gravity to act “down” (ie, toward the bottom of the device )
physics.setGravity (0, 9)
–set background image
local background = display.newImage( “background.jpg” )

–add floor image and position
local floor = display.newImage(“bar.png”)
floor.y = display.contentHeight/1.03
– turn floor into physics body
physics.addBody(floor,“static”)

local function trackBalls (obj)
obj:removeSelf():
end

–set ball to stage and position
local function spawnBall()
local ball =display.newImage(“bead2.png”)
ball.x = math.random ( 320 )
ball.y = -100

– turn ball into physics body
physics.addBody(ball,{bounce = 0.1 } )
end

function ball:touch(e)
if(e.phase == “ended”) then
–remove the balls
trackBalls(self);
end

return true;

end

ball:addEventListener(“touch”, ball) ;

end

timer.performWithDelay( 500,spawnBall,50)
[import]uid: 179948 topic_id: 31552 reply_id: 126067[/import]

Hey, give this a shot…

  
-- add physics engine, start up the engine, and apply gravity  
local physics = require("physics")  
physics.start()  
-- Set gravity to act "down" (ie, toward the bottom of the device )  
physics.setGravity (0, 9)  
--set background image  
local background = display.newImage( "background.jpg" )  
   
--add floor image and position  
local floor = display.newImage("bar.png")   
floor.y = display.contentHeight/1.03  
 -- turn floor into physics body  
physics.addBody(floor,"static")  
   
local function trackBalls (obj)  
 obj:removeSelf()  
end  
   
--set ball to stage and position  
local function spawnBall()  
local ball =display.newImage("bead2.png")  
ball.x = math.random ( 320 )  
ball.y = -100  
   
-- turn ball into physics body  
physics.addBody(ball,{bounce = 0.1 } )  
  
 function ball:touch(e)  
 if(e.phase == "ended") then  
 --remove the balls   
 trackBalls(self);  
 end  
  
 return true;  
  
 end  
  
 ball:addEventListener("touch", ball) ;  
  
end   
  
timer.performWithDelay( 500,spawnBall,50)  
   

You had a " : " after obj:removeSelf() and an extra “end” in the middle of your spawnBall() function. It should be good to go now :slight_smile:

Brandon [import]uid: 136211 topic_id: 31552 reply_id: 126069[/import]

Yeah - I’d agree with Brandon, code looks perfect apart from that.

Good luck with it! [import]uid: 163581 topic_id: 31552 reply_id: 126094[/import]

thanks very very much… it works brandon !!! thanks [import]uid: 179948 topic_id: 31552 reply_id: 126101[/import]

I’d definitely check out Dr. Rafael Hernandez’s video tutorials. It sounds like his "Orb Smasher game is similar to what you’re trying to do. Here it is:

http://www.youtube.com/watch?v=6TsDdLY7VXk

He makes the function to destroy his orbs around 8 minutes in. I’d recommend watching the whole thing though.

Hope this helps!

Brandon [import]uid: 136211 topic_id: 31552 reply_id: 126052[/import]

Do you want to destroy the balls when you touch the screen?

If so you’d need to add event listeners to each ball when they are created.

http://docs.coronalabs.com/api/type/EventListener/addEventListener.html [import]uid: 163581 topic_id: 31552 reply_id: 126057[/import]

i try to do it but didn’t work ,thank very much brandon
this is my code (sorry for the bad english i am from venezuela and i have 13 years old)
if you can tell me what is wrong i appreciate very very much

[code]-- add physics engine, start up the engine, and apply gravity
local physics = require(“physics”)
physics.start()
– Set gravity to act “down” (ie, toward the bottom of the device )
physics.setGravity (0, 9)
–set background image
local background = display.newImage( “background.jpg” )

–add floor image and position
local floor = display.newImage(“bar.png”)
floor.y = display.contentHeight/1.03
– turn floor into physics body
physics.addBody(floor,“static”)

local function trackBalls (obj)
obj:removeSelf():
end

–set ball to stage and position
local function spawnBall()
local ball =display.newImage(“bead2.png”)
ball.x = math.random ( 320 )
ball.y = -100

– turn ball into physics body
physics.addBody(ball,{bounce = 0.1 } )
end

function ball:touch(e)
if(e.phase == “ended”) then
–remove the balls
trackBalls(self);
end

return true;

end

ball:addEventListener(“touch”, ball) ;

end

timer.performWithDelay( 500,spawnBall,50)
[import]uid: 179948 topic_id: 31552 reply_id: 126067[/import]

Hey, give this a shot…

  
-- add physics engine, start up the engine, and apply gravity  
local physics = require("physics")  
physics.start()  
-- Set gravity to act "down" (ie, toward the bottom of the device )  
physics.setGravity (0, 9)  
--set background image  
local background = display.newImage( "background.jpg" )  
   
--add floor image and position  
local floor = display.newImage("bar.png")   
floor.y = display.contentHeight/1.03  
 -- turn floor into physics body  
physics.addBody(floor,"static")  
   
local function trackBalls (obj)  
 obj:removeSelf()  
end  
   
--set ball to stage and position  
local function spawnBall()  
local ball =display.newImage("bead2.png")  
ball.x = math.random ( 320 )  
ball.y = -100  
   
-- turn ball into physics body  
physics.addBody(ball,{bounce = 0.1 } )  
  
 function ball:touch(e)  
 if(e.phase == "ended") then  
 --remove the balls   
 trackBalls(self);  
 end  
  
 return true;  
  
 end  
  
 ball:addEventListener("touch", ball) ;  
  
end   
  
timer.performWithDelay( 500,spawnBall,50)  
   

You had a " : " after obj:removeSelf() and an extra “end” in the middle of your spawnBall() function. It should be good to go now :slight_smile:

Brandon [import]uid: 136211 topic_id: 31552 reply_id: 126069[/import]

Yeah - I’d agree with Brandon, code looks perfect apart from that.

Good luck with it! [import]uid: 163581 topic_id: 31552 reply_id: 126094[/import]

thanks very very much… it works brandon !!! thanks [import]uid: 179948 topic_id: 31552 reply_id: 126101[/import]