Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: FrankT on Sat 22/12/2012 12:41:51

Title: Continuous scrolling?
Post by: FrankT on Sat 22/12/2012 12:41:51
Is it possible to continuously scroll the background of a room without running into any edges?
Title: Re: Continuous scrolling?
Post by: Ghost on Sat 22/12/2012 13:37:39
You mean a looping room? Yes, that is possible, but it requires some trickery. Or do you want an automatically scrolling room "animation"?
Title: Re: Continuous scrolling?
Post by: MurrayL on Sat 22/12/2012 14:44:29
You can use the SetViewport(int x, int y) command to manually move the viewport around in a scrolling room. It'll lock the view to the specified position until you either call ReleaseViewport or leave the room.

To get a room to scroll continuously is a bit trickier, since you'll have to somehow deal with wrapping it around at one end, but it's certainly possible.
Title: Re: Continuous scrolling?
Post by: FrankT on Sat 22/12/2012 16:54:11
It is? How do I do it?
Title: Re: Continuous scrolling?
Post by: Ghost on Sat 22/12/2012 17:32:31
One way to create a scrolling room is to make a large background, and copy a section of the left side to the right side- it must be at least half the screen's width wide. So let's say you have a 500x240 background at 320x240, then you take the left 160 wide bit at copy to to the right, creating a 660x240 room.
In-game, you check if the player is at x:500, and then move him back to x:0. It's impossible to notice because the "right edge" is the same as the "left edge".

I think the age-old "Loopy's Looping Room" in the original AGS demo game did it just like that. Bet there's other ways too.
Title: Re: Continuous scrolling?
Post by: Crimson Wizard on Sun 23/12/2012 18:12:03
I am actually wondering, does FrankT needs scrolling room or scrolling background?
Because, technically speaking, this is two different things.

If you scroll the room (by moving viewport) you just change camera's point of view, which will make everything else in the room (characters, objects, hotspots) "move" other direction as well.
If you scroll only background picture, everything else will stay in place. (You may do this by setting a large object and move it, simulating background scroll).

What do you need depends on what are you trying to achieve in the end.
If you want to simulate road driving (or horse riding, or flying a plane, or similar kind of endless movement), the second variant is better, in my opinion. But again, this depends on goal and specifics.
Title: Re: Continuous scrolling?
Post by: FrankT on Sun 23/12/2012 19:39:22
Background scrolling! Yes - how do I do that?
Title: Re: Continuous scrolling?
Post by: Crimson Wizard on Mon 24/12/2012 00:26:20
Quote from: FrankT on Sun 23/12/2012 19:39:22
Background scrolling! Yes - how do I do that?

Simple example.
Place an object in the room, let's call it oBackground.
Apply background graphic to oBackground object.
Most easy way is to make the graphic consist of two copies of the same picture, with second copy applied to the right end of the first. Let's assume your room is 320 pixels wide, in this case you'll need 320x2 = 640 pixels wide graphic.

Now, put this into room's room_RepExec function:
Code (ags) Select

function room_RepExec()
{
  if (oBackground.X > -Room.Width)
  {
    oBackground.X = oBackground.X - 1;
  }
  else
  {
    oBackground.X = 0;
  }
}

This code will make this imporivised "background" scroll left.

The only problem is to draw the graphic the way that will make it look as a seamless picture. But that's drawing question. Hopefully someone will post an example.

Also, you may use more than one object this way, if you find it more convenient (like one object for far background, other for foreground etc).
Title: Re: Continuous scrolling?
Post by: kinan___rod on Mon 24/12/2012 12:20:57
I made a 640 width background and placed two hotspots on left and right to teleport the character, there is a small jump in where the loop triggers but it is hard to notice.
Code:
Code (AGS) Select
// room script file
function hHotspot2_WalkOn()
{
cEgo.x = 475;
cEgo.Walk(375, cEgo.x);
}
function hHotspot1_WalkOn()
{
cEgo.x = 165;
cEgo.Walk(265, cEgo.x);
}

Sample:
http://www.mediafire.com/?74vc7o8qmbjx5sb
Title: Re: Continuous scrolling?
Post by: Crimson Wizard on Mon 24/12/2012 12:34:02
This depends on what you are trying to do. In some cases this may be done by moving character, in other by scrolling an object.

For example, if character should remain controllable and move around the room in any direction.

FrankT did not explained what he wants in detail, and since other people already mentioned setting Viewport/moving Character methods, I added scrolling object variant.
Title: Re: Continuous scrolling?
Post by: kinan___rod on Mon 24/12/2012 12:43:21
AH I see
Title: Re: Continuous scrolling?
Post by: FrankT on Mon 24/12/2012 13:24:48
Ah, well, this is for the opening credit sequence; no character movement, except maybe for a few comets.
Title: Re: Continuous scrolling?
Post by: MiteWiseacreLives! on Mon 24/12/2012 17:46:58
Invisible Character walking across x-axis and a looped background like the other guys described..
I am new, but I think this is what your getting at??
Title: Re: Continuous scrolling?
Post by: Crimson Wizard on Mon 24/12/2012 17:59:28
Quote from: FrankT on Mon 24/12/2012 13:24:48
Ah, well, this is for the opening credit sequence; no character movement, except maybe for a few comets.
I think that Viewport manipulation could be more useful in such case. No extra object/hotspots needed.

Assuming you made scrollable room twice wider than your game's screen width (e.g. 640 for 320x* game):
Code (ags) Select

function room_RepExec()
{
  if (GetViewportX() < Room.Width / 2)
  {
    SetViewport(GetViewportX() + 1, 0);
  }
  else
  {
    SetViewport(0, 0);
  }
}
Title: Re: Continuous scrolling?
Post by: FrankT on Mon 24/12/2012 20:23:02
Excellent! Is there a way to control the speed at which it scrolls?
Title: Re: Continuous scrolling?
Post by: Crimson Wizard on Mon 24/12/2012 21:06:27
Quote from: FrankT on Mon 24/12/2012 20:23:02
Excellent! Is there a way to control the speed at which it scrolls?
The speed is defined by how often you change the coordinates, of course, and by which amount.
Adjust by either setting up a delay between each viewport change or amount of pixels you add every time (but for low-res game jumping over few pixels at once may look bad).
By setting a delay I do not mean Wait command (it won't do any good here, because we need continious action), but counting how much frames game passed:

Code (ags) Select

int room_scroll_delay;
function room_RepExec()
{
  room_scroll_delay = room_scroll_delay + 1;
  if (room_scroll_delay >= 4)
  {
    room_scroll_delay = 0;
    if (GetViewportX() < Room.Width / 2)
    {
      SetViewport(GetViewportX() + 1, 0);
    }
    else
    {
      SetViewport(0, 0);
    }
  }
}

This example will make it move by one pixel every 4th frame (assuming 40 frames per second - 40/4 = 10 times per second).
Title: Re: Continuous scrolling?
Post by: FrankT on Tue 25/12/2012 19:15:10
Excellent, thank you very much for that!