So I am making an endless runner game, and for the obstacles I use a table and random numbers to randomly select what obstacle function to generate. I have 15 functions right now that all generate different obstacles based on the number that the random generate chooses. The issue is that if the generator, lets say, generated 5. The function will run, obstacle comes on screen. Unfortunately, sometimes the generator will select the same number again right after it had selected the previous number. So lets say 5 again. What happens is the obstacle doesn't finish transitioning off the screen, it vanishes off the screen while physics remain, and starts over from where it began. Here is the code I am using:
local function ranGen()
local obs = {}
if ast1[a].isVisible == false then
obs[#obs + 1] = 1
end
if ast2[b].isVisible == false then
obs[#obs + 1] = 2
end
if ast3[c].isVisible == false then
obs[#obs + 1] = 3
end
if ast4[d].isVisible == false then
obs[#obs + 1] = 4
end
if ast5[e].isVisible == false then
obs[#obs + 1] = 5
end
if ast6[f].isVisible == false then
obs[#obs + 1] = 6
end
local function random()
--local random = obs[math.random(#obs)];
local index = math.random(#obs)
local obstacle = obs[index]
local num = {obstacle}
if obstacle == 1 and obstacle ~= num then
opMove1()
elseif obstacle == 2 and obstacle ~= num then
opMove2()
elseif obstacle == 3 and obstacle ~= num then
opMove3()
elseif obstacle == 4 and obstacle ~= num then
opMove4()
elseif obstacle == 5 and obstacle ~= num then
opMove5()
elseif obstacle == 6 and obstacle ~= num then
opMove6()
elseif obstacle == num then
random()
end
end
random()
end
objTimer = timer.performWithDelay(4000, ranGen, -1)
The idea behind how I coded it was if obstacle generated 1, then num would be 1. If obstacle generated 1 again, it would skip the first if statement and goto the last elseif statement and run the generation again until a number other than 1 was generated. I thought the implementation would be correct, but the issue is still occurring.
How I am trying to make this work is, when a random number generates, I want to avoid having that same number generate again right after it already had. Now if it generated 5, then 2, then 5 again, that would be fine. I am just trying to avoid having the same obstacle function repeat right afterward it already had. Does anyone see what I am doing wrong or could give me a pointer?
Thanks for reading!
Aucun commentaire:
Enregistrer un commentaire