Room scrolling (SOLVED)

Started by paolo, Wed 12/09/2007 19:13:29

Previous topic - Next topic

paolo

I'm using the code Scorpiorus wrote way back for scrolling the viewport (updating it for my version of AGS).

What I want to do is scroll the screen to the right to show something and then scroll it back to the original position.

Here are my function calls to his script:

int distance_to_scroll = 40;

//scroll right
MoveViewportBlocking(distance_to_scroll, GetViewportY(), 1, 0);

//do what I want to do here

//scroll back to left
MoveViewportBlocking(-distance_to_scroll, GetViewportY(), 1, 0);
ReleaseViewport();


Now, the scroll to the right works fine (the viewport being initially aligned with the left edge of the room), but not the scroll back to the left. The call to scroll to the left does nothing, and then the ReleaseViewport() makes the viewport snap back to where it was to start with.

I've tried various values for the first parameter in the second call to MoveViewportBlocking (including 0 and -system.viewport_width / 2) but the result is always the same.

For reference, here is my revised version of Scorpiorus's code. Note in particular that I've taken the !! out before the antiglide parameter in the call to SetGameOption(), as this would not compile, and not not X == X in any case. Or did that mean something else?

function MoveViewportBlocking(int x, int y, int speed, int antiglide)
{
   int dummy = NR;   //my narrator character

   character[dummy].ChangeRoom(cVince.Room);
   character[dummy].x = GetViewportX() + system.viewport_width / 2;
   character[dummy].y = GetViewportY() + system.viewport_height / 2;
   character[dummy].SetAsPlayer();
   character[dummy].SetWalkSpeed(speed, speed);

   antiglide = SetGameOption(OPT_ANTIGLIDE, antiglide);

   character[dummy].Walk(x + system.viewport_width / 2, y + system.viewport_height / 2, eBlock, eAnywhere);
   
   cVince.SetAsPlayer();
   SetViewport(GetViewportX(), GetViewportY());
   SetGameOption(OPT_ANTIGLIDE, antiglide);
}

Can anyone help, please?

Ashen

#1
I'd say the second call doesn't do anything because the Viewport is locked (the SetViewport(GetViewportX(), GetViewportY()) line). Adding ReleaseViewport() after character[dummy].SetAsPlayer(); should sort that - the character is centered in the screen, so the position won't change, but it'll be able to follow them like it should.

Although this has nothing to do with your problem, you might want to replace this line:
Code: ags

character[dummy].Walk(x + system.viewport_width / 2, y + system.viewport_height / 2, eBlock, eAnywhere);


with:
Code: ags

character[dummy].Walk(character[dummy].x + x, character[dummy].y + y, eBlock, eAnywhere);


It won't make any difference if you always scroll from the left-most edge of the room, but if you want to start and end the scroll somewhere else, the way you've got it might mess up - an x value of 40 will always walk the character to 200 (40 + 160 in a 320 or 640 width game, 40 + 200 in 800X600), and not 40 pixels right of their starting point. (Try it out, I don't know how much sense that made ...)

Also, if you're always using the same character for the scroll (NR), you don't need to use the dummy variable. character[NR] or cNr (assuming that's the right script-o-name) will work.

EDIT:
And for other people's reference: system.viewport_height/width are obsolete in V2.72, replaced with System.ViewportHeight/Width.
I know what you're thinking ... Don't think that.

RickJ

Try putting in a wait statement before you release the view port. 

//scroll back to left
MoveViewportBlocking(-distance_to_scroll, GetViewportY(), 1, 0);
Wait(10);
ReleaseViewport();

monkey0506

Rick...you do know that a "blocking" function should block the script from running, meaning he shouldn't have to call a Wait after the block, right? :P

paolo

#4
Ashen, that worked beautifully. Thank you. My ReleaseViewport() was in the wrong place.

And using a negative number to scroll back to the left was of course correct.

EDIT: There is an unwanted side-effect: after the viewport scrolls back to the left, hiding the right-hand part of the room, if my player character (cVince) walks over to the right, the viewport doesn't update automatically as he approaches the right-hand side.

So something is still not quite right.

Ashen

Sorry, I should've mentioned - you still need to call ReleaseViewport() after the scroll back to the left. The one in the function unlocks it so it can follow character[dummy], but it's locked again by the SetViewport line (basically the same reason it wasn't following the character back to the left, is why it's not following Vince now). An extra ReleaseViewport() after all the scrolls are done will return it to normal.
I know what you're thinking ... Don't think that.

paolo

I wondered if I might need another ReleaseViewport(). Thank you - it's all working now!

Scorpiorus

Quote from: paolo on Wed 12/09/2007 19:13:29
int distance_to_scroll = 40;
//scroll right
MoveViewportBlocking(distance_to_scroll, GetViewportY(), 1, 0);

The original function expects absolute values for viewport x and y co-ordinates.
So either change the function script to make it relative to the current position, as Ashen suggested, or call it like this:

MoveViewportBlocking( GetViewportX() + distance_to_scroll, GetViewportY(), 1, 0 );


Quote from: paolo on Wed 12/09/2007 19:13:29For reference, here is my revised version of Scorpiorus's code. Note in particular that I've taken the !! out before the antiglide parameter in the call to SetGameOption(), as this would not compile, and not not X == X in any case. Or did that mean something else?

It was just to ensure the value of antiglide to pass into SetGameOption is either 0 or 1 as it expects according to the manual, even if the originally passed value somehow is not 1 but, say, 5. So (!!1 = 1 ) and ( !!5 = 1 ). It's quite surprising this doesn't compile anymore.

:)

SMF spam blocked by CleanTalk