Android Studio-libgdx making a 2d game.
I have a Main which creates the MenuState, in the render method I made this
#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);
batch = new SpriteBatch();
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
stage = new Stage();
final TextButton button = new TextButton("Click me", skin, "default");
button.setWidth(400f);
button.setHeight(100f);
button.setPosition(Gdx.graphics.getWidth() / 2 - 100f, Gdx.graphics.getHeight() / 2 - 10f);
button.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
});
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
myGame.batch.begin();
stage.draw();
myGame.batch.end();
}
I checked some tutorials on how to make a menu and that's pretty much what I'm testing in here.
But when I run the app the button appears and nothing happens if I click it.
I've tried testing with the ClickListener but still doesn't work. Any ideas? Or am I doing it wrong or it should be in another method?
I'm new with libgdx. Thanks in advance.
You are creating new instances of the stage, skin and batch in render method? Move them away from your render method ;)
Render should look more like this:
#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();
stage.draw();
myGame.batch.end();
}
And in constructor:
batch = new SpriteBatch();
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
stage = new Stage();
final TextButton button = new TextButton("Click me", skin, "default");
button.setWidth(400f);
button.setHeight(100f);
button.setPosition(Gdx.graphics.getWidth() / 2 - 100f, Gdx.graphics.getHeight() / 2 - 10f);
button.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
});
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
Related
I'm developing a game and in the render function I have:
(Square extends Sprite)
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
backgroundSprite.draw(batch);
endTime = System.currentTimeMillis();
if((endTime-startTime) > 1000) {
squareList.add(squarePositionFactory.getSquare());
for(Square square : squareList) {
square.setSize(80, 80);
square.draw(batch);
square.updatePosition(square.posX, square.posY + 100);
startTime = System.currentTimeMillis();
Gdx.app.log("[Playing time]", square.toString());
}
}
batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
The problem is the render method only works when I click repeatdly on the screen so the images are displayed blinking. How can I fix this? Thanks!
You may find this page useful.
you haven't add the actor to the stage.
stage.addActor("your actor");
stage.draw();
Im completly new here and I need some fast help on a Project!!
I use LibGdx to write a little "Risiko" if somebody know it. Im creating a menu and my TextButtons dont work!! Here is the important Part:
stage = new Stage[3];
stage[0] = new Stage();
stage[1]= new Stage();
stage[2]= new Stage();
batch = new SpriteBatch();
setBmap(new Texture("map.png"));
inputMultiplexer=new InputMultiplexer();
inputMultiplexer.addProcessor(this);
inputMultiplexer.addProcessor(stage[0]);
inputMultiplexer.addProcessor(stage[1]);
inputMultiplexer.addProcessor(stage[2]);
Gdx.input.setInputProcessor(inputMultiplexer);
button = new TextButton[8];
//Southamerica
button[0] = new TextButton("Attack", textButtonStyle);
button[0].setBounds(605,463, 100,40);
... Create the buttons.
stage[0].addActor(button[0]);
stage[0].addActor(button[1]);
stage[0].addActor(button[2]);
stage[1].addActor(button[4]);
stage[1].addActor(button[5]);
stage[1].addActor(button[6]);
stage[2].addActor(button[3]);
stage[2].addActor(button[7]);
for(int i= 0; i <= 7 ; i++){
button[i].addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y){
System.out.println("Button action");
}});
}
/*button[0].addListener(new ChangeListener() {
public void changed (ChangeEvent event, Actor actor) {
System.out.println("BUTTOn");
}
});*/
button[0].addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y){
System.out.println("BUTTON PRESSED!!");
}});
Add Buttons to stages and create the Listener!
And now my render() :
super.render();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
font = new BitmapFont();
batch.begin();
batch.draw(getBmap(), 0, 0);
stage[0].draw();
-
batch.end();
}
I use different Stages do draw different menus. Just for Info :D
And already Thanks for any answer!!
Sorry, for the request now I got it. I didnt got that I have to do it like this:
inputMultiplexer=new InputMultiplexer();
inputMultiplexer.addProcessor(stage[0]);
inputMultiplexer.addProcessor(stage[1]);
inputMultiplexer.addProcessor(stage[2]);
inputMultiplexer.addProcessor(this);
Gdx.input.setInputProcessor(inputMultiplexer);
and not like this:
inputMultiplexer=new InputMultiplexer();
inputMultiplexer.addProcessor(this);
inputMultiplexer.addProcessor(stage[0]);
inputMultiplexer.addProcessor(stage[1]);
inputMultiplexer.addProcessor(stage[2]);
Gdx.input.setInputProcessor(inputMultiplexer);
I love it :D
RIP
I'm trying to use the gdx Timer to increment a value repeatedly but when I use this value inside a Label it seems like nothing happens and the value is stuck at 0 , here is my code for the timer :
#Override
public void show() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
Skin skin = new Skin(Gdx.files.internal("gui/uiskin.json"));
table = new Table(skin);
table.setFillParent(true);
atlas = new TextureAtlas("gui/atlas.pack");
skin2 = new Skin(Gdx.files.internal("gui/MenuSkin.json"), atlas);
DragAndDrop dragAndDrop = new DragAndDrop();
// My tables
inventoryActor = new InventoryActor(new Inventory(16, 3), dragAndDrop, skin, "Game Pad");
inventoryActor2 = new InventoryActor(new Inventory(25), dragAndDrop, skin, "Inventory Pad");
// bc image
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("et2.jpg"));
Timer.schedule(new Task(){
#Override
public void run() {
timing++;
}
}, 1);
heading = new Label("Good Luck"+(timing++), skin2, "big");
//Timer task
//timerCount = new Label("Time : "+timing+" s",skin2, "small");
back = new TextButton("Back", skin2, "small");
back.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
((Game) Gdx.app.getApplicationListener()).setScreen(new GameOption());
}
});
back.pad(10);
table.add(heading).colspan(3).expandX().spaceBottom(50).row();
table.add(inventoryActor2).uniformX().expandY().top().left().padLeft(30);
table.add(inventoryActor).top().spaceLeft(30);
table.add(back).bottom().right();
stage.addActor(table);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
/*if (Gdx.input.isKeyPressed(Input.Keys.ANY_KEY)) {
}*/
batch.begin();
batch.draw(texture, 10, 10);
batch.end();
inventoryActor.setVisible(true);
inventoryActor2.setVisible(true);
stage.act(delta);
stage.draw();
}
The problem is, that you never set your Labels text inside the show() method.
You only give it an initial text as a String in the constructor, so the Label doesn't know, that the timing has changed.
So you should set the Labels text in the render() before calling stage.draw().
i have the following class that is suppose to reference a font and then draw a string to the screen once an event has occured. the class is called like this .
if (grumpface.whiteballoon.getBoundingRectangle().overlaps(spriterect)) {
((Game) Gdx.app.getApplicationListener()).setScreen(new GameOverScreen());
}
;
the class it references it this but i still cant get the font to appear any suggestiosns?
class GameOverScreen implements Screen{
private Stage stage;
// Called automatically once for init objects
#Override
public void show() {
stage = new Stage();
stage.setDebugAll(true); // Set outlines for Stage elements for easy debug
BitmapFont white = new BitmapFont(Gdx.files.internal("hazey.fnt"), false);
LabelStyle headingStyle = new LabelStyle(white, Color.BLACK);
Label gameoverstring = new Label("game ovaaaa!", headingStyle);
gameoverstring.setPosition(100, 100);
stage.addActor(gameoverstring);
}
// Called every frame so try to put no object creation in it
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
The font color of your label is getting set to "black", which is the same as your background color.
So I have a splash image that is not rendering and I'm not really sure why. I have done the correct process, creating anew sprite and then in my render method rendering it with the batch like so:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
#Override
public void show() {
splashSprite = new Sprite(new Texture("data/xidstudios_splash.png"));
batch = new SpriteBatch();
}
As you can see, I have done the correct process of steps, am I missing one?
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
You must provide the viewports to opengl either using a camera or directly calling the opengl func so that your projection are currectly set to view the image