Gamemaker studio2 experience (1) - Right-character logical move

Overview of issues

Red Alert, Age of Empires, StarCraft ...... must have a lot of players retain deep affection for this type of real-time strategy game. There is a more common logic - Left paddling select characters, and then right-mark target locations, making the characters move to the target location. The most common problem is that the logic - may have different forces are selected so that they are marching to different areas at the same time. To overcome this difficulty, bloggers want a set of relatively simple logic.

solution

The basic idea is very simple. First, let's define the essential meaning of the object, called a flag banner is the player left after right-click on the target point, the other logical writing code. The basic process is as follows:

  1. Create a banner object obj_mubiao. (Recommended to the object hanging a sprite, easy to debug)
  2. Code the logic selected roles.
  3. Click written in the blank area left to uncheck the logic code.
  4. Click to write in a blank area of the right to place the logic code flag.
  5. Code the logic flag.

Code

According to the above ideas, we start writing code for each object separately.

obj_zhujue

First, create

For the protagonist, to create one of their own banner.
The specific method is to create a banner and this banner id assigned to a variable mubiao in, then put yourself (id) assigned to the banner (mubiao) "master", so to nail down the relationship between the characters and their own flag

mubiao=instance_create(0,0,obj_mubiao);   //创造一个以自己为对象的旗子
mubiao.zhuren=id;                         //将旗子的对象设置为自己

Second, the left mouse button is released

The inside word, shubiao = 1 represents the selected protagonist (was not accustomed to using Boolean value table hit me ~)

shubiao=1;     //表示鼠标选中了这号角色

Third, the global left button down

There is only one sentence to indicate a blank area tap left on the abolition of all selected

if (shubiao==1) shubiao=0;    //判定:如果此时处于选中状态,则取消选中

Fourth, the global right-press

First, determine whether the character is selected, if they entered creation

if (shubiao==1 )                                    //判定右键时是否是该物体被选中
{
    move=1;                                          //开启移动模式
    zhan=0;
    move_x=mouse_x;                                  //移动目标:鼠标x值
    move_y=mouse_y;                                  //移动目标:鼠标y值
    dertax=move_x-x;                                 //横坐标远近
    dertay=move_y-y;                                 //纵坐标远近
    if (dertax!=0) k=dertay/dertax;                    //斜率
    if (dertax==0) k=dertay/(dertax+0.001);             //补救当x=0时导致的运算错误
    if (k<-1 || k>1 && dertay>0) fangxiang=1;
    if (k>=-1 && k<=1 && dertax>=0) fangxiang=2;
    if (k<-1 || k>1 && dertay<0) fangxiang=3;
    if (k>=-1 && k<=1 && dertax<0) fangxiang=4;
}

Five-step event

This is the command to move the character, difficult to understand

/*     向下移动     */
if (move==1 && fangxiang==1 && (image_index>=2)) image_index=0;
if (move==1 && fangxiang==1)
    {
    if (distance_to_point(move_x,move_y)>0.5)mp_potential_step_object(move_x,move_y,move_speed,obj_no);
    if (distance_to_point(move_x,move_y)<=0.5) 
        {
        speed=0;
        move=0;
        zhan=1;
        }
    }
        
/*     向右移动     */
if (move==1 && fangxiang==2 && (image_index<=10 or image_index>=13)) image_index=11
if (move==1 && fangxiang==2)
    {
    if (distance_to_point(move_x,move_y)>0.5) mp_potential_step_object(move_x,move_y,move_speed,obj_no);
    if (distance_to_point(move_x,move_y)<=0.5) 
        {
        speed=0;
        move=0;
        zhan=1;
        }
    }
        
/*     向上移动     */
if (move==1 && fangxiang==3 && (image_index<=1 or image_index>=4)) image_index=2
if (move==1 && fangxiang==3)
    {
    if (distance_to_point(move_x,move_y)>0.5) mp_potential_step_object(move_x,move_y,move_speed,obj_no);
    if (distance_to_point(move_x,move_y)<=0.5) 
        {
        speed=0;
        move=0;
        zhan=1;
        }
    }
        
/*     向左移动     */
if (move==1 && fangxiang==4 && (image_index<=3 or image_index>=6)) image_index=4
if (move==1 && fangxiang==4)
    {
    if (distance_to_point(move_x,move_y)>0.5) mp_potential_step_object(move_x,move_y,move_speed,obj_no);
    if (distance_to_point(move_x,move_y)<=0.5) 
        {
        speed=0;
        move=0;
        zhan=1;
        }
    }

obj_mubiao

Step event

Said that if the corresponding protagonist in the selected state, then display the banner, otherwise it will be hidden
once the protagonist arrived at the designated location, the flag disappeared

if (zhuren.shubiao=1) image_alpha=100;
if (zhuren.shubiao=0) image_alpha=0;
if collision_point(x,y,zhuren,0,0)
{
    x=0;
    y=0;
}

Global Right

There is a set end position

if (zhuren.shubiao==1)
{
	x=zhuren.move_x;
	y=zhuren.move_y;
}
if (collision_circle(x,y,1,obj_difang,1,0) && zhuren.duixiang=collision_circle(x,y,1,obj_difang,1,0))
{
    zhuren.move=0;
    zhuren.da=1;
}
else
{
    zhuren.chetui=1;
}
if collision_point(x,y,1,obj_no,1)
{
	x=0;
	y=0;
}
发布了8 篇原创文章 · 获赞 1 · 访问量 331

Guess you like

Origin blog.csdn.net/weixin_42921101/article/details/100087409