Needing help with Random NPC walking and leaving the screen.

Started by askalarium, Sat 28/12/2013 00:22:48

Previous topic - Next topic

askalarium

Hey All
I'm half way through my game I'm working on and can't seem to get my code to work.

I would like to have random characters move across the screen then return back to room 0. Where they can be called again.
I have 4 of them walking but when they reach the edge of the screen they just stop moving and do not return to room 0.

Code: ags
 function room_Load()
{
SetTimer(1, 400);
}

function room_RepExec()
{
if (Peopleon >1) {
int i;
if (IsTimerExpired(1)) {i = Random (4);
if (i == 0) {

if (Walker1 <1) {  
Walker1 =(2);
cWalker1.ChangeRoom(13, 0, 223);
cWalker1.Walk (319, 223, eNoBlock, eAnywhere);
if (cWalker1.x > 316) {cWalker1.ChangeRoom(0, 50, 50);
Walker1 =(0);}
}
}

if (i == 1) {
if (Walker2 <1){  
Walker2 =(2);
cWalker2.ChangeRoom(13, 0, 225);
cWalker2.Walk (319, 225, eNoBlock, eAnywhere);
if (cWalker2.x > 316) {cWalker2.ChangeRoom(0, 50, 50);
Walker2 =(0);}
}
}

if (i == 2) {
if (Walker3 <1){  
Walker3 =(2);
cWalker3.ChangeRoom(13, 319, 220);
cWalker3.Walk (0, 220);
if (cWalker3.x < 1) {cWalker3.ChangeRoom(0, 50, 50);
Walker3 =(0);}
}
}

if (i == 3) {
if (Walker4 <1){  
Walker4 =(2);
cWalker4.ChangeRoom(13, 319, 217);
cWalker4.Walk (0, 217);
if (cWalker4.x < 1) {cWalker4.ChangeRoom(0, 50, 50);
Walker4 =(0);}
}
}

SetTimer(1, 400);}

}
}


any help would be great. This is my first game so sorry if it's obvious.

Khris

The problem here, apart from the (non-existent) indentation, is that a line like this:
Code: ags
if (cWalker1.x > 316) ...

will only perform the check right when it is executed, not at an arbitrary later time.

This this:
Code: ags
function room_RepExec()
{
  if (Peopleon > 1) {
    int i;
    
    if (IsTimerExpired(1)) {
      // pick random walker, start them walking
      i = Random (4);
    
      if (i == 0) {
        if (Walker1 < 1) {
          Walker1 = 2;
          cWalker1.ChangeRoom(13, 0, 223);
          cWalker1.Walk (319, 223, eNoBlock, eAnywhere);
        }
      }

      if (i == 1) {
        if (Walker2 < 1) {
          Walker2 = 2;
          cWalker2.ChangeRoom(13, 0, 225);
          cWalker2.Walk(319, 225, eNoBlock, eAnywhere);
        }
      }

      if (i == 2) {
        if (Walker3 < 1) {
          Walker3 = 2;
          cWalker3.ChangeRoom(13, 319, 220);
          cWalker3.Walk(0, 220);
        }
      }

      if (i == 3) {
        if (Walker4 < 1) {
          Walker4 = 2;
          cWalker4.ChangeRoom(13, 319, 217);
          cWalker4.Walk(0, 217);
        }
      }

      SetTimer(1, 400);
    }
    
    // outside IsTimerExpired, check for walker reaching edge of screen
    if (Walker1 == 2) {
      if (cWalker1.x > 316) {
        cWalker1.ChangeRoom(0, 50, 50);
        Walker1 = 0;
      }
    }
    if (Walker2 == 2) {
      if (cWalker2.x > 316) {
        cWalker2.ChangeRoom(0, 50, 50);
        Walker2 = 0;
      }
    }
    if (Walker3 == 2) {
      if (cWalker3.x < 1) {
        cWalker3.ChangeRoom(0, 50, 50);
        Walker3 = 0;
      }
    }
    if (Walker4 == 2) {
      if (cWalker4.x < 1) {
        cWalker4.ChangeRoom(0, 50, 50);
        Walker4 = 0;
      }
    }
  }
}

askalarium


SMF spam blocked by CleanTalk