Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [libGDX] [box2D] TextButton don't work  (Read 1188 times)
0 Members and 1 Guest are viewing this topic.
Offline SrFurias

Senior Newbie





« Posted 2013-07-22 19:48:09 »

Hi, I have a problem. I have a TextButton implemented on my MenuScreen, but it don't work. When I click it, it don't do anything...

This is my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
package com.unclain.udevelop.prueba1;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
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.Stage;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;

public class MenuScreen implements Screen{

   private Stage stage;
   private SpriteBatch spriteBatch;
   private Skin skin;
   private TextureAtlas textureAtlas;
   private TextButton textButton;
   private TextButtonStyle textButtonStyle;
   private BitmapFont buttonLevelsFont;
   private Texture background;
   private Sprite window;
   private Texture textureWindow;
   private Sprite mainImage;
   
   public MenuScreen instance = this;
   
   public GameScreen gamescreen;
   
   public MenuScreen(GameScreen gamescreen){
      this.gamescreen = gamescreen;
   }
   
   @Override
   public void render(float delta) {
      // TODO Auto-generated method stub
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      Gdx.gl.glClearColor(0, 0, 0, 1);
      stage.act(delta);
     
      spriteBatch.begin();
      spriteBatch.draw(background,0, 0);
      window.draw(spriteBatch);
      mainImage.draw(spriteBatch);
      mainImage.draw(spriteBatch);
      buttonLevelsFont.draw(spriteBatch, "Demostraci├│n de mec├ínicas de Unclain Entertainment 2013 - Powered by libGDX and Box2D physics.", 6, 18);
      spriteBatch.end();
      stage.draw();
     
      if(Gdx.input.isKeyPressed(Keys.ENTER)) gamescreen.udevelop.setScreen(new ScreenLevels(instance));
     
   }

   @Override
   public void resize(int width, int height) {
      // TODO Auto-generated method stub
      stage.addActor(textButton);
      background.setFilter(TextureFilter.Linear, TextureFilter.Linear);
   }

   @Override
   public void show() {
      Gdx.input.setInputProcessor(stage);
      stage = new Stage();
      spriteBatch = new SpriteBatch();
      textureAtlas = new TextureAtlas(Gdx.files.internal("AdyliumUserInterface.pack"));
      skin = new Skin(textureAtlas);
     
      buttonLevelsFont = new BitmapFont();
     
      textButtonStyle = new TextButtonStyle();
      textButtonStyle.up = skin.getDrawable("BotonNivelesA");
      textButtonStyle.down = skin.getDrawable("BotonNivelesB");
      textButtonStyle.over = skin.getDrawable("BotonNivelesB");
      textButtonStyle.
     
      textButton = new TextButton("", textButtonStyle);
      textButton.setX(Gdx.graphics.getWidth() / 2 - (textButton.getWidth() / 2));
      textButton.setY(Gdx.graphics.getHeight() / 2);
      textButton.setTouchable(Touchable.enabled);
     
      background = new Texture(Gdx.files.internal("BackgroundMenuScreen1.png"));
     
      textureWindow = new Texture(Gdx.files.internal("WindowMenuScreen.png"));
      window = new Sprite(textureWindow);
      window.setX(Gdx.graphics.getWidth() / 2 - (window.getRegionWidth() / 2));
      window.setY(Gdx.graphics.getHeight() / 2 - (window.getRegionHeight() / 2) + 20);
      window.scale(0.1f);
           
      Texture textureMainImage = new Texture(Gdx.files.internal("Logo.png"));
      mainImage = new Sprite(textureMainImage);
      mainImage.setX(Gdx.graphics.getWidth() / 2 - 128);
      mainImage.setY((Gdx.graphics.getHeight() / 2) + 86);
     
   }

   @Override
   public void hide() {
     
   }

   @Override
   public void pause() {
     
   }

   @Override
   public void resume() {
     
   }

   @Override
   public void dispose() {
      skin.dispose();
      stage.dispose();
      textureAtlas.dispose();
      buttonLevelsFont.dispose();
   }

}


Why it don't work?

Thanks.
Offline davedes
« Reply #1 - Posted 2013-07-22 21:17:29 »

You are doing some awfully weird things here..

  • You are rendering your actors manually instead of adding them to the stage and letting stage do the rendering.
  • You are adding a text button every time resize() is called.
  • You are changing the filter on a texture every time resize() is called -- no reason to do that..
  • You are allocating a lot of objects every time the Screen is shown. So if somebody opens your Game screen, and then returns to your Menu screen, all those objects are re-created, which can lead to some big issues. You are also loading Textures here; you should not be doing that every time the screen is shown, as it will lead to eventually running out of memory.
  • You are creating a new Screen every frame that the ENTER key is pressed.. ? At the very least; you should only do this on keyDown input. Using isKeyPressed checks every frame.
  • You have a syntax error in your "textButtonStyle" block of code. This probably won't even compile..
  • For UI elements it's much better to use tables for layout instead of specifying absolute positions and using magic numbers
My suggestion would be to "start small." Write a game with LibGDX that doesn't rely on multiple screens. Maybe Pong or Tic-Tac-Toe. Get familiar with Scene2D UI just using the default skin.

Here is an example of a menu that uses the default skin and a Table for layout:
http://www.java-gaming.org/topics/easy-way-to-click-an-image/29386/msg/269651/view.html#msg269651

You can see how simple the code for that looks:
https://gist.github.com/mattdesl/5461944

Offline ReBirth
« Reply #2 - Posted 2013-07-23 12:58:41 »

Move object creation to constructor. And about TextButton, doesn't you forget to add listener to it?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nexsoftware

Junior Devvie


Medals: 9



« Reply #3 - Posted 2013-07-23 13:19:31 »

As for why the TextButton doesn't do anything...

You call Gdx.input.setInputProcessor(stage) before instantiating the stage. Also, you don't add an event listener, so even once the stage begins to process input, it still won't do anything until you do that. The other comments also apply - there are very 'wrong' things being done here.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!