我目前有一个3类Java应用程序,我正在尝试使用JavaFX创建一个简单的游戏。在我的GameCore类中,我尝试创建gameGrid的实例。但是当我使用“ grid = new gameGrid(int,int,int,int);”时eclipse告诉我gameGrid是未定义的,并建议我创建该方法,当我按照eclipse的要求进行操作时,它将私有方法gameGrid放置在我的gameCore类中,但是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();
}
}
最佳答案
您缺少的是实例化的“新”,即e。你需要写
grid = new gameGrid(32, 32, 10, 10);
在Java类中,大写字母开头,应read the guidelines。
如果您希望在JavaFX中使用Java节点而不是画布来完成网格,则可以查看我最近提出的问题的the code。