Game maker 如何使对象相对于图像角度移动?
注意:我使用的是GameMaker 1.4,而不是2。我不知道这是否有什么不同,但我只是想以防万一 好吧,我在GameMaker游戏中遇到了一个问题,我让一个物体相对于图像角度上下移动。出于某种原因,它确实希望像法线一样在y轴上移动,完全忽略图像的角度。这真的很烦人,如果不修复,可能会完全改变游戏 步骤事件的代码是:Game maker 如何使对象相对于图像角度移动?,game-maker,game-maker-studio-1.4,Game Maker,Game Maker Studio 1.4,注意:我使用的是GameMaker 1.4,而不是2。我不知道这是否有什么不同,但我只是想以防万一 好吧,我在GameMaker游戏中遇到了一个问题,我让一个物体相对于图像角度上下移动。出于某种原因,它确实希望像法线一样在y轴上移动,完全忽略图像的角度。这真的很烦人,如果不修复,可能会完全改变游戏 步骤事件的代码是: // Mouse controls. image_angle = point_direction(x, y, mouse_x, mouse_y); if(keyboard_che
// 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;
创建事件的代码是:
// 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;
@Gamio代码几乎正确(应该是
\u release
而不是\u release
,混淆了W
和S
键的符号)。以下是一些改进:
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;
}
图像角度=点方向(x,y,鼠标x,鼠标y);
方向=图像角度;
如果点距离(x,y,鼠标x,鼠标y)
将代码放入步骤结束
事件中(不是步骤
)
如果您想直接更改坐标,而无需
速度
,这也是可能的,但难度稍大(您需要使用lengthdir\u x
和lengthdir\u y
函数计算x
和y
增量,然后将这些增量添加到当前坐标中)。在您的代码中,您可以设置image\u角度
和方向
,但只更改y
坐标,因此您当然不会看到对x
坐标的任何更改
您可以使用内置的direction
和speed
变量,游戏制作者将自动移动任何对象,或者您可以在x
和y
坐标上应用lengthdir\u x
和y
函数
方向
和速度
示例:
//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;
}
// 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 );
}
请注意,所有game maker功能都使用角度单位,角度0(360)位于圆圈右侧,逆时针递增。
任何函数在值小于或大于0到360的范围内都能正常工作,例如-90=270(360-90)
和400=40(400-360)
。
注意游戏制作人如何检查
true
和false
。任何小于0的值在检查时都将导致false
。注意:我只注意前两行,因为这是我真正需要的。。。它仍然不能工作…嗯,我不确定是否有必要在这里生成var spd,以及为什么要检查每一步是否释放了2*个密钥,我在ansver中以不同的方式解决了这一问题,并且在这里正确使用spd是不必要的。此外,当玩家同时按下两个键,然后只释放一个键(如果玩家经常切换键,这种情况可能经常发生,而玩家没有注意到),您还说有必要在结束步骤事件中使用此代码,这似乎不是真的,如果它能阻止任何事情发生,那么我想你应该稍微说明一下问题。@VojtěchŠvrček这不是一个很好的解决方案,只是对Gamio的代码进行了改进。想了这么多,只是想人们从game maker开始,怎么会结束:p。但好吧,这是可行的,如果他们只是把粘贴代码复制到项目中,那当然会一团糟。