Objects that move while character moves

Started by trothmaster, Fri 01/06/2012 00:28:27

Previous topic - Next topic

trothmaster

Hello all, I have researched this ALL day and come up with a solution, however I feel like it is VERY inefficient...could you guys possibly suggest a better way to implement this:  Thanks in advance, this is a great forum!

A foreground object moves slightly while the character also moves..

Code: ags


 //Foreground Trees
int xposgl = oTrees.X;
int yposgl = oTrees.Y;
int bikview = cBik.Loop;
while (cBik.Moving)
{
  if (bikview == 1){
    oTrees.Move(xposgl, yposgl, 15, eNoBlock, eAnywhere);
   Wait(15);
  xposgl--;
  }
  else if (bikview == 2) {
    oTrees.Move(xposgl, yposgl, 15, eNoBlock, eAnywhere);
  Wait(15);
  xposgl++;
  }


Bik - Eat some sandwiches and save the galaxy!
www.bikgame.com

Khris

You want simple parallax scrolling, right? As in, a foreground object in a scrolling room is supposed to scroll slightly faster than the room background to create depth?
To achieve that effect, the object's position has to move in relation to the viewport's x offset.

The following code will line up the room's and object's left and right edges and interpolate in between:

Code: ags
// inside room_RepExec

  // set foreground object
  Object*o = oTrees;

  // viewport x range: 0 to
  int vpx_max = Room.Width - System.ViewportWidth;

  // object x range: 0 to
  int ox_max = Room.Width - Game.SpriteWidth[o.Graphic];  // actually a negative value

  o.X = (GetViewportX() * ox_max) / vpx_max;


Note that this will still work even if the room is wider than the foreground object; in that case though the effect suggests that the camera is static and turns to follow the character.

trothmaster

Thank You so much for your reply!  I did not know it was called parallax scrolling! 
I'm going to try to implement this myself for practice but I also did a quick search and found this thread which might help...
http://www.adventuregamestudio.co.uk/yabb/index.php?topic=33142.0
Bik - Eat some sandwiches and save the galaxy!
www.bikgame.com

Caracal

One qestion to Khris:
// set foreground object4.
Object*o = oTrees;
Does this replace the oTree in the scripting lines you mentioned here?
I would be interested in this parallax moving but i dont know how to use (apply on my game) the module that trothmaster found. (i just simply dont dare to open a tread about "how to use modules" running the danger that this might have been asked before)Therefore i need to understand this!

Khris

Since throthmaster called his room object "oTrees", I used that line to make a new Object pointer called "o" pointing to oTrees.
The two advantages are that I can now use "o" instead of "oTrees" in the rest of the code, and that somebody else with a differently named object only has to adjust the code in one spot.
It works the same way as the Character pointer "player".
If your object's ID is 4, you can use
Code: ags
  Object*o = object[4];

Or the script name.

As for your module question, you right-click the Scripts node and select "Import...", then the module. That's all. To actually use the module, look at its documentation or its header. One of them should list the available new commands and instructions about general usage.

There's also a manual section about modules: http://www.adventuregamestudio.co.uk/manual/ScriptModules.htm

Quote from: Caracal on Mon 02/07/2012 10:40:47(i just simply dont dare to open a tread about "how to use modules" running the danger that this might have been asked before)
Me likey. There's a search function though which you can use. If you can't find an older thread that answers your question, feel free to open a new one. It can be difficult to find a relevant thread in some cases, but I did a search for "using modules" and the first hit I got was this: http://www.adventuregamestudio.co.uk/yabb/index.php?topic=44728.msg597905#msg597905

Caracal

Quote from: Khris on Mon 02/07/2012 11:01:53
As for your module question, you right-click the Scripts node and select "Import...", then the module. That's all.

Only one more qestion: Which kind file does the module has to be? I downloaded it and saved it on my desktop as "txt"-file but when i open the "Import" window in AGS (after rightklick on the script in the actiontree) it wont show anything but the empty folders.

SMF spam blocked by CleanTalk