apologies in advance for the very newbie question, currently trying to learn libGDX, every tutorial suggests this should work, its correctly displaying the text button in the centre of the screen however it isnt showing the background image title.png.
ive played around with it quite a bit to try and get it to work with no luck, any help you can give me would be greatly appreciated!
public class MainMenu implements Screen {
ZebraGems game;
Stage stage;
BitmapFont font;
BitmapFont blackfont;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
TextButton button;
Texture titleTexture;
Sprite titleSprite;
public MainMenu(ZebraGems game){
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
batch.begin();
titleSprite.draw(batch);
stage.draw();
batch.end();
}
#Override
public void resize(int width, int height) {
if (stage == null);
stage = new Stage (width, height, true);
stage.clear();
titleTexture = new Texture("data/title.png");
titleSprite = new Sprite(titleTexture);
titleSprite.setColor(1,1,1,0);
titleSprite.setSize(480, 800);
titleSprite.setPosition (0, 0);
Gdx.input.setInputProcessor(stage);
TextButtonStyle style = new TextButtonStyle();
style.up = skin.getDrawable("play");
style.down = skin.getDrawable("playpressed");
style.font = blackfont;
button = new TextButton("", style);
button.setWidth(213);
button.setHeight(94);
button.setX(Gdx.graphics.getWidth() /2 - button.getWidth() /2);
button.setY(Gdx.graphics.getHeight() /2 - button.getHeight() /2);
stage.addActor(button);
}
#Override
public void show() {
batch = new SpriteBatch();
atlas = new TextureAtlas("data/playButton.pack");
skin = new Skin();
skin.addRegions(atlas);
blackfont = new BitmapFont(Gdx.files.internal("data/blackfont.fnt"));
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
batch.dispose();
skin.dispose();
atlas.dispose();
stage.dispose();
blackfont.dispose();
}
}
Move stage.draw() out of the batch.begin() and batch.end().
Furthermore in case you are using the latest nighty build of libgdx, you should add stage.getViewport().update(width, height, true) to your resize(...) method.
Furthermore you are doing this titleSprite.setColor(1,1,1,0) which sets the alpha of the background sprite to 0 which means 100% transparent = invisible.
Do this instead: titleSprite.setColor(1,1,1,1).
In total your render should look like this:
batch.begin();
titleSprite.draw(batch);
batch.end();
stage.act(delta);
stage.draw();
Related
I'm unsuccessfully trying to add a button on my interface with the default skin. (http://download.bitowl.de/defaultskin.zip)
My code :
public class Core extends ApplicationAdapter {
Stage stage;
SpriteBatch batch;
Texture background;
TextButton button;
Skin skin;
OrthographicCamera camera;
Vector3 mouse;
#Override
public void create() {
camera = new OrthographicCamera();
batch = new SpriteBatch();
stage = new Stage();
mouse = new Vector3();
batch.setProjectionMatrix(camera.combined);
background = new Texture("fondLogoTikal.jpg");
skin = new Skin(Gdx.files.internal("ui/defaultskin.json"));
TextButton button = new TextButton("Mon boutton", skin);
button.setPosition(125, 200);
stage.addActor(button);
}
public void resize(int width, int height) {
camera.setToOrtho(false, width, height);
}
#Override
public void render() {
batch.begin();
batch.setProjectionMatrix(camera.combined);
batch.draw(background, 0, 0);
batch.end();
}
#Override
public void dispose() {
batch.dispose();
stage.dispose();
background.dispose();
}
}
My error :
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error reading file: ui/defaultskin.json
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:98)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:75)
at com.mygdx.game.Core.create(Core.java:32)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
however, I correctly put the ui file in the assets file.
And if I delete the TextButton, my code works correctly
I'm using libgdx 1.9.0 for game development.
I have one stage in main screen that includes multiple buttons.
I have another screen code that shows game objects.
when the back button in android device is pressed the stage in main screen gets disabled
and listener functions for buttons don't work.
In Main Screen I have :
public class MainMenuScreen extends ScreenAdapter {
TextButton button_start;
TextButtonStyle textButtonStyle;
BitmapFont font;
FitViewport viewp;
Stage stage;
public MainMenuScreen(IndependentArch game) {
this.game = game;
/* add GUI controls */
stage = new Stage(new StretchViewport(game.SCREEN_WIDTH,
game.SCREEN_Height), game.batcher);
Gdx.input.setInputProcessor(stage);
textButtonStyle = new TextButtonStyle();
textButtonStyle.font = Assets.font_btitr;
textButtonStyle.up = Assets.skin.getDrawable("button");
textButtonStyle.down = Assets.skin.getDrawable("button-down");
button_start = new TextButton(Reshape.doReshape_mirror("start"),
textButtonStyle);
button_start.setBounds(0, 0, 200, 50);
button_start.setPosition(140, 255);
button_about.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
MyTextInputListener listener = new MyTextInputListener();
Gdx.input.getTextInput(listener, "name", "",
"please enter your name.");
}
});
stage.addActor(button_start);
}
public void update() {
if (Gdx.input.justTouched()) {
stage.getCamera().unproject(
touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
}
}
public void draw(float delta) {
Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batcher.disableBlending();
game.batcher.begin();
game.batcher.draw(Assets.backgroundRegion[0], 0, 0, game.SCREEN_WIDTH,
game.SCREEN_Height);
game.batcher.end();
stage.act(delta);
stage.draw();
game.batcher.enableBlending();
game.batcher.begin();
game.batcher.draw(Assets.uiskin.findRegion("music"),
game.SCREEN_WIDTH - 90, 10, 80, 80);
game.batcher.end();
}
#Override
public void render(float delta) {
update();
draw(delta);
}}
and in Game Screen I have :
public class GameScreen implements Screen, InputProcessor {
OrthographicCamera guiCam;
Vector3 touchPoint;
World world;
WorldRenderer renderer;
CameraController camController;
InputMultiplexer multiplexer;
GestureDetector gestureDetector;
/** for debug rendering **/
ShapeRenderer debugRenderer = new ShapeRenderer();
int lastScore;
String scoreString;
float totalGameTime;
GlyphLayout glyphLayout = new GlyphLayout();
public GameScreen(IndependentArch game) {
this.game = game;
Gdx.input.setCatchBackKey(true);
state = GAME_READY;
guiCam = new OrthographicCamera(game.SCREEN_WIDTH, game.SCREEN_Height);
guiCam.position.set(game.SCREEN_WIDTH / 2, game.SCREEN_Height / 2, 0);
touchPoint = new Vector3();
world = new World();
renderer = new WorldRenderer(game.batcher, world);
Gdx.input.setInputProcessor(this);
camController = new CameraController(world);
multiplexer = new InputMultiplexer();
multiplexer.addProcessor(new GestureDetector(20, 0.5f, 2, 0.15f,
camController));
multiplexer.addProcessor(this);
Gdx.input.setInputProcessor(multiplexer);
}
// * InputProcessor methods ***************************//
#Override
public boolean keyDown(int keycode) {
if (keycode == Keys.BACK) {
game.setScreen(new MainMenuScreen(game));
// Gdx.app.exit();
}
return false;
}}
Override the show() method in the main menu screen and set the input processor to stage there
#Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
Can someone just give me a real quick answer on how i would implement a viewport so this sprite would fill the whole screen?
Thanks
Current code (not working):
public SplashScreen(Jump game)
{
this.game = game;
cam = new OrthographicCamera();
cam.setToOrtho(false, 1920, 1080);
sb = new SpriteBatch();
}
public void show()
{
}
public void render(float delta)
{
Gdx.gl20.glClearColor(0.2F, 0.6F, 1F, 1F);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(Assets.splash_spr_background, 0, 0);
sb.end();
}
On
#Override
public void resize(int width, int height) {
stage.getViewport().update(Gdx.graphics.getWidth(),
Gdx.graphics.getHeight(), false);
}
as spritebatch does not have a viewport. You have to control this using stage or cam.
you can do that.
I'am trying to center my image, but I can't. I tryied everything with viewports and nothing. I won't want to use magic numbers to make the trick! it's possible that the default viewport is messing up?
here is my code:
public class LoadingScreen extends AbstractScreen{
private Stage stage;
private Texture texture;
private Image loading_image;
public LoadingScreen(XBallGame game ) {
super(game);
texture = TextureManager.SPLASH;
loading_image = new Image();
stage = new Stage();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1); //sets clear color to black
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //clear the batch
stage.act(delta);
stage.draw();
}
#Override
public void show() {
loading_image.addAction(Actions.sequence(Actions.alpha(0)
,Actions.fadeIn(0.5f),Actions.delay(2),Actions.run(new Runnable() {
#Override
public void run() {
System.out.println("DONE");
}
})));
stage.addActor(loading_image);
loading_image.setPosition(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
}
UPDATE (Fixed the problem) :
Just added this line:
loading_image.setPosition((Gdx.graphics.getWidth()/2) - loading_image.getWidth()/2,
(Gdx.graphics.getHeight()/2) - loading_image.getHeight()/2);
and works fine!
I am testing out Libgdx and Scene2d. I expected this small program to display a logo, but it draws a black screen only. Any idea what am I missing?
public class MyGame implements ApplicationListener {
private Stage stage;
#Override
public void create() {
stage = new Stage(800, 800, false);
Gdx.input.setInputProcessor(stage);
MyActor actor = new MyActor();
stage.addActor(actor);
}
#Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
#Override
public void dispose() {
stage.dispose();
}
#Override
public void resize(int width, int height) {
stage.setViewport(800, 800, false);
}
}
public class MyActor extends Actor {
Sprite sprite;
public MyActor() {
sprite = new Sprite();
sprite.setTexture(new Texture("data/libgdx.png"));
setWidth(sprite.getWidth());
setHeight(sprite.getHeight());
setBounds(0, 0, getWidth(), getHeight());
setTouchable(Touchable.enabled);
setX(0);
setY(0);
}
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
batch.draw(sprite, getX(), getY());
}
}
Construct the sprite with the texture and use Gdx.file.internal:
sprite = new Sprite(new Texture(Gdx.files.internal("data/libgdx.png")));
Anyway, if you just want to display and act on images, you might prefer to use Image class:
private Stage stage;
private Texture texture;
#Override
public void create() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
com.badlogic.gdx.scenes.scene2d.ui.Image actor = new com.badlogic.gdx.scenes.scene2d.ui.Image(region);
stage.addActor(actor);
}
#Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
I was getting a black screen too until I explicitly set Actor's height (setHeight(height)) and width (setWidth(width)) to the Sprite's values.
tex = new Texture(Gdx.files.internal("happy.png"));
Image happy = new Image(tex);
/* happy.setBounds(happy.getX(), happy.getY(), happy.getWidth(), happy.getHeight()); not needed if using full image */
stage.addActor(happy);
Your problem is most likely this line, in the draw method
batch.draw(sprite, getX(), getY());
The code I have seen when drawing sprites is
sprite.draw(batch);