Game Development Reference
In-Depth Information
// + Play Button
btnMenuPlay = new Button(skinCanyonBunny, "play");
layer.add(btnMenuPlay);
btnMenuPlay.addListener(new ChangeListener() {
@Override
public void changed (ChangeEvent event, Actor actor) {
onPlayClicked();
}
});
layer.row();
// + Options Button
btnMenuOptions = new Button(skinCanyonBunny, "options");
layer.add(btnMenuOptions);
btnMenuOptions.addListener(new ChangeListener() {
@Override
public void changed (ChangeEvent event, Actor actor) {
onOptionsClicked();
}
});
if (debugEnabled) layer.debug();
return layer;
}
After this, add the following lines of code to the same class:
private void onPlayClicked () {
game.setScreen(new GameScreen(game));
}
private void onOptionsClicked () { }
The controls layer is anchored in the bottom-right corner of the screen. A new button
widget is added using the Play style. Next, a new ChangeListener is added to this
button to define the action to be executed when the button is clicked on.
We are using ChangeListener to register new handlers for our button
widgets. This is the recommended way of implementing handlers for
widgets as most of them will fire ChangeEvent when changes occur.
We could also use ClickListener to accomplish the detection of
clicks on button widgets, but doing so has a major drawback. The
ClickListener method reacts on the input events received by a
widget, but does not know anything about widgets and their properties.
Therefore, if a widget is set to be disabled, clicking on events will still be
detected and handled by the listener.
 
Search WWH ::




Custom Search