Hi!
I had no answer on the help post, so i'm putting this here, just in case this actually is a bug.
I made a little project to pinpoint the bug (which i think it is).
It's a basic statebasedgame with two state. First one has a big abstractcomponent which triggers the start of the second one, the second one only capturing mouse events.
The first state has a component covering almost all the space. When you click on the second state, bam! The component on the first state actually catches it, and it never goes onto the second one (where there is actually no component rendered).
I've attached the source code in a zip, and here it is in this message :
TestApp.javaCode:
package slicktest;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
public class TestApp extends StateBasedGame{
private static TestApp _thisInstance;
public static TestApp getInstance() {
return _thisInstance;
}
public static final int MENUSTATE = 1;
public static final int MAINSTATE = 2;
public TestApp(String name) {
super(name);
_thisInstance = this;
this.addState(new MenuState());
this.addState(new MainState());
this.enterState(MENUSTATE);
}
@Override
public void initStatesList(GameContainer arg0) throws SlickException {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
AppGameContainer app;
try {
app = new AppGameContainer(new TestApp("test"));
app.setDisplayMode(640, 480, false);
app.setTargetFrameRate(60);
app.start();
} catch (SlickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
MenuState.javaCode:
package slicktest;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import component.BaseComponent;
public class MenuState extends BasicGameState implements ComponentListener {
List<BaseComponent> components = new ArrayList<BaseComponent>();
@Override
public void init(GameContainer gc, StateBasedGame arg1) throws SlickException {
BaseComponent b = new BaseComponent(gc,this,5,5,630,470);
b.setVisible(true);
components.add(b);
}
@Override
public void render(GameContainer gc, StateBasedGame arg1, Graphics gr) throws SlickException {
for (BaseComponent b : components){
b.render(gc, gr);
}
}
@Override
public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
// TODO Auto-generated method stub
}
@Override
public int getID() {
return TestApp.MENUSTATE;
}
@Override
public void componentActivated(AbstractComponent arg0) {
System.out.println("Button activated!");
TestApp.getInstance().enterState(TestApp.MAINSTATE);
}
}
MainState.javaCode:
package slicktest;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class MainState extends BasicGameState {
@Override
public void mouseReleased(int button, int x, int y) {
System.out.println("Captured in mainstate");
}
@Override
public void init(GameContainer gc, StateBasedGame arg1) throws SlickException {
}
@Override
public void render(GameContainer gc, StateBasedGame arg1, Graphics gr) throws SlickException {
gr.drawRect(50, 50, 500, 400);
gr.drawString("You are in the main state", 200, 240);
}
@Override
public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
// TODO Auto-generated method stub
}
@Override
public int getID() {
return TestApp.MAINSTATE;
}
}
BaseComponent.javaCode:
package component;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.GUIContext;
public class BaseComponent extends AbstractComponent {
protected int x, y, height, width;
protected boolean over;
protected boolean visible = false;
public BaseComponent(GUIContext container, ComponentListener listener, int x, int y, int width, int height) {
super(container);
this.x = x;
this.y = y;
this.height = height;
this.width = width;
if (listener != null){
addListener(listener);
}
}
@Override
public int getHeight() {
return height;
}
@Override
public int getWidth() {
return width;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
@Override
public void setLocation(int X, int Y) {
this.x = X;
this.y = Y;
}
@Override
public void mouseReleased(int button, int x, int y) {
if (listeners.size() > 0 && visible && x > this.x && x < (this.x + this.width)
&& y > this.y && y < (this.y + this.height)){
notifyListeners();
consumeEvent();
}
}
@Override
public void mouseMoved(int oldx, int oldy, int newx, int newy) {
if (newx > this.x && newx < (this.x + this.width)
&& newy > this.y && newy < (this.y + this.height)){
over = true;
}
else{
over = false;
}
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
@Override
public void render(GUIContext gc, Graphics gr) throws SlickException {
gr.setColor(Color.white);
gr.drawRect(x, y, width, height);
gr.drawString("TEST!!!!", x+width/2-gr.getFont().getWidth("TEST!!!!"), y+height/2-10);
}
}
Can you confirm that it is actually a bug or pinpoint what is wrong about this code?
Thanks!