java - LibGDX primitive keyboard with a listener for each key -


trying make simple typing game. i've created keyboard consisting of libgdx scene2d textbuttons , putting them in 3 scene2d tables (for each row of keys) , wrapping them in table. here's code far:

gdx.input.setinputprocessor(stage);  table keyboard = new table(); table keystop = new table(); table keysmid = new table(); table keysbot = new table();  final char ascii[] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',             'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',             'z', 'x', 'c', 'v', 'b', 'n', 'm'};  // create keys in order of ascii[] table above (int index = 0; index < ascii.length; index++) {     keys[index] = new textbutton("", skin);     string letter = character.tostring(ascii[index]);     keys[index].settext(letter);     keys[index].setskin(skin);      // , put them in correct rows     if (index < 10)         keystop.add(keys[index]).width(keysize).height(keysize + 5).pad(2);     else if (index < 19)         keysmid.add(keys[index]).width(keysize).height(keysize + 5).pad(2);     else         keysbot.add(keys[index]).width(keysize).height(keysize + 5).pad(2); }  // add each row of keys keyboard table keyboard.add(keystop).pad(5).expandx().fill().row(); keyboard.add(keysmid).pad(5).expandx().fill().row(); keyboard.add(keysbot).pad(5).expandx().fill().row();  stage.addactor(keyboard); 

now i'd add listeners each key, preferably in loop. putting following code @ end of loop:

keys[index].addlistener(new changelistener() {     @override     public void changed(changeevent event, actor actor) {         system.out.println(letter);     } }); 

doesn't compile because of error "variable 'letter' accessed within inner class, needs declared final". what's preferred (or simplest, if preferred difficult implement beginner) solution here?

  • declare letter string final

or

  • set name of keys[index] textbutton setname(string name) , inside changed method fetch targetlistener name using event.

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -