0

I have 2 screens and wish to be able to move between them. One opens on load and this one is called gameScreen. the other is called Another. The game doesn't do anything. i'm just playing with libgdx. This is my code

main class which opens gameScreen

the main class, MyGdxGame, contains this

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 is

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

}



}

and Another is

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


}

}

i used rather unusual names such as cameraa and gamee. this is because this whole project is a test, like i said i i couldnt be bothered setting proper names

4

1 回答 1

0

好的,在您的屏幕(gameScreen 和另一个)中删除“扩展游戏”,只留下工具屏幕。

然后,不要像在 gameScreen 中那样使用 setScreen(game2)。像这样使用它:

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

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

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

于 2013-10-09T20:28:11.503 回答