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. |
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? |