0

如何使用 WASD 或箭头键进行平滑旋转?我看到了一些例子,但他们都使用鼠标。我当然想使用 WASD 或箭头键。我真的希望有人可以帮助我。

到目前为止,这是我的代码。

// You can write your code in this editor
if(keyboard_check_pressed(ord("W"))) or (keyboard_check_pressed(vk_up)){
    speed = sailSpeed;
    image_speed = 1;
}
if(keyboard_check_released(ord("W"))) or (keyboard_check_released(vk_up)){
    speed = 0;
    image_speed = 0;
    image_index = 0;
}
if(keyboard_check_pressed(ord("D"))) or keyboard_check_pressed(vk_right){
    direction -= 45;
}
if(keyboard_check_pressed(ord("A"))) or keyboard_check_pressed(vk_left){
    direction += 45;
}

if(direction == 0) or (direction == 360) or (direction == -360){
    sprite_index = sprPlayerShipRight;
}
if(direction == 45) or (direction == -315){
    sprite_index = sprPlayerShipUpRight;
}
if(direction == 90) or (direction == -270){
    sprite_index = sprPlayerShipUp;
}
if(direction == 135) or (direction == -225){
    sprite_index = sprPlayerShipUpLeft;
}
if(direction == 180) or (direction == -180){
    sprite_index = sprPlayerShipLeft;
}
if(direction == 225) or (direction == -135){
    sprite_index = sprPlayerShipDownLeft;
}
if(direction == 270) or (direction == -90){
    sprite_index = sprPlayerShipDown;
}
if(direction == 315) or (direction == -45){
    sprite_index = sprPlayerShipDownRight;
}
4

2 回答 2

1

你追求的是这样的东西吗?

directionHorizontal = (keyboard_check(ord("D")) - keyboard_check(ord("A"));
directionVertical = (keyboard_check(ord("S")) - keyboard_check(ord("W"));
directionMoving = point_direction(0, 0, directionHorizontal, directionMoving);
var directionDifference = angle_difference(direction , directionMoving );
direction -= min(abs(directionDifference ), 10) * sign(directionDifference);

编辑:对不起,我看错了。听起来你正在加速和减速转弯速度。我要做的是:

将方法()添加到您的项目: https ://pastebin.com/7gzJTLKj

在 create 事件中初始化这些变量:

turningSpeed = 0;
turningSpeedMax = 15;
turningSpeedIncrement = 1;

在 step 事件中,你可以有这个:

turningDirection = (keyboard_check(ord("D")) - keyboard_check(ord("A")));
turningSpeed = approach(turningSpeed, turningSpeedMax * turningDirection, turningSpeedIncrement);
direction += turningSpeedIncrement;
于 2020-06-30T17:07:24.940 回答
0

如果您想以“平滑”的方式将您的精灵旋转 45 度,那么您唯一能做的就是在转弯处绘制另外 3 个中间帧。无论如何,精灵越多,动画就越流畅。你不能只用两个精灵进行平滑的转动。

不要依赖GM的转动功能,最好自己画框,相信我。

于 2020-09-09T17:00:06.953 回答