Hello every body,
I am trying to implement an endless runner with tilemap segments as suggested by @vigridzki and @britzl in this other thread: Endless Runner - segment generation
I have followed the same setup suggested in the post:
The segment1.collection contains the tilemap from @britzl examples
That tilemap has a tilesource with tiles 64x64 pixels in size (thanks again @sicher for the name of the collection )
The content of segment.script file
function init(self)
-- Register segment with the controller
msg.post("game:/game", "register_segment", { url = msg.url("#collectionfactory"), id = go.get_id() })
end
And in the game.script file I have something like this (simplified version):
function on_message(self, message_id, message, sender)
if message_id == hash("register_segment") then
table.insert(self.segments_url, message.url)
if #self.segments_url == MAX_SEGMENTS then
self.all_segments_registered = true
spawn_segment(self)
spawn_segment(self)
spawn_segment(self)
end -- if max segments
end
end
function spawn_segment(self)
local xpos = (845 * #self.segments) -- <--------- POSITION IN PIXELS HERE
local seg_num = 1
if math.random() > 0.5 then
seg_num = 2
end
-- spawn tilemap
local segment = collectionfactory.create(self.segments_url[seg_num], vmath.vector3(xpos, 0, 0), nil, { }, 0.6)
table.insert(self.segments, segment)
end
But I can not make the segments spawn together seamlessly, first my logical thought made me get the bounds of the tilemap and multiply them by the size of the tilesource tile
local x, y, w, h = tilemap.get_bounds(segment_url)
xpos = (x + w) * 64
print("BOUNDS", x, y, w, h, w * 64, xpos)
-- RESULT:
DEBUG:SCRIPT: BOUNDS -1 1 22 23 1408 1344
But the result was a big gap between the tilemaps, then I tried to fine tune it by hand and I arrived to the value 845 shown in the code above, but it only works for the first spawn, after that there is always a small gap of varying dimensions:
I think I might missing some property or some configuration some place but I don’t know where.
Any help, observations or suggestions will be greatly appreciated and welcome.
Thanks!