ich im Konstruktor haben:Libgdx Hintergrundbild nicht angezeigt
backgroundTexture = new Texture(Gdx.files.internal("Pantalla.jpg"));
Und in der render():
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
myGame.batch.setProjectionMatrix(camera.combined);
myGame.batch.begin();
myGame.batch.draw(backgroundTexture, 0, 0);
stage.draw();
myGame.batch.end();
}
Der vollständige Code der Klasse ist:
package states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import mx.itesm.mty.MyGame;
public class MenuState implements Screen{
final MyGame myGame;
Skin skin;
Skin skinPant;
Stage stage;
SpriteBatch batch;
TextButton.TextButtonStyle textButtonStyle1;
TextButton.TextButtonStyle textButtonStyle2;
TextButton.TextButtonStyle textButtonStyle3;
TextButton.TextButtonStyle textButtonStyle4;
TextureAtlas buttonAtlas;
BitmapFont font;
Texture backgroundTexture;
Sprite backgroundSprite;
OrthographicCamera camera;
public MenuState(final MyGame game) {
myGame = game;
camera = new OrthographicCamera();
camera.setToOrtho(false,800,480);
batch = new SpriteBatch();
skin = new Skin();
font = new BitmapFont();
stage = new Stage();
Gdx.input.setInputProcessor(stage);
buttonAtlas = new TextureAtlas(Gdx.files.internal("boton.atlas"));
skin.addRegions(buttonAtlas);
backgroundTexture = new Texture(Gdx.files.internal("Pantalla.jpg"));
backgroundSprite = new Sprite(backgroundTexture);
textButtonStyle1 = new TextButton.TextButtonStyle();
textButtonStyle1.font = font;
textButtonStyle1.down = skin.getDrawable("Inicio");
textButtonStyle1.up = skin.getDrawable("Inicio");
textButtonStyle1.checked = skin.getDrawable("Inicio");
TextButton buttonPlay = new TextButton("", textButtonStyle1);
textButtonStyle2 = new TextButton.TextButtonStyle();
textButtonStyle2.font = font;
textButtonStyle2.down = skin.getDrawable("Instrucciones");
textButtonStyle2.up = skin.getDrawable("Instrucciones");
textButtonStyle2.checked = skin.getDrawable("Instrucciones");
TextButton buttonInst = new TextButton("",textButtonStyle2);
textButtonStyle3 = new TextButton.TextButtonStyle();
textButtonStyle3.font = font;
textButtonStyle3.down = skin.getDrawable("Informacion");
textButtonStyle3.up = skin.getDrawable("Informacion");
textButtonStyle3.checked = skin.getDrawable("Informacion");
TextButton buttonInfo = new TextButton("",textButtonStyle3);
textButtonStyle4 = new TextButton.TextButtonStyle();
textButtonStyle4.font = font;
textButtonStyle4.down = skin.getDrawable("Salir");
textButtonStyle4.up = skin.getDrawable("Salir");
textButtonStyle4.checked = skin.getDrawable("Salir");
TextButton buttonSalir = new TextButton("",textButtonStyle4);
buttonPlay.setWidth(300f);
buttonPlay.setHeight(100f);
buttonPlay.setPosition(Gdx.graphics.getWidth()/2 - 150f, Gdx.graphics.getHeight()/2);
buttonInst.setWidth(300f);
buttonInst.setHeight(100f);
buttonInst.setPosition(Gdx.graphics.getWidth()/2 - 150f, Gdx.graphics.getHeight()/2 - 120f);
buttonInfo.setWidth(300f);
buttonInfo.setHeight(100f);
buttonInfo.setPosition(Gdx.graphics.getWidth()/2 - 150f, Gdx.graphics.getHeight()/2 - 240f);
buttonSalir.setWidth(300f);
buttonSalir.setHeight(100f);
buttonSalir.setPosition(Gdx.graphics.getWidth()/2 - 150f, Gdx.graphics.getHeight()/2 - 360f);
buttonPlay.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
myGame.setScreen(new PlayMenuState(myGame));
}
});
buttonInst.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
myGame.setScreen(new PlayMenuState(myGame));
}
});
buttonInfo.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
myGame.setScreen(new PlayMenuState(myGame));
}
});
buttonSalir.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
myGame.setScreen(new PlayMenuState(myGame));
}
});
stage.addActor(buttonPlay);
stage.addActor(buttonInst);
stage.addActor(buttonInfo);
stage.addActor(buttonSalir);
}
@Override
public void show() {
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
myGame.batch.setProjectionMatrix(camera.combined);
myGame.batch.begin();
myGame.batch.draw(backgroundTexture, 0, 0);
stage.draw();
myGame.batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
Ich füge mehrere Knöpfe und das Hintergrundbild hinzu. Das Bild befindet sich im Ordner "Assets", es wird jedoch nicht angezeigt.
Und ich hatte einen Zweifel über Rechtecke, dass, wenn es eine Möglichkeit ist, ein Bild auf ein Rechteck-Objekt auf die gleiche Weise stellen Sie die x, y, Höhe, Breite hinzufügen?
warum nennst du 'myGame.batch' aber nicht die, die Sie in' MenuState' Klasse deklariert? BTW, vergiss nicht, die Antwort auf deine vorherige [Frage] zu akzeptieren (http://stackoverflow.com/questions/36819541/androidstudio-libgdx-changelistener-not-working) – Enigo
Vergaß das zu tun! Vielen Dank! Und ich rufe 'myGame' an, weil ich' myGame = game' deklariert habe. –
Aber warum deklarierst du 'SpriteBatch charge;' nochmal in dieser Klasse? Außerdem sehe ich, dass du 'backgroundSprite' hast, warum also nicht das zeichnen? – Enigo