i am attempting to spawn an object that matches the randomly spawned object but am not sure how to do so.
i have a folder called Objects , within that folder i have 18 variations of the same object named object1,object2,object3…etc.
i want to be able to spawn the first object in the middle of the screen then spawn the object that matches it on the right or left side of the screen.
here is the code i have on this,
local function objectMatch() -- works on left side of screen if newObject == "Objects/Object2.png" then display.newImageRect( mainGroup, "Objects/Object2.png" , 69.2 , 60.8, display.contentCenterX, display.contentCenterY) end --newObject2 = display.newImageRect(mainGroup,"Objects/Object" .. math.random(18) .. ".png", 69.2 , 60.8) --newObject2.x = 50 --newObject2.y = math.random (60 , 600) end local function objectMatch2() -- works on right side of screen newObject3 = display.newImageRect(mainGroup,"Objects/Object" .. math.random(18) .. ".png", 69.2 , 60.8) newObject3.x = 310 newObject3.y = math.random (60 , 600) end local function createObjects() newObject = display.newImageRect(mainGroup,"Objects/Object" .. math.random(18) .. ".png", 69.2 , 60.8) table.insert( ObjectsTable, newObject ) physics.addBody( newObject, "dynamic", {radius=0 , bounce = 0.5} ) -- changing radius adjusts hit box, hitbox does not mean touchbox newObject.myName = "Object" objectMatch() objectMatch2() local whereFrom --from top newObject.x = math.random(150, 210) -- keeps Objects within middle of screen newObject.y = -60 end
The code currently will just spawn random objects and not match the objects already spawned, this is because of the code that is in objectMatch2()
local function objectMatch2() -- works on right side of screen newObject3 = display.newImageRect(mainGroup,"Objects/Object" .. math.random(18) .. ".png", 69.2 , 60.8) newObject3.x = 310 newObject3.y = math.random (60 , 600) end
i attempted to get this to work with the code in objectMatch1() but this did not work either.
local function objectMatch() -- works on left side of screen if newObject == "Objects/Object2.png" then display.newImageRect( mainGroup, "Objects/Object2.png" , 69.2 , 60.8, display.contentCenterX, display.contentCenterY) end --newObject2 = display.newImageRect(mainGroup,"Objects/Object" .. math.random(18) .. ".png", 69.2 , 60.8) --newObject2.x = 50 --newObject2.y = math.random (60 , 600) end
any help on how to solve this would be much appreciated!