1

I'm trying to create a function that would choose one of four objects from a table that would then create it. Essentially what's meant to happen is if objects 1-3 are chosen they would be considered "regular" and the "player" would have to catch them while the fourth object is "special" and would take points away if caught.

here's the table

local objects = {
"object1",
"object2",
"object3",
"object4"
}

this is the code I'm using in place of what I want currently (mostly to make sure everything else in my code is working)

local function spawnObject()
 object = display.newImage("images/object1.png")
object.x =  math.random(screenLeft+ 30, screenRight-30)
object.y = screenTop-100
object.width = 100
object.height = 100
object.Number = 1
object.type = "regular"
object.score = 5
object.rotation = math.random(-20,20)
    
physics.addBody(object, "dynamic", {density =1, friction =1, bounce = .5})
end

1 Answer 1

1

Start with this...

local function spawnObject()

local objects = {
"object1",
"object2",
"object3",
"object4"
}

object = display.newImage("images/"..objects[math.random(3)]..".png")
object.x =  math.random(screenLeft+ 30, screenRight-30)
object.y = screenTop-100
object.width = 100
object.height = 100
object.Number = 1
object.type = "regular"
object.score = 5
object.rotation = math.random(-20,20)
    
physics.addBody(object, "dynamic", {density =1, friction =1, bounce = .5})
end

...for choosing 1-3.
If you dont like math.random(3) you can do a table.remove(objects,1) 3 times so it is clear the 4th time is the special one.
But then have to be objects global ( outside function and not local ).
...you have to check for.

Not the answer you're looking for? Browse other questions tagged or ask your own question.