Adventure Game Studio | Forums

AGS Support => Beginners' Technical Questions => Topic started by: Clist186 on Wed 17/04/2019 17:58:16

Title: Help: How To Script Scrolling Background (Simulated Flying) [SOLVED]
Post by: Clist186 on Wed 17/04/2019 17:58:16
Hey AGS Community,

Can someone please explain to me in simple layman's terms, how I would script a scene where a spaceship is flying through space. The spaceship is to stay stationary while the starry background slowly moves across the screen. I would prefer to have it set up for infinite looping scroll (until the room changes) but I can make do with a maximum length.

My game is 320x200 and I have created a background of 1700x200 to prepare for the scroll.

I have tried the method in https://www.adventuregamestudio.co.uk/forums/index.php?topic=47310.0:
Code (ags) Select

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


I have also tried the code from https://www.adventuregamestudio.co.uk/forums/index.php?topic=45566.0:
Code (ags) Select

// in room_RepExec

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


Neither of these scripts scrolled the background. Everything remained stationary. I can't figure out where I'm going wrong.

Title: Re: Help: How To Script Scrolling Background (Simulated Flying)
Post by: CaptainD on Wed 17/04/2019 18:23:59
Might be easiest to simply have a transparent character move through the screen, it will then scroll automatically without having to mess about with the viewport or anything. (You just move the character itself.)
Title: Re: Help: How To Script Scrolling Background (Simulated Flying)
Post by: Clist186 on Wed 17/04/2019 19:17:49
Quote from: CaptainD on Wed 17/04/2019 18:23:59
Might be easiest to simply have a transparent character move through the screen, it will then scroll automatically without having to mess about with the viewport or anything. (You just move the character itself.)

Thanks for the reply. Would I be able to keep the spaceship object at a fixed position (screen center, for example) while the background scrolls with the invisible character walking? Or would I also have to set up the spaceship object to move along the screen in tandem with the invisible character?
Title: Re: Help: How To Script Scrolling Background (Simulated Flying)
Post by: CaptainD on Wed 17/04/2019 19:21:35
Ah well if you have a space ship then make THAT a character and just move it in the Y axis.  That should achieve the desired effect.
Title: Re: Help: How To Script Scrolling Background (Simulated Flying)
Post by: Crimson Wizard on Wed 17/04/2019 20:05:41
Quote from: CaptainD on Wed 17/04/2019 19:21:35
Ah well if you have a space ship then make THAT a character and just move it in the Y axis.  That should achieve the desired effect.

Clist186 wants a looped scroll, that is the main issue I think (?).


Regarding the script examples posted above, they both are supposed to work (with certain adjustments for your particular case), but they move object, not room background itself.
Have you set the object correctly?
Title: Re: Help: How To Script Scrolling Background (Simulated Flying)
Post by: Clist186 on Wed 17/04/2019 20:28:02
Quote from: Crimson Wizard on Wed 17/04/2019 20:05:41
Quote from: CaptainD on Wed 17/04/2019 19:21:35
Ah well if you have a space ship then make THAT a character and just move it in the Y axis.  That should achieve the desired effect.

Clist186 wants a looped scroll, that is the main issue I think (?).


Regarding the script examples posted above, they both are supposed to work (with certain adjustments for your particular case), but they move object, not room background itself.
Have you set the object correctly?

Yes, a looped scroll is the preference of what I'm looking for. Here's what I did to set up the scene:


Trying to figure out what I'm missing in this sequence of events.  :cry:
Title: Re: Help: How To Script Scrolling Background (Simulated Flying)
Post by: Crimson Wizard on Wed 17/04/2019 20:37:44
Have you set the sprite as object's Image? It's missing in these steps above so I thought I'd ask just in case.

Have you correctly linked room_RepExec function to room events, on the events panel? If you just copy it to the script it won't "hook up".
It's easy to find out if you call Display inside that function.
Code (ags) Select

function room_RepExec()
{
  Display("I am called!");
....
}
Title: Re: Help: How To Script Scrolling Background (Simulated Flying)
Post by: Clist186 on Wed 17/04/2019 20:44:50
Quote from: Crimson Wizard on Wed 17/04/2019 20:37:44
Have you set the sprite as object's Image? It's missing in these steps above so I thought I'd ask just in case.

Have you correctly linked room_RepExec function to room events, on the events panel? If you just copy it to the script it won't "hook up".
It's easy to find out if you call Display inside that function.
Code (ags) Select

function room_RepExec()
{
  Display("I am called!");
....
}


Hmm...no. Nothing is displayed on screen when I add that code. And yes, I did just copy the code from another thread right into AGS. How do I properly link this to room events? I want it to start automatically when entering the scene (room).
Title: Re: Help: How To Script Scrolling Background (Simulated Flying)
Post by: Clist186 on Wed 17/04/2019 20:47:59
Quote from: Crimson Wizard on Wed 17/04/2019 20:37:44
Have you set the sprite as object's Image? It's missing in these steps above so I thought I'd ask just in case.

Have you correctly linked room_RepExec function to room events, on the events panel? If you just copy it to the script it won't "hook up".
It's easy to find out if you call Display inside that function.
Code (ags) Select

function room_RepExec()
{
  Display("I am called!");
....
}


OMG I figured it out after reading this, now it works perfectly! Thank you so much!!!