Trouble with spawning random images

Hello everyone! I just need a little bit of help.
 
I’m trying to spawn random objects.  I having al ittle bit of trouble.
 
I’m assuming I would need tables.
 My current spawn code is

[lua]function spawnBoxes()
big1 = display.newImage(“big1.png”)
big1.x = 22 --math.random(22, 150) --playing with random positioning
big1.y = 10
big1.id = “Big 1”
big1V = 0
physics.addBody(big1, --“static”,
{ friction=1, bounce=0.0,density=0.0,
})

big2 = display.newImage(“big2.png”)
big2.x = 63 --math.random(22, 150) --playing with random positioning
big2.y = 10
big2.id = “Big 2”
big2V = 0
physics.addBody(big2, --“dynamic”,
{ friction=5, bounce=0.0, density=0.0,
})

big3 = display.newImage(“big3.png”)

big3.x = --math.random(22, 150)  --playing with random positioning

big3.y = 10

big3.id = “Big 3”

big3V = 0

physics.addBody(big3, --“dynamic”, 

   { friction=5, bounce=0.0,  density=0.0, 

      }) 

[/lua]

That works. What it does is drop is simply drop different boxes from the top of the screen.

What I am having a issue with is how can I spawn random boxes fall from the top of the screen. 

Basically pick one of those boxes above and randomly spawn it. Each box has specific variables, and id’s. 

So I don’t want to merely just change the graphic.

As I’ve said I have been playing with tables, to maybe help solve the problem. I’m kinda lost

this is my Table

[lua]local boxTable = {
{number=1, image=“big1.png”, big1V=1 },
{number=2, image=“big2.png”, big2V=2},
{number=3, image=“big3.png”, big3V=3 },

}
local randN = math.random(1,3)
local boxObj = display.newImage(boxTable[randN].image)[/lua]

This merely spawns a random picture from the table with no characteristics. aka variables.

Is there anyway I can pass those variables for specific boxes into the table ?

Thanks in advance for any help

also can I get a forum name change change to jmorgan or jmorgan88?

Your post is really confusing or I’m just tired.

You want to have 3 box images up top and when a user clicks on either of them it spawns that box randomly on the screen with certain characteristics?

Sorry I think I tried to over explain everything. I have 3 boxes each with their own characteristics/ variable etc. I was trying to figure out how I would go about randomly spawning one of the boxes.

Spawning one of the boxes randomly is really easy. Although honestly in your table number =1 and big1V = 1 and then id = “Big 1”, you can probably just have variable store the number 1 and it will do just as much as the other 3.  I’d suggest something like this

local boxTable = { "big1", "big2", "big3" } local boxes = {} local boxNr = 1 function spawnBoxes() boxes[boxNr] = display.newImage( boxTable[boxNr]..".png" ) boxes[boxNr].x = math.random(22, 150) boxes[boxNr].y = math.random(22, 150) boxes[boxNr].id = boxNr physics.addBody( boxes[boxNr], "static", { friction =1, bounce =0, density=0 } ) boxNr = boxNr + 1 end timer.performWithDelay( 1000, spawnBoxes, -1 )

if you want to spawn random images you just do a local ranNr = math.random( 1, 3 ) and change display.newImage( boxTable[ranNr]…".png" )

Also something jsut a little more advanced instead of using boxNr to plus one is you can use the table boxes directly. replace boxNr with #boxes. What #boxes does is it takes the number of variables in the table. So if there were 5 boxes than #boxes would return 5 so you could do this :

local boxTable = { "big1", "big2", "big3" } local boxes = {}   function spawnBoxes() local ranNr = math.random( 1, 3 ) boxes[#boxes + 1] = display.newImage( boxTable[ranNr]..".png" ) boxes[#boxes].x = math.random(22, 150) boxes[#boxes].y = math.random(22, 150) boxes[#boxes].id = boxNr physics.addBody( boxes[#boxes], "static", { friction =1, bounce =0, density=0 } )   end timer.performWithDelay( 1000, spawnBoxes, -1 )

Edit: If any of this doesn’t make sense, feel free to ask what you don’t get
 

Thanks for the help!

I was using the variable and id’s cause in my ontouch function I had it set up for when the user taps on a specific box it increases the score by the value of that box.

[lua]

local function onObjectTouch( self, event )

if event.phase == “began” then
print( self.id … " was tapped." )
Text.text = self.id …“was tapped”
self:removeSelf()

–bmy add on
–for big 1 obj if box one tapped increase score and variable by one
if self.id == “Big 1” then
big1V = big1V + 1
score = score + 1
print(“Big 1 Variable is now”… big1V)
print(Score)

else

–if box two tapped increase score and variable by two

if self.id == “Big 2” then
big2V = big2V + 1
score = score + 2
print(“Big 2 Variable is now”… big2V)
print(score)

else
if self.id == “Big 3” then
big3V = big3V + 1
score = score + 3
print(“Big 3 Variable is now”… big3V)[/lua]

I was trying program it  so that if it is box1 that spawns set somehow give it the variables of big1V, sets it’s Id to"big 1" etc.

An easier way to do this is like I wrote in the code above. For example I did :

boxes[boxNr].id = boxNr   if event.phase == "began" then local targetNr = self.id    if targetNr == 1 then big1V = big1V + 1 elseif targetNr == 2 then big2V = big2V + 1 elseif targetNr == 3 then big3V = big3V + 1    end score = score + targetNr end

That’s why I was suggesting you give them id’s of 1 2 3 instead of “big 1” or “big 2”

But if you want to do it your way let me know what you want to do exactly and I’ll help you do it your way

Your post is really confusing or I’m just tired.

You want to have 3 box images up top and when a user clicks on either of them it spawns that box randomly on the screen with certain characteristics?

Sorry I think I tried to over explain everything. I have 3 boxes each with their own characteristics/ variable etc. I was trying to figure out how I would go about randomly spawning one of the boxes.

Spawning one of the boxes randomly is really easy. Although honestly in your table number =1 and big1V = 1 and then id = “Big 1”, you can probably just have variable store the number 1 and it will do just as much as the other 3.  I’d suggest something like this

local boxTable = { "big1", "big2", "big3" } local boxes = {} local boxNr = 1 function spawnBoxes() boxes[boxNr] = display.newImage( boxTable[boxNr]..".png" ) boxes[boxNr].x = math.random(22, 150) boxes[boxNr].y = math.random(22, 150) boxes[boxNr].id = boxNr physics.addBody( boxes[boxNr], "static", { friction =1, bounce =0, density=0 } ) boxNr = boxNr + 1 end timer.performWithDelay( 1000, spawnBoxes, -1 )

if you want to spawn random images you just do a local ranNr = math.random( 1, 3 ) and change display.newImage( boxTable[ranNr]…".png" )

Also something jsut a little more advanced instead of using boxNr to plus one is you can use the table boxes directly. replace boxNr with #boxes. What #boxes does is it takes the number of variables in the table. So if there were 5 boxes than #boxes would return 5 so you could do this :

local boxTable = { "big1", "big2", "big3" } local boxes = {}   function spawnBoxes() local ranNr = math.random( 1, 3 ) boxes[#boxes + 1] = display.newImage( boxTable[ranNr]..".png" ) boxes[#boxes].x = math.random(22, 150) boxes[#boxes].y = math.random(22, 150) boxes[#boxes].id = boxNr physics.addBody( boxes[#boxes], "static", { friction =1, bounce =0, density=0 } )   end timer.performWithDelay( 1000, spawnBoxes, -1 )

Edit: If any of this doesn’t make sense, feel free to ask what you don’t get
 

Thanks for the help!

I was using the variable and id’s cause in my ontouch function I had it set up for when the user taps on a specific box it increases the score by the value of that box.

[lua]

local function onObjectTouch( self, event )

if event.phase == “began” then
print( self.id … " was tapped." )
Text.text = self.id …“was tapped”
self:removeSelf()

–bmy add on
–for big 1 obj if box one tapped increase score and variable by one
if self.id == “Big 1” then
big1V = big1V + 1
score = score + 1
print(“Big 1 Variable is now”… big1V)
print(Score)

else

–if box two tapped increase score and variable by two

if self.id == “Big 2” then
big2V = big2V + 1
score = score + 2
print(“Big 2 Variable is now”… big2V)
print(score)

else
if self.id == “Big 3” then
big3V = big3V + 1
score = score + 3
print(“Big 3 Variable is now”… big3V)[/lua]

I was trying program it  so that if it is box1 that spawns set somehow give it the variables of big1V, sets it’s Id to"big 1" etc.

An easier way to do this is like I wrote in the code above. For example I did :

boxes[boxNr].id = boxNr   if event.phase == "began" then local targetNr = self.id    if targetNr == 1 then big1V = big1V + 1 elseif targetNr == 2 then big2V = big2V + 1 elseif targetNr == 3 then big3V = big3V + 1    end score = score + targetNr end

That’s why I was suggesting you give them id’s of 1 2 3 instead of “big 1” or “big 2”

But if you want to do it your way let me know what you want to do exactly and I’ll help you do it your way