【发布时间】:2014-10-15 13:17:32
【问题描述】:
我正在制作一个简单的游戏,该游戏旨在使用计算机的鼠标来控制其中的对象,但我很难让它按我的意愿运行。该对象(在本例中标记为“拳头”)旨在跟随鼠标在屏幕上的位置,直到某个常数(“到达”),然后它将继续跟随鼠标,但仅在最大值范围内抵达。基本上,这个人的手会跟随你的鼠标到一个点,但如果你的鼠标离得太远,他不会从他的肩膀上扯下来。到目前为止,我对该函数的代码是这样的:
void FistPosition(int& player_x,int& player_y, int& fist_x, int& fist_y){ //Start
ALLEGRO_MOUSE_STATE mousepos;
al_get_mouse_state(&mousepos); //Get the mouse's x and y position
const int REACH = 150; //Define the maximum distance the fist can go.
int playerc_x = player_x + 62; //Define the x and y center of the player object
int playerc_y = player_y + 92;
double x_dist = abs(playerc_x - mousepos.x); //get the x and y distance between
double y_dist = abs(playerc_y - mousepos.y); //body and mouse
int mousedist = sqrt((x_dist * x_dist) + (y_dist * y_dist)); //define mouse distance
if (mousedist < REACH){ //If within bounds of reach, follow the mouse position exactly
fist_x = mousepos.x;
fist_y = mousepos.y;
}
else{
fist_x = mousepos.x - (mousepos.x - fist_x); //Otherwise it cannot leave the
fist_y = mousepos.y - (mousepos.y - fist_y); //maximum reach
}
return;
}
我现在遇到的主要问题是,当角色在关卡中移动时,当玩家走得太远时,手会留在后面并锁定在原位。 我还希望第一个对象在距离达到 REACH 后继续移动,但要在相对鼠标位置之后保持在定义的距离“圆圈”内,但我很难在脑海中创建逻辑。非常感谢您更有经验的人可以提供的任何帮助!
【问题讨论】: