Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Babar on Mon 31/10/2005 23:53:47

Title: Pause before walking
Post by: Babar on Mon 31/10/2005 23:53:47
I am trying to make it so that a (loopable) room is always scrolling with the character going along with it. When the player presses an arrow key, the character moves in that direction. The problem is that for some reason, you have to press the arrow key a long while before the character starts moving. This happens right at the beginning, and then (randomly?) while moving around.

Here is the code. All the code I used was placed in Rep_ex:


function room_a() {
  // script for Room: Repeatedly execute
cEgo.y--;
SetViewport(0, GetViewportY()-1);
if (GetViewportY()==0) {
  cEgo.y+=400;
  SetViewport(0, 400);
}

if (IsKeyPressed(375)==true) {
  if (GetWalkableAreaAt(cEgo.x-1, cEgo.y)==1) cEgo.x--;
}

if (IsKeyPressed(377)==true) {
  if (GetWalkableAreaAt(cEgo.x+1, cEgo.y)==1) cEgo.x++;
}

if (IsKeyPressed(380)==true) {
  if (GetWalkableAreaAt(cEgo.x, cEgo.y+1)==1) cEgo.y++;
}

if (IsKeyPressed(372)==true) {
  if (GetWalkableAreaAt(cEgo.x, cEgo.y-1)==1) cEgo.y--;
}

}


I had substituted IsKeyPressed for on_key_press, but it didn't help.
Title: Re: Pause before walking
Post by: Elliott Hird on Tue 01/11/2005 10:08:08
Well, that won't react to presses, it checks every key if it's held down, normally a tap will escape it.
Title: Re: Pause before walking
Post by: Gilbert on Tue 01/11/2005 10:14:54
Hmmm is it possible that the character is walking sometimes? Changing the character's coordinates directly may not work if he's moving.
Title: Re: Pause before walking
Post by: Babar on Tue 01/11/2005 10:23:11
I can't see how he could be walking. I don't make him walk. Besides that bit of code in Rep_execute, I haven't scripted a single thing
Title: Re: Pause before walking
Post by: Gilbert on Tue 01/11/2005 10:41:59
Are there codes in on_key_pressed() that detects these keys? though the chance is small, it may interfere the detection.

try also putting these codes in repeatedly_execute_always() (just create that function yourself in the room's script, it's not in the interaction editor so it won't be created automatically by the editor) instead, maybe the game's blocked for some reasons sometimes.
Title: Re: Pause before walking
Post by: Babar on Tue 01/11/2005 10:49:33
Nothing in on_key_pressed() that detects arrow keys. I figured out that the problem started when I inserted the walkablearea detection. Before that, it moved smoothly.

I had a slightly complicated walkable area, with lots of curves in it. Now I've sacrificed it in favour of (cEgo.x>60) and (cEgo.x<250). It works fine now, even if it is not really what I wanted. If anyone figures out what the problem was with GetWalkableAreaAt, they can tell me, but at least it works now.
Title: Re: Pause before walking
Post by: SSH on Tue 01/11/2005 10:51:14
Ahah, I should have RTFM when I was helpign you on #ags  ;)

Quote
GetViewportY ()

Returns the Y-offset of the current viewport in a scrolling room. This allows you to find out what part of the room the player is looking at. The co-ordinate returned is the top edge of the screen, and so it can have a value between 0 and (ROOM HEIGHT - 200).

So change the code to:


if (IsKeyPressed(375)==true) {
  if (GetWalkableAreaAt(cEgo.x-1, cEgo.y-GetViewportY())==1) cEgo.x--;
}

if (IsKeyPressed(377)==true) {
  if (GetWalkableAreaAt(cEgo.x+1, cEgo.y-GetViewportY())==1) cEgo.x++;
}

if (IsKeyPressed(380)==true) {
  if (GetWalkableAreaAt(cEgo.x, (cEgo.y+1)-GetViewportY())==1) cEgo.y++;
}

if (IsKeyPressed(372)==true) {
  if (GetWalkableAreaAt(cEgo.x, (cEgo.y-1)-GetViewportY())==1) cEgo.y--;
}


obviously, you want to check that you're not going off the top/bottom of the visible screen, too and you may need to bump your character back into the middle again if you have irregular edges to the walkable area: the scrolling might make your char move off the walkable and get stuck if he's right at the edge of a wide bit then goes to a narrow bit

Title: Re: Pause before walking
Post by: Gilbert on Tue 01/11/2005 10:55:14
Hehe yeah, even I forgot about the room VS screen coordinates problem. ;)
Title: Re: Pause before walking
Post by: Babar on Tue 01/11/2005 10:58:09
Works! Many thanks.

/me  hits himself for not RTFM either

And don't worry about going off the walkableareas. Crashing into walls is all going to be factored in.