繁体   English   中英

Java-Eclipse中未定义的方法

[英]Java - Method Undefined in Eclipse

我目前有一个3类Java应用程序,我试图使用JavaFX创建一个简单的游戏。 在My 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();


    }

}

您缺少的是实例化的“新”内容,即您需要编写

grid = new gameGrid(32, 32, 10, 10); 

在Java类以大写字母开头的情况下,您应该阅读准则

如果您希望在JavaFX中使用Java节点而不是画布来完成网格,则可以查看我最近提出的问题的代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM