如何使对象相对于image_angle移动?

时间:2017-07-08 14:13:43

标签: game-maker game-maker-studio-1.4

注意:我使用的是GameMaker 1.4,而不是2.我不知道这是否会有所不同,但为了以防万一,我指出它。

好的,所以我在GameMaker游戏中遇到了一个问题,我正在使一个物体相对于image_angle进行上下移动。出于某种原因,它确实想要像往常一样在y轴上移动并完全忽略image_angle。这真的很烦人,如果不修复,可能会完全改变游戏。

我的代码是针对步骤事件:

// Mouse controls.
image_angle = point_direction(x, y, mouse_x, mouse_y);

if(keyboard_check(ord('W')))
{
    y -= playerSpeed;
}

if(keyboard_check(ord('S')))
{
    y += playerSpeed;
}

我的创建活动代码是:

globalvar fuelRemaining;
fuelRemaining = 60;

playerSpeed = 3;

3 个答案:

答案 0 :(得分:0)

image_angle = point_direction(x, y, mouse_x, mouse_y);
direction = image_angle;

if(keyboard_check(ord('W'))){
    speed = -playerSpeed;
}
if(keyboard_check_release(ord('W'))){
    speed = 0;
}

if(keyboard_check(ord('S'))){
    speed = playerSpeed;
}
if(keyboard_check_release(ord('S'))){
    speed = 0;
}

答案 1 :(得分:0)

@Gamio代码几乎是正确的(应该是_released而不是_release,混淆了WS键的符号。以下是一些改进:

image_angle = point_direction(x, y, mouse_x, mouse_y);
direction = image_angle;

if point_distance(x, y, mouse_x, mouse_y) < spd
{
   speed = 0;
}
else
{
    var spd = 0;

    if keyboard_check(ord("W")) 
        spd += playerSpeed; // If pressed both W and S, then will stop 

    if keyboard_check(ord("S"))
        spd -= playerSpeed;

    speed = spd;

    if keyboard_check_released(ord("W")) or keyboard_check_released(ord("S"))
        speed = 0;
}

将代码放入Step End事件(不是Step)。

如果您想直接更改坐标,而不是speed,则也可以,但更难一点(您需要使用x同时计算ylengthdir_x增量lengthdir_y函数,然后将这些增量添加到当前坐标。)

答案 2 :(得分:0)

在您的代码中,您设置了image_angledirection,但后来只更改了y坐标,因此当然您不会看到x坐标的任何更改。

您可以使用内置的directionspeed变量,游戏制作者会自动移动任何对象,或者您可以在{{{{}}上应用lengthdir_xlengthdir_y个函数1}}和x坐标。

ydirection示例:

speed

//create event playerSpeed = 3; //step event image_angle = point_direction(x, y, mouse_x, mouse_y); direction = image_angle; speed = 0; //reset speed at start of each step event //now you wont have to check if key has been released to stop object if(keyboard_check(ord('W'))) { //distance between player and target distance = point_distance(x, y, mouse_x, mouse_y) if distance < playerSpeed { //with this, object will move only to mouse position, not further speed = distance; } else { speed = playerSpeed; } } else if(keyboard_check(ord('S'))) { speed = -playerSpeed; } lengthdir_x示例:

lengthdir_y

请注意,所有游戏制作者功能都使用角度,角度为0(360),在圆圈的右侧,逆时针增加。 任何函数都可以在低于和高于0到360范围的值下正常工作,例如// create event playerSpeed = 3; // step event image_angle = point_direction(x, y, mouse_x, mouse_y); if keyboard_check( ord("W") ) { x += lengthdir_x( playerSpeed, image_angle ); y += lengthdir_y( playerSpeed, image_angle ); } else if keyboard_check( ord("S") ) { x += lengthdir_x( playerSpeed, image_angle-180 ); y += lengthdir_y( playerSpeed, image_angle-180 ); } -90 = 270 (360-90)。 请注意游戏制作者如何检查400 = 40 (400-360)true。任何值&lt;检查时false将导致超过0。