Animating an NPC in the Background while having Player control?

Started by GameMaker_95, Thu 01/01/2015 03:58:40

Previous topic - Next topic

GameMaker_95

Hi,

I have a background character named Kate who I would like to walk from one spot to another, repeatedly. The code I have does this -

Code: ags

function room_RepExec()
{
  Kate.Walk(214, 356, eBlock, eWalkableAreas);
  Kate.Walk(470, 351, eBlock, eWalkableAreas); 
}


But the problem is, I can't control the player character and I have tried eNoBlock and it does not work,
Kate will the move one frame and freeze. How can I fix this?

Thank You.

Also, I have searched the manual and the forum exhaustively and could not find anything to help solve this.

Gurok

Try something like this:

Code: ags
function room_RepExec()
{
    if(Kate.x == 214 && Kate.y == 356 || !Kate.Moving)
      Kate.Walk(470, 351, eNoBlock, eWalkableAreas);
    else
      if(Kate.x == 470 && Kate.y == 351)
        Kate.Walk(214, 356, eNoBlock, eWalkableAreas);
}


The key with eNoBlock is that things happen asynchronously. Your code will tell the character to start walking eNoBlock and then the game will keep executing the room_RepExec every frame. That's why your character appeared to be staying in place or moving one frame and freezing. The above code will tell Kate to start walking eNoBlock only when Kate reaches a particular point on the screen. This is perhaps the most direct way to handle it. You could, for instance, have a more complex system where you toggle between her walking one way or the other if she gets stuck.
[img]http://7d4iqnx.gif;rWRLUuw.gi

GameMaker_95

Thanks Gurok

I can now move my character around but Kate for some reason is walking on the spot. She is on a walkable area
so any idea how to fix that?

Edit: I am going to have to do some work in other areas of the game and come back to this, I entered the room, clicked on a fridge wich opens and
she started moving but stopped at the coordinates and didn't loop.

Gurok

Oh whoops, I hadn't tested that code. Try this:

Code: ags
function room_RepExec()
{
    if(!Kate.Moving)
      if(Kate.x == 214 && Kate.y == 356)
        Kate.Walk(470, 351, eNoBlock, eWalkableAreas);
      else
        Kate.Walk(214, 356, eNoBlock, eWalkableAreas);
}
[img]http://7d4iqnx.gif;rWRLUuw.gi

GameMaker_95

Perfect!

Thank you very much Gurok, you've been a huge help, I can use this code for any other characters moving in the background.

Thanks again.

SMF spam blocked by CleanTalk