问题
I'm trying to make button in slick by extending AbstractComponent:
public class StandardButton extends AbstractComponent {
protected int x;
protected int y;
protected int Width;
protected int Height;
protected String Text;
public StandardButton(GUIContext container,int x, int y, String Text, int Width, int Height,ComponentListener listener) {
super(container);
this.Text=Text;
setLocation(x, y);
this.Width=Width;
this.Height=Height;
addListener(listener);
}
@Override
public void render(GUIContext container, Graphics g) throws SlickException {
g.setColor(Color.white);
g.setLineWidth(2f);
g.drawRect(x, y, Width, Height);
g.drawString(Text, x+5, y+(Height/2-4));
}
[...]
And in my state:
public class UpdaterState extends BasicGameState implements ComponentListener { private StandardButton buttonPlay;
@Override
public void init(GameContainer container, final StateBasedGame game)
throws SlickException {
buttonPlay=new StandardButton(container,100,100,"Graj",60,30,new ComponentListener(){
@Override
public void componentActivated(AbstractComponent source) {
State.nextState(0, game);
}
});
But nothing happens. My nestState method does not run when i click my button. How schould I do that correctly? I just want my program to do block in componentActivated method. Is that possible or i just need to chceck if mouse is released on my button field in update method all the time for all buttons?
回答1:
I would recommend you take a quick peak at buckys videos which give you helpful tips on making stuff with slick 2d.
http://www.youtube.com/watch?v=AXNDBQfCd08&feature=share&list=PL22EF3E3752768216
That's a link to the playlist of his tutorials. One of the videos talks about making buttons and switching states. Good luck and I hope you make an amazing program :)
来源:https://stackoverflow.com/questions/17970481/slick2d-gui-button-listener