1
我有一個GTGE遊戲,我遇到了一些數學問題。我不明白爲什麼它不以我想要的方式移動。當你點擊某處時,精靈就會在那裏移動。當您右鍵單擊它計算角度並在那裏移動,但它不會停止。我捕獲鼠標的位置,並且每次它更新屏幕時,我都會檢查該精靈是否已經到達該位置,但是何時它不會停止。這裏是我的代碼:GTGE的Java問題數學
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import com.golden.gamedev.Game;
import com.golden.gamedev.GameLoader;
import com.golden.gamedev.object.Background;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.background.ImageBackground;
public class SpriteTest extends Game {
//Default Background
Background background;
//Toolkit!
Toolkit toolkit = Toolkit.getDefaultToolkit();
//Main Sprite
Sprite pacman;
Dimension stop;
@Override
public void initResources() {
//Load the background
background = new ImageBackground(getImage("nature-wallpapers.jpg"));
//Load the Sprite
pacman = new Sprite(getImage("pacman.png"));
pacman.setActive(false);
pacman.setBackground(background);
playMusic("intro.mid");
showCursor();
}
@Override
public void render(Graphics2D g) {
//Render background
background.render(g);
//Render sprites
pacman.render(g);
}
@Override
public void update(long elapsedTime) {
//Update the background
background.update(elapsedTime);
//Update sprites
if(click()) {
pacman.setActive(true);
pacman.setLocation(getMouseX(), getMouseY());
}
if(rightClick()) {
int spriteLocX = (int) pacman.getX();
int spriteLocY = (int) pacman.getY();
int mouseX = getMouseX();
int mouseY = getMouseY();
pacman.setMovement(1, -(Math.toDegrees(Math.atan2(mouseY - spriteLocY, mouseX - spriteLocX))));
stop = new Dimension(mouseX, mouseY);
}
if(stop != null) {
if(pacman.getX() == stop.getWidth()) {
if(pacman.getY() == stop.getHeight()){
pacman.setMovement(0, 0);
}
}
}
pacman.update(elapsedTime);
}
public static void main(String[] args) {
GameLoader game = new GameLoader();
game.setup(new SpriteTest(), new Dimension(800,600), true);
game.start();
}
}