The Movement Function
The purpose of this function is to test the location that the NPC is going to move to, and if it can move there (there is no wall present) then it moves the NPC.
 
function Movement(){ 
  MoveCheck()
  if (y<this.testy&&!onwall(x+1.5,y+3+ this.addy)) y+=this.addy
  if (y>this.testy&&!onwall(x+1.5,y+1- this.addy)) y-=this.addy
  if (x<this.testx&&!onwall(x+3+ this.addx,y+1.5)) x+= this.addx
  if (x>this.testx&&!onwall(x- this.addx,y+1.5)) x-=this.addx
}
This function, like the ones before it, starts off by calling the previous function I mentioned.  That is because in the case of Movement(), the function MoveCheck() is what tells the NPC where it needs to go, so that it can be moved.  After MoveCheck() has executed we have the variables this.addx and this.addy which are the distances that the NPC is going to move in each direction.  Then I use the basic onwall detection for a player/showcharacter NPC (which is discussed in the Newbie Scripter's Bible ) and if there is no wall, then the NPC is moved.  Notice the reappearance of this.testx and this.testy , those are the baddy's target's x and y cords.
This function has to be called within a timeout loop, a.k.a. the "Control Loop", everytime you want the NPC to actually move.  So if for example the NPC is attacking within the Control Loop, then you'll have to tell it not to move.
 
Animating Your Baddy
I have found that the most simple way to animate the walking of your baddy is to throw it in the middle of this function.
Also, if you are to make your baddy face its target, you should include your direction function right there as well.
 
function Movement(){ 
  Direction() ; 
  MoveCheck();  
  sprite =(sprite% 8)+1; 
  if (y<this.testy&&!onwall(x+1.5,y+3+this.addy)) y+=this.addy;  
  if (y>this.testy&&!onwall(x+1.5,y+1-this.addy)) y-=this.addy;  
  if (x<this.testx&&!onwall(x+3+this.addx,y+1.5)) x+=this.addx;  
  if (x>this.testx&&!onwall(x-this.addx,y+1.5)) x-=this.addx;  
}
The first difference here is that I included a Direction() function.  That function has its own page, so read about it there.  Secondly, there is the blue sprite stuff. The % operator is difficult and confusing, but I can explain what is doing here: a %b=a-int( a/b)* b.  SO sprite %8 will take the integer of the current sprite divided by 8, multiply that by 8, and subtract this value from the current sprite.  When all that is done, 1 is added to that value (by the script, not by the % operator).  So if the current sprite were 2, then int(2/8)*8=0, 2-0+1=3.  So that obviously advances the current sprite by one, and the walking sprites are 1 through 8.  So what happens when sprite =8? int(8/8)*8=8, 8-8+1=1.  So it automatically resets the current sprite back to 1 to start the whole process over.  Pretty neat huh?
You should probably use this as your movement function if you want the baddy to be animated, and to actually look at its target.
 
Previous Topic         Home           Next Topic