Hi guys, I wanted to set up scrolling clouds for my platformer background.
Basically I have foreground clouds, and background clouds for depth. The reason I don't like this code is because I have to repeat it for each additional scrolling image. Each have 2 images that repeat. So for the foreground Clouds, there is one scrolling image, then a second attracted to the edge of that one, once the first scrolls off screen it attaches itself to the second one, this repeats.
Ryne wrote:I don't like this code is because I have to repeat it for each additional scroller image.
Put state data for a scroller in a table, then you can store a list of them. In your update handler, iterate through all the scrollers in the list and do your update logic.
Here's a crude refactor (not changing any of your logic, just rearrainging it):
local scroller = {}
function add_scroller(x, y, speed, y2, edge_offset)
scroller[#scroller+1] = { x=x, y=y, speed=speed, y2=y2, edge=x+edge_offset }
end
function update_scrollers(dt)
for i,g in ipairs(scroller) do
g.x = g.x - g.speed * dt
g.edge = g.edge - speed * dt
if g.x <= -480 then
g.x = g.edge + 480
end
if g.x <= 0 then
g.edge = g.x + 480
end
end
end
Ryne wrote:I don't like this code is because I have to repeat it for each additional scroller image.
Put state data for a scroller in a table, then you can store a list of them. In your update handler, iterate through all the scrollers in the list and do your update logic.
Here's a crude refactor (not changing any of your logic, just rearrainging it):
local scroller = {}
function add_scroller(x, y, speed, y2, edge_offset)
scroller[#scroller+1] = { x=x, y=y, speed=speed, y2=y2, edge=x+edge_offset }
end
function update_scrollers(dt)
for i,g in ipairs(scroller) do
g.x = g.x - g.speed * dt
g.edge = g.edge - speed * dt
if g.x <= -480 then
g.x = g.edge + 480
end
if g.x <= 0 then
g.edge = g.x + 480
end
end
end
-- enemies updating
for i=1, #enemies do
enemies[i].anim:update(dt)
end
The issue is that if 2 enemies are on the same animation (meaning both are using the same "idle" animation) then the animation double-updates and playes very fast. Is there a way to stop this besides manually updating every enemy animation separately?
bartbes wrote:Well, there are 2 solutions, either make every enemy have its own animation, or instead of looping over the enemies, looping over the animations.
The enemies are sort of endless mobs of the exact same sprite, so giving each it's own animation would be kind of inefficient I think. How would I go about looping over animations?
Well, it depends on your code, however, I must assume you store the animations somewhere, so if that table is called anims the code would be like this:
I have each enemy having their own animation and it works pretty ok (remember, we're talking about relatively small images here... even on a netbook having 100 copies of the same 64x64 image is pretty easy). The big problem with having everyone sharing a sprite is that they'll all move in sync, and it looks weird.