问题描述
我有一个程序,它在开始时创建多个随机放置的对象(球),现在我试图将玩家球自动移向最近的对象,直到它们的中心相同.我已经确定了存储在一个名为 distance 且索引为closest"的数组中的最近对象的距离,并将速度设置为在其他地方初始化的变量 delta,我如何获得正确的方向?马上它只是对角移动,但不向最近的球移动
I have a program which creates multiple randomly placed objects(balls) at the beginning, now im trying to move the player ball towards the closest object automatically until their centres are the same. I have determined the distance of the closest object stored in an array called distance with index of 'closest', and set the speed as variable delta initialised somewhere else, how do i get the direction right? Right nowIt moves just diagonally but not towards the closest ball
coordinatex[closest] 和coordinatey[closest] 是最近对象的 x 和 y 坐标.
coordinatex[closest] and coordinatey[closest] are the x and y coordinates of the closest object.
playerObject.getX 和 playerObjectgetY 给我玩家注射的坐标
playerObject.getX and playerObjectgetY gives me the coordinates of the player onject
public void move(int delta) {
for(int i=0; i<distance[closest]; i++) {
if (x<coordinatex[closest] && y<coordinatey[closest]) {
playerObject.setX(playerObject.getX() + 0.1*delta);
playerObject.setY(playerObject.getY() + 0.1*delta);
} else if(x>coordinatex[closest] && y>coordinatey[closest]) {
playerObject.setX(playerObject.getX() - 0.1*delta);
playerObject.setY(playerObject.getY() - 0.1*delta);
} else if(x>coordinatex[closest] && y<coordinatey[closest]) {
playerObject.setX(playerObject.getX() - 0.1*delta);
playerObject.setY(playerObject.getY() + 0.1*delta);
} else if (x<coordinatex[closest] && y>coordinatey[closest]) {
playerObject.setX(playerObject.getX() + 0.1*delta);
playerObject.setY(playerObject.getY() - 0.1*delta);
}
}
}
推荐答案
我看到了你的四个半基本(对角线)方向的处理程序,但没有四个基本方向.例如,在这个游戏中,敌人使用move(int row, int col)
方法,显示 这里,从八个方向中的任何一个.
I see your handlers for the four semi-cardinal (diagonal) directions, but not the four cardinal directions. In this game, for example, enemies approach the player in a step-wise manner using the move(int row, int col)
method, shown here, from any of eight directions.
这篇关于在随机路径上将对象移动到另一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!