Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: themaninthehat on Mon 30/06/2014 13:23:51

Title: SOLVED: Help with vertical scroll SetViewport function
Post by: themaninthehat on Mon 30/06/2014 13:23:51
Hi all,

Newbie here, having problems setting up some script to achieve the vertical scroll I want. I've already searched and read related posts but I'm afraid I'm lost.

My game is currently set up at 1920x1080, and my room is 1920 x 1533. I want the camera to follow the character vertically, but without scripting, the scrolling activates way too late for what I want to achieve. So I want the camera to follow the character only in the Y axis.

I've tried something like this:

Code (ags) Select

// room script file
function room_RepExec(){
SetViewport (cManhat.x, cManhat.y) ;
}


This produces no scrolling at all.

Any help would be greatly appreciated.

Thanks.
Title: Re: Help with vertical scroll SetViewport function
Post by: Gurok on Mon 30/06/2014 13:31:05
SetViewport doesn't centre the coordinates you specify within the viewport. You use it to set the top/left of the viewport. You're probably finding that the viewport is stuck showing the bottom of the screen. To centre your character vertically at all times, you could try something like this:

SetViewport(0, cManhat.y - (System.ViewportHeight / 2));

Or even something like this if you want the middle of the character:

ViewFrame *frame;

frame = Game.GetViewFrame(cManhat.View, cManhat.Loop, cManhat.Frame);
SetViewport(0, cManhat.y - ((Game.SpriteHeight[frame.Graphic] + System.ViewportHeight) / 2));
Title: Re: Help with vertical scroll SetViewport function
Post by: Snarky on Mon 30/06/2014 13:39:28
To make it scroll only vertically, you can't use the character X coordinate. You also probably want the character to be in the middle of the screen, not right at the top edge. Try this instead:

Code (AGS) Select
int yCoord = cManhat.y - System.ViewportHeight/2;
if(yCoord<0) yCoord = 0;
else if(yCoord + System.ViewportHeight > Room.Height) yCoord = Room.Height - System.ViewportHeight;
SetViewport(GetViewportX(), yCoord);


Not sure why your code didn't work, but I'm guessing it's because you're setting invalid coordinates. (Or perhaps that unnecessary space before the semi-colon.)
Title: Re: Help with vertical scroll SetViewport function
Post by: themaninthehat on Mon 30/06/2014 14:37:03
Thanks both for your responses.

The first code from Gurok's post and Snarky's one both produce the same effect that I get if I leave the scrolling room with no script at all. I will include an image of my room to help me explain my issue:

(http://s9.postimg.org/b57csa8qn/Salon_Small.jpg)

So, at the moment, without scripting or with Snarky's code or Gurok's first post, I get a TINY bit of verticall scroll when I reach the red line. What I'd like is the screen to start scrolling vertically sooner, ie when the character walks above the green line. Is there any other way to achieve this rather than using SetViewport?

Gurok's second suggestion does work, the camera follows the character vertically all the time. However, the camera movement is a very twitchy or jumpy (that's the best way I can describe it) when the character walks vertically...any ideas?

Thank you very much for your time and help.



Title: Re: Help with vertical scroll SetViewport function
Post by: themaninthehat on Mon 30/06/2014 18:29:26
Just writing to say this is now resolved.

After more fiddling with the SetViewport function, I managed to get the desired scrolling with this code:

Code (ags) Select
function room_RepExec(){
SetViewport(0, cManhat.y - ((Room.Height + 500) / 2));
}


Thanks again to Gurok and Snarky for pointing me in the right direction, much appreciated.
Title: Re: Help with vertical scroll SetViewport function
Post by: Snarky on Mon 30/06/2014 18:42:52
The only real difference between the various code examples is the y-offset, or in other words where on the screen the code tries to keep the character. If it's not scrolling up "soon enough" it's because the character coordinates (usually the feet) aren't yet halfway to the top of the screen, and you simply need to subtract an offset (the distance between the green and red lines) from the y-coordinate calculation, as in Gurok's second example. (Edit: And as you have done in your latest post.)

Twitchy camera following a character? Hmmm... I think it could be a few different things, all of them more or less built into AGS. It would be possible program a "camera" that kind of floated along with the character, which perhaps might solve it depending on exactly what the problem is. Try this module (http://www.adventuregamestudio.co.uk/forums/index.php?topic=33142.0).