在obj_player里,增加create事件,填写如下代码
//stat spd=6; hsp=0; vsp=0; jspd=18;//跳跃力度 grv=0.8;//重力 grounded=false; //moving and jumping direction=0; facing_dir=1;//朝向右边,-1为朝向左边 gamepad_set_axis_deadzone(0,0.5);
添加step事件,填写如下代码
//键盘控制代码 //手柄控制代码 var haxis=gamepad_axis_value(0,gp_axislh); var move_left=keyboard_check(vk_left) or gamepad_button_check(0,gp_padl) or (haxis<0); var move_right=keyboard_check(vk_right) or gamepad_button_check(0,gp_padr) or (haxis>0); var jumping=keyboard_check_pressed(ord("Z")) or gamepad_button_check_pressed(0,gp_face1); var jump_released=keyboard_check_released(ord("Z")) or gamepad_button_check_pressed(0,gp_face1); //设置移动和速度 var move=move_right-move_left; hsp=move*spd;//左右速度 vsp=vsp+grv;//向下速度 if move!=0 { sprite_index=spr_player_walk; facing_dir=move; image_xscale=facing_dir; direction=point_direction(x,y,x+move,y); } else { sprite_index=spr_player_idle; } //jump if jumping and grounded { grounded=false; vsp=-jspd; stprite_index=spr_player_jump; audio_play_sound(snd_jump,1,false); } if jump_released { vsp*=0.5; } //水平碰撞检测 if(place_meeting(x+hsp,y,obj_block)) { while(not place_meeting(x+sign(hsp),y,obj_block)) { x+=sign(hsp); } hsp=0; } x+=hsp; //垂直碰撞检测 if(place_meeting(x,y+vsp,obj_block)) { while(not place_meeting(x,y+sign(vsp),obj_block)) { y+=sign(vsp); } vsp=0; grounded=true; } else { grounded=false; } y+=vsp;
把主角放入房间0,地面绘制好,
这是地面,现在再用建立好的obj_block对象,在地面上再画一层,效果如下
按F5运行游戏,支持手柄控制,效果如下
暂无关于此日志的评论。