Mar 29, 2013

Someone on the GMC forums asked for a code to make a bat sleep and then fly at the player when the player gets close enough. Hmm. Sounds like some bat we know, doesn't it? So I typed this up for him off the top of my head, although I did glance at my notes for the values. If you're looking for intuitive code (i.e., one that uses fractions instead of hexadecimal words for speed values), then you'll probably like this. The whole code is self-contained: stick it in a script and call it during the Step Event. This is just the movement code for a sleeping bat, sans collision code.

if alarm[0]<0
    alarm[0] += 1;
switch alarm[0]
{
    case 0:
            sprite_index = spr_bat_sleep;
            image_speed = 0;
            image_index = 0;
            alarm[0] += 1;
            break;
    case 1:
            if abs(x-obj_player.x) < $60
            if abs(y-obj_player.y) < $20
                alarm[0] += 1;
            break;
    case 2:
            sprite_index = spr_bat_fly;
            image_speed = 1/10;
            image_index = image_speed;
            hspeed = 5/4;
            alarm[0] += 1;
            break;
    case 3:
            vspeed = 3/2;
            alarm[0] += 1;
            alarm[1] = 32;
            break;
    case 4:
            alarm[1] -= 1;
            if alarm[1]
                vspeed -= 1/25;
            else
                alarm[0] += 1;
            break;
    case 5:
            vspeed = -5/4;
            ystart = y;
            alarm[0] += 1;
            break;
    case 6:
            vspeed += (ystart - y) / 256;
            break;
}

1 comment:

©TheouAegis Productions™. Powered by Blogger.