当我在Libgdx中更改屏幕时,它会立即返回

时间:2013-10-09 19:16:23

标签: screen libgdx

我有2个屏幕,希望能够在它们之间移动。一个打开负载,这个称为gameScreen。另一个叫做另一个。游戏什么都不做。我只是在玩libgdx。这是我的代码

打开gameScreen的主类

主要类MyGdxGame包含此

package com.me.mygdxgame;

import com.badlogic.gdx.Game;
import com.me.mygdxgame.gameScreen;

public class MyGdxGame extends Game{

public gameScreen game;

@Override
public void create() {  
    game = new gameScreen(this);
    setScreen(game);
    }
}

gameScreen是

package com.me.mygdxgame;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;

public class gameScreen extends Game implements Screen{

OrthographicCamera camera;
 private MyGdxGame game;

 public Another game2;


public gameScreen (MyGdxGame game) {
    this.game=game;
    camera=new OrthographicCamera();
    camera.setToOrtho(true,1080,1920);
}

@Override
public void render(float delta) {
    // TODO Auto-generated method stub
    Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);



    camera.update();

    if(Gdx.input.isTouched()){
        game2=new Another(this);
        setScreen(game2);

    }

}



@Override
public void resize(int width, int height) {
    // TODO Auto-generated method stub

}

@Override
public void show() {
    // TODO Auto-generated method stub

}

@Override
public void hide() {
    // TODO Auto-generated method stub

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

@Override
public void create() {
    // TODO Auto-generated method stub

}



}

和另一个是

package com.me.mygdxgame;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Another extends Game implements Screen{

OrthographicCamera cameraa;
SpriteBatch batch;
Sprite hello;
Texture thello;




private gameScreen game;

public Another(gameScreen game){
    this.game=game;
    cameraa=new OrthographicCamera();
    cameraa.setToOrtho(true,1080,1920);
    batch=new SpriteBatch();


    thello = new Texture(Gdx.files.internal("data/libgdx.png"));
    thello.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    hello = new Sprite(thello);
    hello.flip(false, true);

}

@Override
public void render(float delta) {
    // TODO Auto-generated method stub
    Gdx.gl.glClearColor(0F, 0F, 1F, 1F);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


    cameraa.update();


    if(Gdx.input.isTouched()){


    }
}

@Override
public void resize(int width, int height) {
    // TODO Auto-generated method stub

}

@Override
public void show() {
    // TODO Auto-generated method stub
    Gdx.gl.glClearColor(0F, 0F, 1F, 1F);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    batch.setProjectionMatrix(cameraa.combined);
    cameraa.update();

    batch.begin();
    batch.draw(hello,0,0);
    batch.end();

}

@Override
public void hide() {
    // TODO Auto-generated method stub

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

@Override
public void create() {
    // TODO Auto-generated method stub


}

}

我使用了相当不寻常的名字,比如cameraa和gamee。这是因为整个项目都是一个测试,就像我说的那样,我不能为设置专有名称而烦恼

1 个答案:

答案 0 :(得分:0)

好的,在你的屏幕(gameScreen和另一个)中删除“extends Game”,只留下工具屏幕。

然后代替在gameScreen中使用setScreen(game2)。像这样使用它:

game.setScreen(game2)//添加“游戏”。

那是因为你的屏幕不会从游戏延伸,但是你可以在该领域获得游戏,所以你可以从中使用它。

仅作为额外:不要将另一个实例称为“game2”,因为它不是游戏。并且不要使用小写字母来开始一个类的名称(gameScreen应该是GameScreen),但这只是约定,如果你这样做,由你决定。