Really basic room scrolling, with cursor keys.

Started by , Mon 24/04/2006 11:00:32

Previous topic - Next topic

FractalCore

How do I make a room scroll using the arrow keys. I know about SetViewport and ReleaseView, but I can't get them to work. I followed several posts that explained it. After copying and pasting the code in, it would tell me every time that some line in the script was wrong. Is there a REALLY basic, foolproof way to get it to work... Using current, up to date code, I've got 2.71.

Ashen

Could you post the code you've tried, and the actual error message you get?
This should work:
Code: ags

//on_key_press
if (keycode == 375) { // Left Arrow
  SetViewport(GetViewportX() - 1, GetViewportY());
}
else if (keycode == 377) { // Right Arrow
  SetViewport(GetViewportX() + 1, GetViewportY());
}


You might need to add a check to makesure it doesn't try to exceed the room width (although I think AGS handles this internally).

But still post your code & error message.
I know what you're thinking ... Don't think that.

FractalCore

Now that code worked! I tried about three others in the same place, which didn't. Some were IsKeyPressed and some must have been On_Key_Press, which I believe is different.

Now if you could just paste the rest of it (up and down), this task will be done. As reversing the values doesn't work. I found out about Keycodes 372 and 380 for up and down arrows. This is what I tried...

if (keycode == 372) { // Up Arrow
  SetViewport(GetViewportY() + 1, GetViewportX());
}

And it didn't work. I did change the "else if" to just an "if". It didn't work either way. Also, any way to not have it jump after initially holding down the key, then scrolling? It would work better if it would just instantly start scrolling. I know it's the key repeat thing, which is why it wouldn't surprise me if it's not fixable.

Gilbert

I'm too lazy to explain, so I just muck up this little demo (for V2.71), so you may just load up room1 and examine its code and get some ideas.

Controls in demo:
Hit space to toggle mode (either scroll by keyboard or just release it to scroll with the player), if in keyboard mode, use arrows to scroll.

Ashen

Haven't tried Gilbot's demo, but I'd just like to point out:
SetViewport(GetViewportY() + 1, GetViewportX());

SetViewport (and all coordinate based functions) always uses (x,y) so this'd be setting the viewport's X-coord to GetViewportY()+1, not the Y-coord.

Reversing them should work, but also note that (0,0) is taken as the TOP left of the room, so to scroll up you'll need to decrease the Y-coord:
Code: ags

if (keycode == 372) { // Up Arrow
  SetViewport(GetViewportX(), GetViewportY()-1);
}
if (keycode == 380) { // Down Arrow
  SetViewport(GetViewportX(), GetViewportY()+1);
}


Can't help you with the 'jumping' thing (unless fixing the code has sorted it), but hopefully Gilbot already did.
I know what you're thinking ... Don't think that.

FractalCore

Thanks guys, they both worked well. I see that I was putting the +/- number in the wrong place. Now, I would only ask if there's a way to scroll diagonally while holding two keys at once, but I feel I may be pushing it :)

Gilbert

Yes, you can but then you need to use the function IsKeyPressed() to detect it as on_key_press() can only have one keycode at a time.
You may use IsKeyPressed() to detect additional key press inside on_key_press(), but to make stuff more tidy, you may do that in the repeatedly execute event.
Again, due to my laziness, just see this example.

P.S. I missed some if {} checking in my last demo, so the arrow keys actually works even when scrollmode was false, but I think that's not important anyway.

Candle

I was going to use this code as it works but if you use it then when the player walks to the side it won't scroll ?
Code: ags
//on_key_press
if (keycode == 375) { // Left Arrow
  SetViewport(GetViewportX() - 1, GetViewportY());
}
else if (keycode == 377) { // Right Arrow
  SetViewport(GetViewportX() + 1, GetViewportY());
}

Gilbert

It's advisible to check the coordinates so the ViewPort won't be out of bound (I'm not sure but I think if you set the ViewPort to something out-of-bound it may generate an error or the ViewPort would be set the farest possible to the edges), just check my demos and look at the codes.

I don't quite understand what you meaned, if you meaned something like, when the screen scrolled to the left most position you can't scroll further, than what's the problem? It can't scroll outside of teh room area anyway.


FractalCore

Gilbot, again, that script is a masterpiece, I see how the use of IsKeyPressed lets it scroll instantly, including doing diagonals, I increased the values from 1 up to 5, otherwise that's a perfect piece of coding. Kudos again :)

Candle

I'm sorry , what I mean if your player is walking to the left or right and you stop and use the left or right keys and then try to have the player walk he won't scroll the screen even if there is more room for him to walk to left or right. see what I mean.

Ashen

I think that's taken care of, if you download Gilbot's second demo.

To make the screen follow the player again after using SetViewport(x,y) you need to call ReleaseViewport() somewhere in the script. In Gilbot's demos you can toggle between keyboard controlled and character-centred with the space bar, which calls ReleaseViewport() when needed. Where you'd put it depends on how you want to handle it.
Perhaps something like checking the player.Moving property would do - if the character's moving, centre the view on them, if not you can scroll with the cursor keys. Alternatively, a check in on_mouse_click (e.g. if (mouse.Mode == eModeWalkto) ReleaseViewport();) might be easier.
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk