我目前有一个3类java应用程序,我正在尝试使用JavaFX创建一个简单的游戏。在我的GameCore类中,我正在尝试创建gameGrid的一个实例。但是当我使用“grid = new gameGrid(int,int,int,int);”时eclipse告诉我gameGrid是未定义的并建议我创建方法,当我做eclipse请求时,它在我的gameCore类中放置一个私有方法gameGrid,但gameGrid应该是gameGrid.class的构造函数。我已经重新启动了项目并清理了项目无济于事。
public class gameCore {
gameGrid grid;
public gameCore(){
getGrid();
}
public void getGrid(){
grid = gameGrid(32, 32, 10, 10); //Error is here, underlining "gameGrid"
//Also using gameGrid.gameGrid(32,32,10,10); does not work either, still says its undefined
/*
This is the code that Eclipse wants to place when I let it fix the error, and it places this code in this class.
private gameGrid gameGrid(int i, int j, int k, int l) {
// TODO Auto-generated method stub
return null;
}
*/
}
}
public class gameGrid {
protected int[][] grid;
protected int tileWidth;
protected int tileHeight;
public gameGrid(int tileWidth, int tileHeight, int horizTileCount, int vertTileCount){
//Create Grid Object
grid = new int[vertTileCount][];
for(int y = 0; y < vertTileCount; y++){
for(int x = 0; x < horizTileCount; x++){
grid[y] = new int[horizTileCount];
}
}
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
}
}
import java.awt.Dimension;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class gameGUI extends Application {
Dimension screenDimensions = new Dimension(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
public static void main(String[] args){
launch(args);
}
public void start(Stage stage) throws Exception {
Canvas c = new Canvas();
StackPane sp = new StackPane();
Scene scene = new Scene(sp, screenDimensions.width, screenDimensions.height);
sp.getChildren().add(c);
stage.setScene(scene);
gameCore game = new gameCore();
stage.show();
}
}
答案 0 :(得分:2)
你缺少的是&#34;新的&#34;为了实例化,我即你需要写
grid = new gameGrid(32, 32, 10, 10);
在Java类中,以大写字符开头,你应该read the guidelines。
如果您希望在JavaFX中使用Java节点而不是画布来查看网格,您可以查看我最近问过的问题的the code。