Log In  


Cart #25546 | 2016-07-19 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
2

Hi,

I'm a bit of a beginner where programming is concerned, but I'm trying to work out how best to create an infinite scrolling background for a side-scrolling shooter game.

I figured that the way to achieve this would be to make a screen full of sprites and move them to the left, deleting them once they are off the screen and replacing them with new sprites on the right. I have managed to achieve this, but I don't think the performance is that great. It doesn't feel very smooth.

Am I doing it the right way? Is there a better way?

Thanks for any help you guys can give!

--tile scrolling

function _init()
	tile_h={} --horizontal tile strip

	--spawn 16 tiles in strip
	for i=0,16 do
		spawn_tile(i*8,0)
	end
end

function _update()
	foreach(tile_h,update_tile)
end

function _draw()
	cls()
	for i=0,16 do
		foreach(tile_h,draw_tile)
	end
end

function update_tile(t)
	t.x-=1 --move tile to left

	--if tile off edge of screen
	if t.x<-8 then
		del(tile_h,t) --delete tile

		--add new tile to right
		spawn_tile(16*8,0)
	end
end

function spawn_tile(x,y)
	local t={}
	t.x=x
	t.y=y
	add(tile_h,t)
end

function draw_tile(t)
	--draw 16 tiles vertically
	for i=0,16 do
		spr(1,t.x,t.y+i*8)
	end
end
2


I made a scroller that used part of the map instead of individual sprite/objects.

I added enough map swatches to fill the initial space I needed plus one full size extra, and then moved them with x/y or whatever. When it's coordinate went off screen, I removed it and added to the other end so it just kept recycling the same 2 over and over without having to increase anything.

Here's the game I made with that technique:
https://www.lexaloffle.com/bbs/?pid=23524

You'll see the trees at the bottom scrolling by...that's a map moving and recycling. Actually, several layers.

Using a map will be much smoother than moving a ton of sprite objects all at once - I found that out the hard way too.


@morningtoast Ahhh I see, so it's the same method as I have used, but more like moving a pair of huge tiles rather than a load of small ones. I shall give that a go, thanks for your advice!


@morningtoast I've implemented your suggestion and it works like a charm! And it of course has the added benefit of allowing detail in the tiling. Thanks a lot!

Cart #25554 | 2016-07-19 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA


Sweet, glad that worked out. Nice, smooth tiles. Can't wait to see your shooter...love me some shmups :)


Added parallax

Cart #25561 | 2016-07-19 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA



[Please log in to post a comment]