SCRIPTING INSTRUCTIONS FOR MULTI-IMAGE NPC'S

Now you've got your image, how do you make Graal read the right part of it at the right time?

Well, here's how.  Let's say that your frame width equals 80 pixels, and your frame height also equals 80 pixels.  (Square frame -- yay)  Let's also assume you want the image to be an actual NPC rather than a showimg.

This converts to 5 tiles by 5 tiles in Graal, if you're interested.

So.........

First, the "duh" stuff.  Put a blank NPC on your level and choose the image file you already created as the image filename.

Second, click "Action" to edit the script.  Go ahead and delete the playerenters and playertouchsme "if" blocks.  You don't need them at this stage, if indeed you ever will (depends on your NPC).

Enter the following script:

if (created) {
    setimgpart yourimagename,160,0,80,80;  
//remember, we assumed you had frames 80 pixels square.  This sets the NPC to be standing still, facing south.
   
this.sprite=0;  //should be the idle sprite, or else the first walking sprite
    dir=2;
    timeout=.05 
//begins rest of script
}

Now you need a way of adjusting the image to match the NPC's sprite and direction.  Guess what?  It's easy!  Multiply dir * framewidth to get the starting point for x, and this.sprite * frameheight to get the starting point for y.

if (timeout) {
    setimgpart yourimagename,dir*80,this.sprite*80,80,80;
}

And then there's the problem with modes.  How do you make the NPC show the images for the other mode?  Well, change the (created) block a bit:

if (created) {
    setimgpart yourimgname,160,0,80,80;
    this.sprite=0;
    dir=2;
    this.mode=0;
    timeout=.05;
}

..........and add another variable in the setimgpart line in the (timeout) block:

if (timeout) {
    setimgpart yourimgname,(dir*80) + (this.mode*4*80),this.sprite*80,80,80;
}

This sets the starting point for x all the way over on the second-mode area of the image.

Now script the rest of the NPC, and as you change directions, sprites, and modes, the NPC should display properly.

To make the NPC walk:

I should have put these instructions in sooner:

Set a variable or flag or something to show when the NPC is walking, like this.walking=1 when moving or this.walking=0 when standing still.

Put another variable, this.minisprite, in the if(created){} block and set it equal to 0.

Now put this INSIDE the if(timeout){} block, BEFORE the setimgpart command:

if(this.walking=1){
    this.minisprite++; 
//  "this.minisprite" slows the animation down.  See the next line.
    if(this.minisprite>4){ 
//  The "4" can be whatever you want.  The higher the number, the slower the animation.
        this.sprite++;
        this.minisprite=0;
    }
    if(this.sprite>3){  
//  Sprites are numbered from the top to the bottom, starting with 0.  "3" in this line is the number of the last walking sprite.
        this.sprite=0;  
//  "0" is the number of the first walking sprite.
    }
}

And, finally, add the line timeout=.05; in the if(timeout){} block AFTER all the other commands.

Whenever you want the NPC to start walking, set this.walking equal to 1, and the NPC will start going through the walking animation.  Of course, you'll have to make him move or he'll just walk in place.....  I'll let you figure that out, it's not hard.
back to image instructions
Back to the graphic instructions
Let me know how you like my tutorials, and feel free to suggest corrections or new topics.....
AIM:  Aylad2001
Back to Graal
Back to Graal
back to image instructions
Back to the tutorials