为什么我的create()方法无法识别全局变量? -Java LibGDX

时间:2019-01-21 16:00:50

标签: java libgdx

我有一个全局变量,称为:

private int currentLevel;
private int mapHeight;

我有一个游戏的构造函数,其中的currentLevel =调用setScreen()方法时输入的级别:

public ElevatorLevel(Game g, int level)
{  
    super(g, level);  
    currentLevel = level;
}

在我的create()方法中,我将mapHeight设置为750 * currentLevel(从1开始),但是,当我尝试如下所示在刀片中生成时,它们在-200到300之间生成。这是因为程序无法识别currentLevel(我假设),因此将其乘以零,得到-200。

public void create() 
{   
    world = new World(new Vector2(0, -9.8f), true);
    timeElapsed = 0;
    mapHeight = 750 * currentLevel;

    blade = new PhysicsActor();
        blade.storeAnimation( "", exTex );
        blade.setOriginCenter();
        blade.circularBoundary();
        blade.setMaxSpeed(50);
        blade.setDeceleration(50);

    bladesList = new ArrayList<PhysicsActor>();
        for (int i = 0; i < 3 ; i++)
        {
            blades = blade.clone();
            float xCoord = randomFloatGenerator(440, 20);
            float yCoord = randomFloatGenerator(mapHeight - 200, 300);
            blades.setPosition(xCoord, yCoord);
            mainStage.addActor(blades);
            bladesList.add(blades);
        }

Here is an image to help visualize

我的update(float dt)方法中也有一个标签设置为:

timeLabel.setText("Level: " + currentLevel);

如上图所示,currentLevel由标签识别,但create()中的mapHeight无法识别。 如何让currentLevel被create()方法识别?

1 个答案:

答案 0 :(得分:0)

下面是我几乎在每个libgdx项目中使用的标准肖像应用程序代码。我从不使用创建方法。取而代之的是,我使用构造函数,此代码是我所能找到的最无错误的代码。

游戏或菜单屏幕:

public class TutorialScreen implements Screen    {
private OrthographicCamera camera;

public static final float WORLD_HEIGHT = 240;
public static final float WORLD_WIDTH = 135;
private Viewport viewport;
private Stage stage;

private EntryPoint game;
private AdsController adsController;





public TutorialScreen(final EntryPoint game, final AdsController adsController){

    this.adsController = adsController;
    this.game = game;

    adsController.hideBannerAd();

    float aspectRatio = (float) (Gdx.graphics.getHeight() / Gdx.graphics.getWidth());
    camera = new OrthographicCamera(aspectRatio * WORLD_WIDTH, WORLD_HEIGHT);
    camera.setToOrtho(false);
    viewport = new FitViewport(WORLD_WIDTH , WORLD_HEIGHT,camera );
    stage = new Stage(viewport, game.batch);      

}



@Override
public void show() {

}

@Override
public void render(float delta) {

    stage.draw();
    stage.act();




}

@Override
public void resize(int width, int height) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}
}

首先打开您的应用的输入点:

package some.package;



public class EntryPoint extends Game {
SpriteBatch batch;
final AdsController adsController;



 public EntryPoint(final AdsController adsController ){

    this.adsController = adsController; //Interface for admob


}

@Override
public void create () {

    batch = new SpriteBatch();

    this.setScreen(new YourScreenClass(this,adsController)); //Above is tutorial so this would be new TutorialScreen(this,adsController)

}

@Override
public void render () {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();

    batch.end();
    super.render();
}

@Override
public void dispose () {
    batch.dispose();

}
}

可能错过了一些花括号或“;”但你明白了。这样,您就不会遇到任何参考问题。