Infinite scrolling bug

So here’s the code for my infinite scroller, it uses basically 2 objects with sprites attached and just scrolls them infinitely. But in this image you can see the problem? I’ve programmed this many times before, and I feel like I’m missing something stupid, but I can’t for the life of me figure it out. :sweat:

function init(self)
	self.width = 1000
	self.edge = 0 - self.width / 2
	self.speed = 600
end

function update(self, dt)
	fg1Pos = go.get_position("fg1")
	fg2Pos = go.get_position("fg2")
	
	fg1Pos.x = fg1Pos.x - self.speed * dt
	if fg1Pos.x + self.width / 2 < self.edge then
		fg1Pos.x = fg2Pos.x + self.width
	end

	fg2Pos.x = fg2Pos.x - self.speed * dt
	if fg2Pos.x + self.width / 2 < self.edge then
		fg2Pos.x = fg1Pos.x + self.width
	end
	
	go.set_position(fg1Pos, "fg1")
	go.set_position(fg2Pos, "fg2")
	
end

What is the actual width of the texture?

It’s 1000, I double-checked. It’d be more proper if I actually knew how to reference the actual width of the texture, but I figure it’d be easier on a newbie like me if I just put in the magic number. :sweat:

–Edit
I just noticed something just now. If I lower the speed, the gap is smaller between the 2 images. :thinking: Is it somehow setting the position at the wrong time?

Fixed it.

function update(self, dt)
	fg1Pos = go.get_position("fg1")
	fg2Pos = go.get_position("fg2")
	
	fg1Pos.x = fg1Pos.x - self.speed * dt
	fg2Pos.x = fg2Pos.x - self.speed * dt
	
	if fg2Pos.x + self.width / 2 < self.edge then
		fg2Pos.x = fg1Pos.x + self.width
	end

	if fg1Pos.x + self.width / 2 < self.edge then
		fg1Pos.x = fg2Pos.x + self.width
	end

	go.set_position(fg1Pos, "fg1")
	go.set_position(fg2Pos, "fg2")
end
1 Like