Jibble

Author Topic: Help: How To Script Scrolling Background (Simulated Flying) [SOLVED]  (Read 184 times)

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: Adventure Game Studio
  1. function room_RepExec()
  2. {
  3.   if (oBackground.X > -Room.Width)
  4.   {
  5.     oBackground.X = oBackground.X - 1;
  6.   }
  7.   else
  8.   {
  9.     oBackground.X = 0;
  10.   }
  11. }
  12.  

I have also tried the code from https://www.adventuregamestudio.co.uk/forums/index.php?topic=45566.0:
Code: Adventure Game Studio
  1. // in room_RepExec
  2.  
  3.   Object*o = oScrollingBackground;
  4.   if (o.Visible) {
  5.     o.X -= 1;    // speed
  6.     if (o.X == -320) o.X = 0;
  7.   }
  8.  

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

« Last Edit: 30 Apr 2019, 18:58 by Clist186 »

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

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?

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.

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?

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:

  • Created a room for the scene, Room 101
  • Loaded a 640x200 image as a sprite into AGS for the background (sky & stars)
  • Named the object 'oBackground'
  • Set the object to start at X0 Y200
  • Loaded my ship object 'oShip' and placed it in centre
  • Tried both sets of code used in first post (separately)
  • Ran game. Room loaded, objects loaded, 'oBackground' did not scroll.

Trying to figure out what I'm missing in this sequence of events.  :cry:

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: Adventure Game Studio
  1. function room_RepExec()
  2. {
  3.   Display("I am called!");
  4.  ....
  5. }
  6.  

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: Adventure Game Studio
  1. function room_RepExec()
  2. {
  3.   Display("I am called!");
  4.  ....
  5. }
  6.  

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

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: Adventure Game Studio
  1. function room_RepExec()
  2. {
  3.   Display("I am called!");
  4.  ....
  5. }
  6.  

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