Scrolling Background / Enemies Updating.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Scrolling Background / Enemies Updating.

Post by Ryne »

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.

Load

Code: Select all

	-- scrolling bg related code
	bgx = 0
	bgy = 0
	speed = 15
	
	bg2y = 0
	bgedge = bgx + 480
	
	-- scrolling bg related code
	bgx2 = 0
	bgy2 = 0
	speed2 = 30
	
	bg2y2 = 0
	bgedge2 = bgx2 + 480

Update

Code: Select all

	-- bgcloud scrolling code
	bgx = bgx - speed * (dt)
	bgedge = bgedge - speed * (dt)
	
	if bgx <= -480 then
	bgx = bgedge + 480
	end
	
	if bgx <= 0 then
	bgedge = bgx + 480
	end	
	
	-- fgcloud scrolling code
	bgx2 = bgx2 - speed2 * (dt)
	bgedge2 = bgedge2 - speed2 * (dt)
	
	if bgx2 <= -480 then
	bgx2 = bgedge2 + 480
	end
	
	if bgx2 <= 0 then
	bgedge2 = bgx2 + 480
	end	
I was just wondering if anyone had any tips to make it better, or even a more efficient method?
Last edited by Ryne on Mon Dec 13, 2010 6:07 pm, edited 1 time in total.
@rynesaur
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Scrolling Background - Working but ugly

Post by Mud »

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):

scroller management

Code: Select all

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
love.load

Code: Select all

add_scroller(0, 0, 15, 0, 480)
add_scroller(0, 0, 30, 0, 480)
-- add more scrollers here
love.update

Code: Select all

update_scrollers(dt)
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Scrolling Background - Working but ugly

Post by Ryne »

Mud wrote:
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):

scroller management

Code: Select all

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
love.load

Code: Select all

add_scroller(0, 0, 15, 0, 480)
add_scroller(0, 0, 30, 0, 480)
-- add more scrollers here
love.update

Code: Select all

update_scrollers(dt)
Ahh, I see. Thank's a lot! :)
@rynesaur
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Scrolling Background / Enemies Updating.

Post by Ryne »

Hi everyone. I'm using this code to update my enemy animations

Code: Select all

	-- 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?
@rynesaur
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Scrolling Background / Enemies Updating.

Post by bartbes »

Well, there are 2 solutions, either make every enemy have its own animation, or instead of looping over the enemies, looping over the animations.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Scrolling Background / Enemies Updating.

Post by Ryne »

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?
@rynesaur
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Scrolling Background / Enemies Updating.

Post by bartbes »

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:

Code: Select all

for i = 1, #anims do
    anims[i]:update(dt)
end
(you might want to look at the generic for though)
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Scrolling Background / Enemies Updating.

Post by tentus »

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.
Kurosuke needs beta testers
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest