ForumsProgramming ForumLooping the background

5 2517
seven_11
offline
seven_11
304 posts
Nomad

Hi i am trying to make a game using http://www.kongregate.com/games/Kongregate/shootorial-2
but when i try to make the background loop it just goes off the screen any help?

  • 5 Replies
Erasmus_Darwin
offline
Erasmus_Darwin
59 posts
Nomad

You're constantly moving the background to the left by decreasing its x coordinate on every frame, right? So if your original background (before you double it up) is 1000 pixels wide, then you check if the x coordinate is <= -1000. If so, you add 1000 to the x coordinate.

seven_11
offline
seven_11
304 posts
Nomad

I think its 2110 pixels wide so i add 1000 and make it 3110 pixels wide?

Agent_86
offline
Agent_86
2,132 posts
Nomad

Well, you could also tile the background...

dank
offline
dank
986 posts
Peasant

Assuming you bg is in a MC:
Select all of the bg art in the MC
duplicate it
on duplication modify>transform>flip horizontally
align to adjacent side of other bg art
select all art and align to right side of stage
close the mc

then use the code:
if(x < stage.stageWidth - width){
x = 0;
}else{
x--;
}

Erasmus_Darwin
offline
Erasmus_Darwin
59 posts
Nomad

I think its 2110 pixels wide so i add 1000 and make it 3110 pixels wide?


Err, sort of.

Let's say your screen is 600 pixels wide. You want to copy the first 600 pixels of the beginning of your background to the end of it. Now your background will be 2710 pixels wide.

In your onEnterFrame, you're doing something like this:
x = x - 3;
What you want to do after that is:
if (x <= -2110) {
x = x + 2110;
}

What happens is each time your original background is completely off the screen to the left, it'll slide it back to the right by the length of the background. And since you copied the beginning of the background on to the end, it'll look like a seamless loop because the end masks the blank area that would otherwise show up before it loops.

Also, I'm not an AS2 guy, so you might have to tweak the code a bit. I think AS2 uses _x instead of x.
Showing 1-5 of 5