GameMaker 2中的可变问题

时间:2019-03-15 22:19:08

标签: game-maker-language

在GameMaker中进行游戏时,我遇到了一个问题,即未设置变量,我不确定为什么。当按下W,A,S或D并单击LMB时,它可以工作,但是如果我按下W,A,S或D然后放开然后单击LMB,则崩溃。

代码:

var look
var bullet
if (keyboard_check(ord("A"))){
     x = x - 5;
    sprite_index = spr_west_hiro
    image_xscale = 3
    image_yscale = 3
    look = 180

} else if (keyboard_check(ord("D"))){
    x = x + 5;
    sprite_index = spr_east_hiro
    image_xscale = 3
    image_yscale = 3
    look = 0

} else if (keyboard_check(ord("S"))){
    y = y + 5;
    sprite_index = spr_south_hiro
    image_xscale = 3
    image_yscale = 3
    look = 270

} else if (keyboard_check(ord("W"))){
    y = y - 5;
    sprite_index = spr_north_hiro
    image_xscale = 3
    image_yscale = 3
    look = 90

}
//------------------------------------------------------------------------------
if (keyboard_check_released(ord("A"))){
    sprite_index = spr_idlewest_hiro
    image_xscale = 3
    image_yscale = 3
    look = 180

} else if (keyboard_check_released(ord("D"))){
    sprite_index = spr_idleast_hiro
    image_xscale = 3
    image_yscale = 3
    look = 0

} else if (keyboard_check_released(ord("S"))){
    sprite_index = spr_idlesouth_hiro
    image_xscale = 3
    image_yscale = 3
    look = 270

} else if (keyboard_check_released(ord("W"))){
    sprite_index = spr_idlenorth_hiro
    image_xscale = 3
    image_yscale = 3
    look = 90

}
//--------------------------------------------------------------------------------
if (mouse_check_button_pressed(mb_left)){
    var bullet = instance_create_layer(x,y, "Instances", obj_bullet)
    bullet.direction = look
}

错误:

ERROR!!! :: ############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object obj_hiro:


local variable look(100001, -2147483648) not set before reading it.
 at gml_Object_obj_hiro_Step_0 (line 61) -  bullet.direction = look
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_hiro_Step_0 (line 61)

我已经多次检查了代码,但仍然感到困惑。尤其是因为尽管在按下W,A,S或D之后应该释放该变量,但该变量似乎并没有保存设置给它的系数。

1 个答案:

答案 0 :(得分:0)

首先,您要为look变量分配一个值,否则,如果没有按下任何按钮,它将不会设置为任何值。

第二,您可能希望在Create事件中这样做,因为您可能希望角色朝最后按下的方向射击,而不仅仅是“某个地方”