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
Related
I have only found a couple of topics regarding this and it seems like everything I have is correct, but it doesn't work.
public Block(Body body, int x, int y, Player player, GameStage stage, GameScreen screen)
{
...
bar = progressBar();
stage.addActor(this);
stage.addActor(bar);
}
public ProgressBar progressBar()
{
ProgressBar.ProgressBarStyle barStyle;
TextureRegionDrawable textureBar;
Skin skin = new Skin();
Pixmap pixmap = new Pixmap(10, 10, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.WHITE);
pixmap.fill();
skin.add("white", new Texture(pixmap));
textureBar = new TextureRegionDrawable(new TextureRegion(new Texture("bar.png")));
barStyle = new ProgressBar.ProgressBarStyle(skin.newDrawable("white", Color.DARK_GRAY), textureBar);
barStyle.knobBefore = barStyle.knob;
bar = new ProgressBar(0f, maxHitPoints, maxHitPoints/30, false, barStyle);
bar.setPosition(this.x, this.y);
bar.setSize(20, 5);
bar.setAnimateDuration(2);
return bar;
}
public void draw(Batch batch, float parentAlpha)
{
super.draw(batch, parentAlpha);
batch.setColor(blockColor);
batch.draw(region, this.getX(), this.getY());
batch.setColor(Color.WHITE);
bar.setValue(hitPoints);
}
stage.act and stage.draw are called elsewhere but are being called.
I get a solid bar but nothing I try causes it to fill/unfill
I imagine I must be missing something stupid, but for the life of me I can't find out what.
So I have a game I am working on and once you finish playing I want to be able for the user to tap on the "Play Again" button and be able to reset at the start.
To do this I create a Rectangle over the Texture and use the contains() method.
if(cloud.contains((float)Gdx.input.getX(),(float)(Gdx.graphics.getHeight()-Gdx.input.getY()))){
reset();
}
Reset method:
public void reset(){
speed=0;
paraY = Gdx.graphics.getHeight() - para[parachuteState].getHeight();
gameState=0;
backY = 0-Gdx.graphics.getHeight();
bach=0;
Gdx.input.setCursorPosition(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
}
So what is happening is the program recognizes the button being pressed and resets but when the game is over again it automatically resets without displaying the end screen. I think that the cursor is not being moved to the center and is instead remaining on top of the button. Am I incorrectly using the setCursorPosition() method or is there another way to do this?
The button would be just right. It might looks more complex, but it's recommended way to do what you want to do.
So, to create a button you should do something like this:
Skin skin = new Skin();
skin.addRegions(uiTextureAtlas);
TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.up = skin.getDrawable("textureName");
buttonStyle.font = font;
Button button = new Button(buttonStyle);
button.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
reset();
return true;
}
});
button.setSize(100, 100);
button.setPosition(300, 400);
then in your Screen class you create
Stage stage = new Stage();
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
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 am trying to add to my game a less sliding menu(left and right) at the bottom of the screen.
My problem is that I can not adjust the table at the bottom of the screen, this is my code:
private void initUI() {
// inizializzazione dello stage
stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()/4));
Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
Gdx.input.setInputProcessor(stage);
// inizializzazione della tabella
container = new Table();
container.setFillParent(true);
stage.addActor(container);
Table table = new Table();
table.debug();
final ScrollPane scroll = new ScrollPane(table, skin);
scroll.setFillParent(true);
InputListener stopTouchDown = new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
event.stop();
return false;
}
};
table.pad(20).defaults().expandY().space(5);
for (int i = 0; i < 15; i++) {
table.row();
TextButton button = new TextButton(i + "dos", skin);
table.add(button);
button.addListener(new ClickListener() {
public void clicked (InputEvent event, float x, float y) {
System.out.println("click " + x + ", " + y);
}
});
}
container.add(scroll).expandY().fill().colspan(1);
container.row().space(10).padBottom(10);
}
render(){
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),Gdx.graphics.getHeight()/4);
Gdx.input.setInputProcessor(stage);
stage.draw();
stage.act(Gdx.graphics.getDeltaTime());
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
In this way I can split the screen into 2 parts (top and bottom), but the input is not correct: to activate the first button I have to press the top of the screen but the buttom is "designed" at the bottom.
How i can create a table with a scrollpane in the bottom of the screen?
There seems some misunderstanding of the viewport, table and how to use them:
Use one Viewport for your rendering your total screen. Split the screen up by using a Table. Hence change the first line in initUI to
stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight())); (remove the /4)
and adjust your render method to something like:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.
stage.act(delta);
stage.draw();
}
Clear your color buffer, to make sure that animations, or button-press operations are drawed correctly. Do not set stage as InputProcessor every frame call. Once this is done in initUI it is ok.
To the size of the Table: I assume that the table does not match the containing elements. Hence everytime you add a button with table.add(button) add the information about its height and width with table.add(button).height(...).width(...). Then the Table and the corresponding Scroll pane is adjusted right.
Currently I only see that container contains the scroll object that fills its parent that fills the whole screen. So make sure to add a further table to the container after you add the scroll to the container after container.row().space(10).padBottom(10);. This table will be drawn below the scroll pane.
THANK YOU, i have done, this is my new code:
// inizializzazione dello stage
stage = new Stage(new ExtendViewport(Constants.VIEWPORT_GUI_WIDTH,Constants.VIEWPORT_GUI_HEIGHT/4));
Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
Gdx.input.setInputProcessor(stage);
// inizializzazione della tabella
container = new Table();
container.setFillParent(true);
container.bottom();
stage.addActor(container);
Table table = new Table();
table.debug();
table.bottom();
final ScrollPane scroll = new ScrollPane(table, skin);
//scroll.setFillParent(true);
scroll.setupFadeScrollBars(0f, 0f);
InputListener stopTouchDown = new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
event.stop();
return false;
}
};
table.pad(20).defaults().space(5);
for (int i = 0; i < 15; i++) {
TextButton button = new TextButton(i + "dos", skin);
table.add(button).height(scroll.getHeight()).width(Constants.VIEWPORT_GUI_WIDTH/8);
button.addListener(new ClickListener() {
public void clicked (InputEvent event, float x, float y) {
System.out.println("click " + x + ", " + y);
}
});
}
container.bottom();
container.add(scroll).height(Gdx.graphics.getHeight()/3);//.expandY().fill().colspan(1);
container.row().space(10).padBottom(10);
I'm new to LibGDX and I am taking it slowly. I'm still trying to understand most things which is why typically google searches don't help due to the fact that their too complicated. I have a main menu that has text that I want centered no matter what the screen size is. Here is the code that I have for that menu.
public class Menu implements Screen {
SlingshotSteve game;
OrthographicCamera camera;
public Menu(final SlingshotSteve gam) {
this.game = gam;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.font.draw(game.batch, "Welcome to Slingshot Steve!!! ", 100, 150);
game.font.draw(game.batch, "Tap anywhere to begin!", 100, 100);
game.batch.end();
if (Gdx.input.isTouched()) {
game.setScreen((Screen) new GameScreen(game));
dispose();
}
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
I'm here to save you!
To get width / height from a String drawn with your BitmapFont you can utilize this super nice method:
game.font.getBounds(String string)
And to use it in your case, it would be something like this:
game.font.getBounds("Tap anywhere to begin!").width / 2;
Cheers!
It is possible to do it the way Nine Magics suggested, however one would usually do it via a Stage, which is part of scene2d.
More specifically one would use scene2d.ui which is a bunch of Actors like Button, Image, Label etc. You can attach a ClickListener to a Button for example and react on this event.
Furthermore, for layouting there is one very powerful Actor, namely Table which you can very easily use to center things on the screen.
Some very minimalistic code:
// do this once, in create() or show()
Skin skin = new Skin("uiskin.json"); // get the demo skin from the libgdx test resources
Stage stage = new Stage();
Table table = new Table(skin);
table.add("Welcome to Slingshot Steve!!!");
table.row();
table.add("Tap anywhere to begin!");
stage.addActor(table);
// do this in your render loop
stage.act();
stage.draw();
You can find the "default" skin resources here. You need to get all the uiskin.* files.