Help with Tables

This is part code, I need to add my balloon code to a table which I do not know how to do, and then I need a subtract-balloon button as well. Would anyone be willing to help me with this? Just post on here or contact me on skype, dayaffe99.

[code]
local buttons = {}

local touchButton = function (event) – function to handle touch events

local obj = event.target – localise the button pressed
group:insert( obj )
if event.phase == “ended” then

if obj.id == 1 then – if button with ID=1 is pressed
local balloon = display.newImage(“red_balloon.png”)
balloon.x = display.contentWidth/2
physics.addBody(balloon, { bounce = 0.5, radius = 45, friction = 1.0} )
balloon:addEventListener(“touch”, moveBalloon)
balloon:addEventListener(“swipe”, moveBalloon)
group:insert( balloon )
– put your add balloons code here
–remember to add each one to the balloons table

end

end

end

local i = display.newImageRect(“add_button.png”, 75, 75) – load a button image, make it 60 pixels wide and 30 high

i.x = 580; i.y = 55 – 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

group:insert( i ) [import]uid: 226740 topic_id: 37026 reply_id: 67026[/import]

I don’t get what you are trying to do.

Why do you need multiple buttons in the same location?

Why not use 1 button that spawns a new balloon every time it is touched?

Just guessing, because I don’t get it, but don’t you really want to add the balloons to a table when they are spawned each time the button is touched and have the balloons referenced with an ID so you can tell which one got “popped” or whatever and then count it somehow and remove it from the screen?

FWIW, I wouldn’t use “i” to reference a button, I’d use “button”, as often “i” is often used to reference indexing of a table…just say’n, it helps others decipher your code.

Nail [import]uid: 106779 topic_id: 37026 reply_id: 145266[/import]

I do not fully understand.
I guess you´re using storyboard and that´s why you insert all objects into the group group!? And now you want another group( or table) for just the ballons? Right?
I think it should work if have just another group “balloons” have this new group declared earlier and insert it into the storyboard group. Everytime you insert a ballon into balloons it´s part of group as well.
If you want to delete a ballon just for-loop through ballons and delete one.
[import]uid: 64016 topic_id: 37026 reply_id: 145275[/import]

I believe he needs help with how to implement tables? Although he is using the buttons table just fine, but maybe someone helped him with that and now he needs help to understand the whole concept to add for other things.

Tables are simple

local balloons = {} --congratulations you have just created a table  

Tables are simple

--these are just examples:  
local balloons = { 5, 7, 13, 61 }  
  
--if you were to print this  
print( balloons[3] ) -- it would call the third stored value, in this case it would be 13  
print( balloons[1] ) --it would be 5  
print( balloons[4] ) --and of course it would be 61, simple enough.  

Let’s add this to your code. I will also add a maxBall and minBall. Even though you could use other methods, this will be easier to understand and implement.

local buttons = {}  
maxBall = 1  
minBall = 1  
   
   
local touchButton = function (event) -- function to handle touch events  
   
 local obj = event.target -- localise the button pressed  
 group:insert( obj )  
 if event.phase == "ended" then  
   
 if obj.id == 1 then -- if button with ID=1 is pressed  
local balloon[maxBall] = display.newImage("red\_balloon.png")  
balloon[maxBall].x = display.contentWidth/2  
physics.addBody(balloon[maxBall], { bounce = 0.5, radius = 45, friction = 1.0} )  
balloon[maxBall]:addEventListener("touch", moveBalloon)  
balloon[maxBall]:addEventListener("swipe", moveBalloon)  
group:insert( balloon[maxBall] )  
maxBall = maxBall + 1  
 -- put your add balloons code here  
 --remember to add each one to the balloons table  
 elseif obj.id == 2 then  
 balloon[minBall]:removeSelf()  
 balloon[minBall] = nil  
 minBall = minBall + 1  
 end  
   
 end  
   
end  
  
local i = display.newImageRect("add\_button.png", 75, 75) -- load a button image, make it 60 pixels wide and 30 high  
   
i.x = 580; i.y = 55 -- 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  
   
group:insert( i )  
  
local o = display.newImageRect("add\_button.png", 75, 75)  
o.x = 675  
o.y = 55  
o:addEventListener("touch",touchButton)  
o.id = 2  
buttons[2] = o  

So what this does is instead of:

--Instead of giving it a table number ourselves, we have a variable that starts at 1, in this case maxBall, and every time it creates a balloon then  
maxBall = maxBall + 1  
--Therefor the next time it creates a balloon maxBall will be 2, and it goes on and on, gradually increasing by 1  
--Now for minBall it does the same, it starts at 1 and removes the first balloon and then goes on to become number 2.  

I used o as button number 2 just to keep the code on par, but xnailbender is right, you do not want to use i and even o as a variable. But of course you can use anything you want. Hope this all makes sense
[import]uid: 77199 topic_id: 37026 reply_id: 145284[/import]

Im sorry if I was unclear, I only understand tables vaguely and I am working on it. What I was trying to do was add each balloon created with the above code (the code made an add balloon button) to a table and then create a remove balloon button. I now understand how to do the table but could someone please put the code for a “remove balloon” button. Thanks. [import]uid: 226740 topic_id: 37026 reply_id: 145301[/import]

In my post above I already added a remove button and showed you how to remove. If you copy pasted without reading and it didn’t work that’s because the second button wasn’t added to group, so :

group:insert( o ) [import]uid: 77199 topic_id: 37026 reply_id: 145321[/import]

I don’t get what you are trying to do.

Why do you need multiple buttons in the same location?

Why not use 1 button that spawns a new balloon every time it is touched?

Just guessing, because I don’t get it, but don’t you really want to add the balloons to a table when they are spawned each time the button is touched and have the balloons referenced with an ID so you can tell which one got “popped” or whatever and then count it somehow and remove it from the screen?

FWIW, I wouldn’t use “i” to reference a button, I’d use “button”, as often “i” is often used to reference indexing of a table…just say’n, it helps others decipher your code.

Nail [import]uid: 106779 topic_id: 37026 reply_id: 145266[/import]

I do not fully understand.
I guess you´re using storyboard and that´s why you insert all objects into the group group!? And now you want another group( or table) for just the ballons? Right?
I think it should work if have just another group “balloons” have this new group declared earlier and insert it into the storyboard group. Everytime you insert a ballon into balloons it´s part of group as well.
If you want to delete a ballon just for-loop through ballons and delete one.
[import]uid: 64016 topic_id: 37026 reply_id: 145275[/import]

I believe he needs help with how to implement tables? Although he is using the buttons table just fine, but maybe someone helped him with that and now he needs help to understand the whole concept to add for other things.

Tables are simple

local balloons = {} --congratulations you have just created a table  

Tables are simple

--these are just examples:  
local balloons = { 5, 7, 13, 61 }  
  
--if you were to print this  
print( balloons[3] ) -- it would call the third stored value, in this case it would be 13  
print( balloons[1] ) --it would be 5  
print( balloons[4] ) --and of course it would be 61, simple enough.  

Let’s add this to your code. I will also add a maxBall and minBall. Even though you could use other methods, this will be easier to understand and implement.

local buttons = {}  
maxBall = 1  
minBall = 1  
   
   
local touchButton = function (event) -- function to handle touch events  
   
 local obj = event.target -- localise the button pressed  
 group:insert( obj )  
 if event.phase == "ended" then  
   
 if obj.id == 1 then -- if button with ID=1 is pressed  
local balloon[maxBall] = display.newImage("red\_balloon.png")  
balloon[maxBall].x = display.contentWidth/2  
physics.addBody(balloon[maxBall], { bounce = 0.5, radius = 45, friction = 1.0} )  
balloon[maxBall]:addEventListener("touch", moveBalloon)  
balloon[maxBall]:addEventListener("swipe", moveBalloon)  
group:insert( balloon[maxBall] )  
maxBall = maxBall + 1  
 -- put your add balloons code here  
 --remember to add each one to the balloons table  
 elseif obj.id == 2 then  
 balloon[minBall]:removeSelf()  
 balloon[minBall] = nil  
 minBall = minBall + 1  
 end  
   
 end  
   
end  
  
local i = display.newImageRect("add\_button.png", 75, 75) -- load a button image, make it 60 pixels wide and 30 high  
   
i.x = 580; i.y = 55 -- 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  
   
group:insert( i )  
  
local o = display.newImageRect("add\_button.png", 75, 75)  
o.x = 675  
o.y = 55  
o:addEventListener("touch",touchButton)  
o.id = 2  
buttons[2] = o  

So what this does is instead of:

--Instead of giving it a table number ourselves, we have a variable that starts at 1, in this case maxBall, and every time it creates a balloon then  
maxBall = maxBall + 1  
--Therefor the next time it creates a balloon maxBall will be 2, and it goes on and on, gradually increasing by 1  
--Now for minBall it does the same, it starts at 1 and removes the first balloon and then goes on to become number 2.  

I used o as button number 2 just to keep the code on par, but xnailbender is right, you do not want to use i and even o as a variable. But of course you can use anything you want. Hope this all makes sense
[import]uid: 77199 topic_id: 37026 reply_id: 145284[/import]

Im sorry if I was unclear, I only understand tables vaguely and I am working on it. What I was trying to do was add each balloon created with the above code (the code made an add balloon button) to a table and then create a remove balloon button. I now understand how to do the table but could someone please put the code for a “remove balloon” button. Thanks. [import]uid: 226740 topic_id: 37026 reply_id: 145301[/import]

In my post above I already added a remove button and showed you how to remove. If you copy pasted without reading and it didn’t work that’s because the second button wasn’t added to group, so :

group:insert( o ) [import]uid: 77199 topic_id: 37026 reply_id: 145321[/import]