Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Jaffles on Wed 14/03/2012 03:07:33

Title: Constantly Scrolling Background [FIXED]
Post by: Jaffles on Wed 14/03/2012 03:07:33
What is the best way to go about creating a constantly scrolling background?

In this case, there are two Characters racing, and I need the background to scroll past them to give the illusion of movement.

Should I just create an incredibly long background, set it as an object in the room and have it scripted to move across the screen until the finish line appears? Or is there a much less convoluted way to do this?

Thanks in advance
Title: Re: Constantly Scrolling Background
Post by: Eric on Wed 14/03/2012 04:28:48
Someone else will chime in with a more detailed answer, but I believe the recommended way is two copies of the background image, one that begins its loop at the end of the other, and vice versa.
Title: Re: Constantly Scrolling Background
Post by: Khris on Wed 14/03/2012 11:01:23
It doesn't have to be incredibly long, only two screens long.

I'd use an object, position it at (0, Screen.Height) and move it like this:

// in room_RepExec

  Object*o = oScrollingBackground;
  if (o.Visible) {
    o.X -= 1;    // speed
    if (o.X == -320) o.X = 0;
  }


Where it says speed, try 2, 4, 5, 8 instead of 1 to make it scroll faster
For other speeds, make the sprite slightly wider so its width is a multiple of the speed.
Title: Re: Constantly Scrolling Background
Post by: Jaffles on Wed 14/03/2012 20:59:54
Thanks for the help, I just had a few questions concerning the code, to make sure I understand it properly (and thus actually learn something instead of just copypasting and calling it a day).

// in room_RepExec

  Object*o = oScrollingBackground;
  if (o.Visible) {
    o.X -= 1;    // speed
    if (o.X == -320) o.X = 0;
  }

How is this actually moving the background? Is there a command that is moving it? Also, why is there a - before the speed? Thanks again for the code though.
Title: Re: Constantly Scrolling Background
Post by: Victor6 on Wed 14/03/2012 21:06:41
o.X -= 1  // This line reduces the X co-ordinate of the background by 1.
The - causes it to move right to left.
Title: Re: Constantly Scrolling Background
Post by: monkey0506 on Wed 14/03/2012 21:33:07
To further clarify the -= is a single operator, not two separate operations.

The -= operator is a shortcut:

int X = 5;
X = (X - 3); // set X equal to its current value, minus 3


Simply becomes:

int X = 5;
X -= 3; // set X equal to its current value, minus 3


There is also a += operator for assignment, and then the comparison operators == and != (equals and not equals, respectively). These operators should not be confused with single-symbol operators, or operators which are simply next to each other (as in assigning a literal negative value).

Oh, and also regarding what is "actually moving the background", the "background" in this case is being stored in an AGS Object, and the object is in turn having its x-coordinate moved (keep in mind that the Y axis is inverted from a typical graph, but the X axis is the same, so the top-left corner of the screen is the origin).
Title: Re: Constantly Scrolling Background
Post by: Jaffles on Wed 14/03/2012 21:38:37
Ok, I see. I'm still picking up the shorthand for these things. Thanks for all the help guys, the scrolling works great!
Title: Re: Constantly Scrolling Background [FIXED]
Post by: monkey0506 on Wed 14/03/2012 21:46:41
Seriously, the fact that you asked for clarification says a lot more about you than some folks. ;) Welcome to the forums by the way!
Title: Re: Constantly Scrolling Background [FIXED]
Post by: Jaffles on Wed 14/03/2012 22:43:49
Gah, sorry to post again, but there's been a slight snafu. Whenever I put numbers higher than five in for the speed, the second part of the code doesn't work. Is there some specific parameter I'm missing (like the number has to go into 320 evenly) or is there something else I'm missing?

And thanks guys, It's good to know that there are helpful people who're willing to help newcomers like myself.
Title: Re: Constantly Scrolling Background [FIXED]
Post by: monkey0506 on Wed 14/03/2012 22:58:16
Change this line:

if (o.X == -320) o.X = 0;

To this:

if (o.X <= -320) o.X += 320;

The original line Khris wrote was based on the change always being exactly 1, in which case -320 would always be reached. The case would also be the same for shifts of 2, 4, or 5, but not shifts of 3, 6, or 7, as 320 isn't evenly divisible by those. If you check if the X coordinate is less than or equal to -320 (another two-symbol operator, also >= for greater than or equal to) then you can shift by any value and once it passes the limit at -320 it will then be shifted back to the left. Of course that might then mean you would need to expand the background slightly more than just the two screens.
Title: Re: Constantly Scrolling Background [FIXED]
Post by: Jaffles on Wed 14/03/2012 23:07:02
Works like a charm! Thanks for the fix!
Title: Re: Constantly Scrolling Background [FIXED]
Post by: Khris on Thu 15/03/2012 00:47:54
Like I said, for other speeds, the sprite has to be wider, as in, a multiple of the speed.
6: 324
7: 322
9: 324
etc.

Smooth movement requires the x movement to be the same every frame, so if the total width of one background tile isn't a multiple of the x movement per frame, that's not going to happen. That's why I posted code which would break as soon as the user didn't pay attention to that.