Hello,
I am pretty new to the Defold platform and have been folowing the “Getting Started” tutorial.
When the first code was introduced (the code for moving the ground), I had trouble understanding two lines of code
local pieces = { "ground0", "ground1", "ground2", "ground3",
"ground4", "ground5", "ground6" } 1
function init(self) 2
self.speed = 6
end
function update(self, dt) 3
for i, p in ipairs(pieces) do 4
local pos = go.get_position(p)
if pos.x <= -228 then 5
pos.x = 1368 + (pos.x + 228)
end
pos.x = pos.x - self.speed
go.set_position(pos, p) 6
end
end
Here, I don’t understand the relevance of adding (pos.x+228) to 1368.
Also, if the game object is on the left most edge of the screen, we bring to 1368, which is the rightmost edge so even then why do we subtract self.speed in the X axis?
Please explain.
@sicher: I think we should improve the tutorial to define a constant for the width of a platform and perhaps use the width multiplied by the number of entries in pieces when we move the platforms.
Thank you. I understood the concept clearly now but still I have doubts about it:
Even after moving it by 1596, we move it by self.speed (6) to the left. Why is that? And what does 1368+(pos.x+228) mean? I mean why don’t we simply say 1596?
And I did what you asked. I set the pos.x=1596 in the IF statement. However this doesn’t work and this leaves a big gap in the ground.
I am not getting this at all. Please help.
All ground pieces should move at a constant speed every frame, even the one that is moved to the end of the line. If you’re not consistently doing this you’ll get a gap or overlap. You always want all ground pieces to match up one after the other without a single pixel’s gap between them. For this to happen you need to move all of them at the same speed every frame and the one that moves outside the left edge of the screen must be positioned as the rightmost ground piece.