GameMaker平台游戏敌人垂直碰撞失效及逻辑颠倒问题求助
GameMaker平台游戏敌人碰撞问题
- 水平碰撞功能正常,但垂直碰撞完全失效
- 调试消息逻辑颠倒:碰撞墙体时显示“colliding with the floor”,碰撞地面时显示“colliding with the wall”
- 敌人触碰墙体未停止移动,反而穿墙而过
- 已排查碰撞掩码、变量、代码顺序、对象名称,未解决问题
敌人Create事件AI代码
//BIGGER SPRITE image_xscale = 3 image_yscale = 3 //VARIABLES hsp=0 vsp=0 grav=2 state=noone state_time = room_speed * 10; state_timer=0; destination_x = x; vel=2 //STANDING STATE //WALKING STATE //METHOD OF CHANGE OF STATE change_state = function(_state) { state_time--; state_timer = irandom(state_time); if (state_timer == state_time or state_time <= 0) { //I'LL CHECK WHICH STATE SHOULD I GO state = _state[irandom(array_length(_state)-1)]; time_state = room_speed * 10; } } //METHOD OF IT STANDING standing_state = function() { //image_blend = c_white; sprite_index = spr_enemy change_state([walking_state, standing_state]); } walking_state = function() { //MAKE HIM WALK //HE WILL CHOOSE ANY POINT WITHIN THE ROOM //CHECKING MY DISTANCE FROM THE DESTINATION var _dist = point_distance(x, y, destination_x, y); //I WILL ONLY SET A DESTINATION IF I AM CLOSE TO MY DESTINATION if (_dist < 100) { destination_x = random(room_width); } //FINDING THE DIRECTION TO MY DESTINATION var _dir = point_direction(x, y, destination_x, y) //MOVING TO MY DESTINATION x += lengthdir_x(vel, _dir); //image_blend = c_red; //SETTING THE SPRITE sprite_index = spr_enemy_run if (destination_x < x) { image_xscale = -3 } else { image_xscale = 3 } change_state([standing_state, walking_state]); } //SETTING ITS INITIAL STATE state = standing_state;
敌人Step事件碰撞与重力代码
//HORIZONTAL COLLISION if place_meeting(x + hsp, y, obj_block) { var _hsp = sign(hsp); //AS LONG AS I'M NOT COLLIDING 1 PIXEL IN THE DIRECTION I'M while (!place_meeting(x + _hsp, y, obj_block)) { //I MOVE 1 PIXEL IN THAT DIRECTION x+=_hsp; } //I'VE MOVED WHAT I HAD TO MOVE, AND NOW I'M COLLIDING. //I END MY SPEED hsp = 0; show_debug_message("colliding with the floor") } //I MOVE x += hsp; //VERTICAL COLLISION if place_meeting(x, y + vsp, obj_block) { var _vsp = sign(vsp); while (!place_meeting(x, y + _vsp, obj_block)) { y+=_vsp; } vsp = 0; show_debug_message("colliding with the wall") } y += vsp; //GRAVITY if !place_meeting(x,y,obj_block) { vsp += grav; } //RUNNING THE CURRENT STATE state();
问题修复方案
1. 修正调试消息文本颠倒
- 水平碰撞代码中的
show_debug_message("colliding with the floor")改为show_debug_message("colliding with the wall") - 垂直碰撞代码中的
show_debug_message("colliding with the wall")改为show_debug_message("colliding with the floor")
2. 解决垂直碰撞失效与穿墙问题
- 修复移动逻辑冲突:
walking_state中直接修改x坐标会跳过碰撞检测,需改为更新hsp变量让碰撞代码处理移动:// 替换原来的x += lengthdir_x(vel, _dir); hsp = lengthdir_x(vel, _dir); - 调整重力代码顺序:将重力代码移到垂直碰撞检测之前,确保
vsp被重力更新后再检测碰撞://GRAVITY if !place_meeting(x,y+1,obj_block) { vsp += grav; } //VERTICAL COLLISION if place_meeting(x, y + vsp, obj_block) { var _vsp = sign(vsp); while (!place_meeting(x, y + _vsp, obj_block)) { y+=_vsp; } vsp = 0; show_debug_message("colliding with the floor") } y += vsp; //HORIZONTAL COLLISION if place_meeting(x + hsp, y, obj_block) { var _hsp = sign(hsp); while (!place_meeting(x + _hsp, y, obj_block)) { x+=_hsp; } hsp = 0; show_debug_message("colliding with the wall") } x += hsp; //RUNNING THE CURRENT STATE state();
3. 修复状态切换函数变量错误
change_state中使用了未定义的time_state变量,改为state_time:
change_state = function(_state) { state_time--; state_timer = irandom(state_time); if (state_timer == state_time or state_time <= 0) { state = _state[irandom(array_length(_state)-1)]; state_time = room_speed * 10; } }
内容的提问来源于stack exchange,提问作者Ynowihet
相关产品推荐
相关产品推荐

