Hi, I’m currently using a Factory collection to generate the endless map of my game as it goes. Each segment is 512px wide and the Factory which spawns these collections is at the 960px position as the intention is that the tiles spawn outside the player’s screen when the end of each segment reaches that point so it’s seemles.
Spawning the tiles works mostly fine but they generate a gap between each segment as seen in the video bellow.
I tried debugging the code and found out that the trigger that checks the position has some kind of delay.
Maybe is actually getting the position value of the collection each frame but when it receives the notice that the position is lesser or equal to 960px it’s already bellow that point and that’s why it spawns with those gaps.
Here’s my most recent code of this functional version. I’ve tried looking for possible delay timers for the factory to spawn and other workarounds this issue but I’m against another roadblock. Thanks again in advance for the help and tips
Position Marker/Parent Object that moves the tiles
function init(self)
self.speed = go.get("/controller#controller", "speed")
self.spawn_free = true
--self.moving = true
end
function update(self, dt)
local pos = go.get_position()
if self.spawn_free and pos.x <= 960 then
print(go.get_position())
msg.post("/level_gen_factory", "spawn_free")
self.spawn_free = false
end
pos.x = pos.x - self.speed * dt
go.set_position(pos)
if pos.x < -10 then
go.delete(true)
end
end
Factory code
local probability = {
[1] = 100,
[2] = 0,
[3] = 0,
[4] = 0
}
local function level_random(max)
local lvl
local random = math.random(1, max)
if random <= probability[1] then
lvl = 1
elseif random <= probability[1] + probability[2] then
lvl = 2
elseif random <= probability[1] + probability[2] + probability[3] then
lvl = 3
elseif random <= probability[1] + probability[2] + probability[3] + probability[4] then
lvl = 4
end
return lvl
end
function init(self)
self.probability_max = 0
for i = 1, #probability do
self.probability_max = self.probability_max + probability[i]
end
math.randomseed(tonumber(hash_to_hex(hash(tostring({}))):sub(4, 8), 16))
self.spawn_free= true
end
function on_message(self, message_id, message, sender)
if message_id == hash("spawn_free") then
self.spawn_free = true
print("Spawn Tile")
end
end
function update(self, dt)
if self.spawn_free then
self.spawn_free = false
local pos = go.get_position()
pos.y = 0
local component = "/level_gen_factory#level_gen_factory_" ..level_random(self.probability_max)
local level = collectionfactory.create(component, pos)
for key, value in pairs(level) do
msg.post(value, "start_animation", { delay = 0.3 * math.random()})
msg.post(value, "disable_spawncheck")
end
end
end