I’ll post the code so you can see what I’m doing. My game is an endless runner so the obsatcles last until the player dies. In addition to that I am using 15 different obstacle images. Every obstacle has its own style and size. Here is the code I am using right now:
-- Located in the scene:create to load in the obstacle. Have 15 different images op1 = display.newImageRect(sceneGroup, "levels/normalMode/level1/obstacle1.png", 491, 450) op1.isVisible = false op1.isBodyActive = false --[[Located in scene:show, methods controling each individual obstacle due to them having their own animations or rotations. Obstacles travel off screen to the x coordinate of screen width-2500 then gets cycled out]]-- local opMove1 = function() physics.addBody(op1, "kinematic", physicsData:get("obstacle1") ) op1.x = 1000 op1.y = 200 op1.isVisible = true op1.isBodyActive = true op1.type = "obstacle" local function op1Remove(target) op1.isVisible = false op1.isBodyActive = false physics.removeBody(target) end transition.to(op1,{time = 12500,x=(W-2500), onComplete = op1Remove, tag = "transTag"}) end -- The random selection and generation of the obsstacles local function ranGen() local obs = {1, 2, 3, 4, 5, 6} local index = math.random(#obs) local obstacle = obs[index] if index == 1 then opMove1() if op1.isVisible == true then table.remove(obs, 1) elseif op1.x == (W-2500) then table.insert(obs, 1, 1) end elseif index == 2 then opMove2() if op2.isVisible == true then table.remove(obs, 2) elseif op2.x == (W-2500) then table.insert(obs, 2, 2) end elseif index == 3 then opMove3() if op3.isVisible == true then table.remove(obs, 3) elseif op3.x == (W-2500) then table.insert(obs, 3, 3) end elseif index == 4 then opMove4() if op4.isVisible == true then table.remove(obs, 4) elseif op4.x == (W-2500) then table.insert(obs, 4, 4) end elseif index == 5 then opMove5() if op5.isVisible == true then table.remove(obs, 5) elseif op5.x == (W-2500) then table.insert(obs, 5, 5) end elseif index == 6 then opMove6() if op6.isVisible == true then table.remove(obs, 6) elseif op6.x == (W-2500) then table.insert(obs, 6, 6) end else end end objTimer = timer.performWithDelay(2500, ranGen, -1)
So what’s happening is math.random chooses a number, continues to go through iteration, but sometimes calls lets say 3, then 3 again right afterwards. This makes obstacle 3 vanish and restart at the beginning before it reaches the point of where isVisible = false and isBodyActive = false. Multiple obstacles are selected before one another goes off screen. At one given time 3 obstacles could be on screen, so I am trying to restrict it from choosing the same number to avoid it calling a method twice if that method/obsatcle is still on screen.
Can something like this be done using your pooling method if I were to make a random selection of the images and use a get.width and get.height for the display.newImageRect()? Something like:
local obs = {{filename = "normalMode/level1/obstacle5", width = 598, height = 450, tag = "obstacle5"}, {filename = "normalMode/level1/obstacle4", width = 562, height = 450, tag = "obstacle4"}, {filename = "normalMode/level1/obstacle6", width = 379, height = 396, tag = "obstacle6"}, {filename = "normalMode/level1/obstacle2", width = 478, height = 450, tag = "obstacle2"}, {filename = "normalMode/level1/obstacle1", width = 491, height = 450, tag = "obstacle1"}} local index = math.random(#obs) local obstacle = obs[index] local obstacles = 15 local obj = {} for i = 1, obstacles do obj[i] = display.newImageRect(sceneGroup, obstacle.filename..".png", obstacle.width, obstacle.height); obj[i].isVisible = false obj[i].isBodyActive = false end
Using this method doesn’t allow me to preload the images using composer.loadScene(), so I’d have to use display.newImage to get the width and heigh of the image then call it into that method. Thanks for your help, I appreciate it!!!