java - How to access other elements in click listeners in libgdx -


how modify lable info's text calling settext method?

for e.g. depending in button pressed want set label's text appropriately

i error when try accessing label :

cannot refer non-final variable inside inner class defined in different method

        skin skin = new skin(gdx.files.internal("uiskin.json"));         stage = new stage();         gdx.input.setinputprocessor(stage);         table = new table();         table.setfillparent(true);         stage.addactor(table);          string sentence = "one 2 3 4 5 6 7 eight";            string[] temp = sentence.split(" ");         arraylist<string> words = new arraylist<string>(arrays.aslist(temp));           info = new label( "welcome android!", skin );          for(int i=0; i<words.size();i++)         {              textbutton button = new textbutton( words.get(i), skin);             table.add(button);              button.addlistener(new clicklistener() {                 @override                 public void clicked(inputevent event, float x, float y) {                     gdx.app.log("button","clicked");                 //info.settext(integer.tostring(i)); how make work? //also how know button pressed?                 };             });          }           table.row();         table.add(info);           gdx.input.setinputprocessor(stage); 

william morrison listed number of ways can create clicklisteners. prefer method of anonymous classes myself , see method used in question.

you need make references outside of anonymous class final can reference them inside. how java ensures reference not change. @ code (and comments) complete solution, had change couple things. (i have skin file located @ 'skin/uiskin.json' keep in mind if wish use code).

import com.badlogic.gdx.applicationlistener; import com.badlogic.gdx.gdx; import com.badlogic.gdx.graphics.gl20; import com.badlogic.gdx.scenes.scene2d.inputevent; import com.badlogic.gdx.scenes.scene2d.stage; import com.badlogic.gdx.scenes.scene2d.ui.label; import com.badlogic.gdx.scenes.scene2d.ui.skin; import com.badlogic.gdx.scenes.scene2d.ui.table; import com.badlogic.gdx.scenes.scene2d.ui.textbutton; import com.badlogic.gdx.scenes.scene2d.utils.clicklistener; import java.util.arraylist; import java.util.arrays; import java.util.list;  public class clicktest implements applicationlistener {     private stage stage;     private skin skin;      @override public void create() {         gdx.app.log("create", "app opening");          this.skin = new skin(gdx.files.internal("skin/uiskin.json"));         stage = new stage();         gdx.input.setinputprocessor(stage);         table table = new table();         table.setfillparent(true);         stage.addactor(table);          string sentence = "one 2 3 4 5 6 7 eight";         string[] temp = sentence.split(" ");         list<string> words = new arraylist<string>(arrays.aslist(temp));          final label info = new label("welcome android!", skin);          (int = 0; < words.size(); i++) {             // make final here can reference inside             // anonymous class             final int index = i;               textbutton button = new textbutton(words.get(i), skin);             table.add(button);              button.addlistener(new clicklistener() {                 @override public void clicked(inputevent event, float x, float y) {                     // when click button print value assign.                     // way know 'which' button clicked , can perform                     // correct action based on it.                     gdx.app.log("button", "clicked " + index);                        info.settext(integer.tostring(index));                 };             });         }          table.row();         // changed centers label.         table.add(info).colspan(words.size()).expandx();           gdx.input.setinputprocessor(stage);          gdx.gl20.glclearcolor(0f, 0f, 0f, 1);     }      @override public void render() {         this.stage.act();         gdx.gl20.glclear(gl20.gl_color_buffer_bit);         gdx.gl20.glenable(gl20.gl_blend);         this.stage.draw();     }      @override public void dispose() {         gdx.app.log("dispose", "app closing");     }      @override public void resize(final int width, final int height) {         gdx.app.log("resize", width + "x" + height);         gdx.gl20.glviewport(0, 0, width, height);         this.stage.setviewport(width, height, false);     }      @override public void pause() { }      @override public void resume() { } } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -