Help: How To Script Scrolling Background (Simulated Flying) [SOLVED]

Started by Clist186, Wed 17/04/2019 17:58:16

Previous topic - Next topic

Clist186

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

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

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


CaptainD

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

Clist186

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?

CaptainD

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.
 

Crimson Wizard

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?

Clist186

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:


  • 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:

Crimson Wizard

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

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

Clist186

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

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

Clist186

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

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


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

SMF spam blocked by CleanTalk