So I have a question about generating multiple objects of the same image. This is not by any means supposed to be random objects either. The obstacles, which are controlled by local functions, are random. Not the images that form them though. So In the scene:create, I am creating 14 display objects which I set the isVisible to false so I can load the class. Then when an obstacle function is selected by random generation, it sets the isVisible to true for the objects that form the obstacle. So an example would be, I used 4 of the 14 images and organized them to make up an obstacle for the player to go around or go through.
The issue is that lets say I have two obstacle functions that get called right after each other. One particular display object is in both of those functions, lets say obj4. If the object gets removed in the first function while the second is generated off screen, then that display object wont be in the second function. Or if the second function gets called before the first is finished, the object in the first function is removed and is generated in the second. I know this is because I am using one display object in multiple functions.
So my question is how would I generate multiples of the same object? I’m not looking to make it random generation. I’ve placed these objects in a particular order for game play reasons. I just need to be able to have multiples of the same image generate in different functions, without having to code multiples of the same image just with different variable names. I am trying to keep my texture memory in check. Here is an example of my code:
-- Inside the scene:create op4 = display.newImageRect(sceneGroup, "levels/normalMode/level1/ast4.png", 385, 539) op4.isVisible = false op4.isBodyActive = false camera:add(op4, 3) op6 = display.newImageRect(sceneGroup, "levels/normalMode/level1/ast6.png", 340, 335) op6.isVisible = false op6.isBodyActive = false camera:add(op6, 3) op14 = display.newImageRect(sceneGroup, "levels/normalMode/level1/ast14.png", 604, 653) op14.isVisible = false op14.isBodyActive = false camera:add(op14, 1) -- Inside my scene:show did function, is randomly selected by number generation assigned -- to particular methods. local opMove4 = function() physics.addBody(op4, "kinematic", physicsData:get("ast4") ) physics.addBody(op6, "kinematic", physicsData:get("ast6") ) physics.addBody(op14, "kinematic", physicsData:get("ast14") ) op4.x = W/2 + 1250 op4.y = H/2 + 450 op4.isVisible = true op4.isBodyActive = true op4.type = "obstacle" op6.x = W/2 + 1800 op6.y = H/2 - 420 op6.isVisible = true op6.isBodyActive = true op6.type = "obstacle" op14.x = W/2 + 1450 op14.y = H/2 - 50 op14.isVisible = true op14.isBodyActive = true op14.type = "obstacle" local function rotate() transition.to(op14, {time = 2000, rotation = op14.rotation-360, onComplete = rotate, tag = "transTag"}) end rotate() local function targetRemove(target) target.isVisible = false target.isBodyActive = false physics.removeBody(target) end transition.to(op4, {time = 8000, x = W/2 - 2250, onComplete = targetRemove, tag = "transTag"}) transition.to(op6, {time = 8000, x = W/2 - 1700, onComplete = targetRemove, tag = "transTag"}) transition.to(op14, {time = 8000, x = W/2 - 2000, onComplete = targetRemove, tag = "transTag"}) end local opMove5 = function() physics.addBody(op4, "kinematic", physicsData:get("ast4") ) physics.addBody(op6, "kinematic", physicsData:get("ast6") ) physics.addBody(op14, "kinematic", physicsData:get("ast14") ) op4.x = W/2 + 1250 op4.y = H/2 -200 op4.isVisible = true op4.isBodyActive = true op4.type = "obstacle" op6.x = W/2 + 1800 op6.y = H/2 - 420 op6.isVisible = true op6.isBodyActive = true op6.type = "obstacle" op14.x = W/2 + 1450 op14.y = H/2 + 300 op14.isVisible = true op14.isBodyActive = true op14.type = "obstacle" local function rotate() transition.to(op14, {time = 2000, rotation = op14.rotation-360, onComplete = rotate, tag = "transTag"}) end rotate() local function targetRemove(target) target.isVisible = false target.isBodyActive = false physics.removeBody(target) end transition.to(op4, {time = 8000, x = W/2 - 2250, onComplete = targetRemove, tag = "transTag"}) transition.to(op6, {time = 8000, x = W/2 - 1700, onComplete = targetRemove, tag = "transTag"}) transition.to(op14, {time = 8000, x = W/2 - 2000, onComplete = targetRemove, tag = "transTag"}) end
It’s pretty obvious as to what the issue is. Would I have to make a table or an array for each display object to allow me to use multiples of the same display object? Thanks for reading!