Gamemaker AI 自动转向和躲避子弹

分类栏目:gamemaker教程

314

敌人自动转向

objAI:

Step://步事件

image_angle = direction

speed = 1

Collision<block>://碰撞事件

direction += 5

躲避子弹(zpw759)

obj_playerAI

step://步事件

if distance_to_object(bullet)<30

{

    safe=0

}

else

{

    safe=1

}

 

if safe==1 

{

    x+=lengthdir_x(2,point_direction(x,y,flag.x,flag.y));

    y+=lengthdir_y(2,point_direction(x,y,flag.x,flag.y));

    if (point_distance(x,y,flag.x,flag.y)<3) //prevents shaking.

    {

        x=flag.x;

        y=flag.y;

    }

}

else

{

    threat=instance_nearest(x,y,bullet);

    targetx=x+lengthdir_x(30,threat.direction+90);

    targety=y+lengthdir_y(30,threat.direction+90);

    x+=lengthdir_x(3,point_direction(x,y,targetx,targety));

    y+=lengthdir_y(3,point_direction(x,y,targetx,targety));

}

bullet

create://创建事件

direction=point_direction(x,y,ai.x,ai.y);

speed=8;

flag

create://创建事件

alarm[0]=1;

alarm0:

instance_create(random(room_width),random(room_height),bullet);

alarm[0]=20