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
P#25547 2016-07-19 10:10 ( Edited 2016-07-19 18:03)

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.

P#25550 2016-07-19 11:22 ( Edited 2016-07-19 15:31)

@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!

P#25553 2016-07-19 11:56 ( Edited 2016-07-19 15:56)

@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

P#25555 2016-07-19 12:16 ( Edited 2016-07-19 16:16)

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

P#25556 2016-07-19 12:57 ( Edited 2016-07-19 16:57)

Added parallax

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

P#25562 2016-07-19 14:03 ( Edited 2016-07-19 18:03)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 08:15:39 | 0.019s | Q:24