Search code examples
javaslick2djbox2d

slick2d - can't get mouse click event to work correctly


Yo!. I'm trying to make a small hobby game in Java using Slick2d, JBox2d and Tiled, but I've come across an annoying problem. My game currently consists of a spaceship flying around in a small stage. I've managed to get the movement just right, and even attachad a small cannon that rotates to follow the cursor. The camera is focused so that the ship is centered, always. My problem is that I can't get any of the methods that tell me if a mousebutton has been pressed to work. They work just fine when the ship is standing still, but when it picks up speed it stops working.. And when the ship has reached maximum speed it works again! So mouse button events are not firing while the ship is accelerating!? Please give help me out if you have some idea of what might be happening :D :D

ship is centered, cannon follows mouse. when ship is accelerating mouse press events are not working?? why?

The game state:

package code;

import java.util.ArrayList;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

public class Play extends BasicGameState {
    public final int ID;
    public final int PPM = 20;

    public World world;
    public Player player;
    public ArrayList<BasicObject> pool = new ArrayList<BasicObject>();
    public TiledMap map;
    public Camera camera;

    public Play(int ID) {
        this.ID = ID;
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        world = new World(new Vec2(0, 0));
        map = new TiledMap("res/map0.tmx");
        camera = new Camera(0, 0, gc.getWidth(), gc.getHeight());
        gc.getGraphics().setBackground(Color.white);

        spawn();
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        g.translate(camera.x, camera.y);
        map.render(0, 0);
        for (BasicObject bo : pool) {
            bo.draw(g, PPM);
        }

        player.setAlpha((float) Math.toDegrees(Math.atan2(
                (((gc.getHeight() - Mouse.getY()) - camera.y) - player.getCenterY()*PPM),
                ((Mouse.getX() - camera.x) - player.getCenterX()*PPM)
                )));

        player.draw(g, PPM);

        //g.drawLine(player.getCenterX()*PPM, player.getCenterY()*PPM, Mouse.getX()-(camera.x), (gc.getHeight() - Mouse.getY())-(camera.y));
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException { 
        Input input = gc.getInput();
        player.update(delta);
        for (BasicObject bo : pool) {
            bo.update(delta);
        }

        if (input.isKeyDown(input.KEY_A)) {player.move("left", delta);}
        if (input.isKeyDown(input.KEY_D)) {player.move("right", delta);}
        if (input.isKeyDown(input.KEY_W)) {player.move("up", delta);}
        if (input.isKeyDown(input.KEY_S)) {player.move("down", delta);}
        if (Mouse.isButtonDown(0)) {
            System.out.println("mousepressed");
        }


        //player.getBody().applyForceToCenter(new Vec2((Mouse.getX()-(camera.x))/PPM, ((gc.getHeight() - Mouse.getY())-(camera.y))/PPM).sub(player.getBody().getPosition()));

        float deltaF = (float) delta/1000;
        world.step(deltaF, 6, 2);

        camera.x = player.getBody().getPosition().mul(PPM).mul(-1).x + gc.getWidth()/2;
        camera.y = player.getBody().getPosition().mul(PPM).mul(-1).y + gc.getHeight()/2;
    }

    public void spawn() {
        for(int i=0;i<map.getObjectCount(0);i++) {
            float theX = map.getObjectX(0, i);
            float theY = map.getObjectY(0, i);
            float theWidth = map.getObjectWidth(0, i);
            float theHeight = map.getObjectHeight(0, i);

            addWall(theX, theY, theWidth, theHeight);
        }

        for(int i=0;i<map.getObjectCount(1);i++) {
            if (map.getObjectType(1, i).equals("Player")) {
                float theX = map.getObjectX(1, i);
                float theY = map.getObjectY(1, i);
                float theWidth = 40;
                float theHeight = 60;

                player = new Player((theX + theWidth/2)/PPM, (theY + theHeight)/PPM, theWidth/PPM, theHeight/PPM, world);
            }
        }
    }

    public void addWall(float x, float y, float width, float height) {
        pool.add(new Wall((x + width/2)/PPM, (y + height/2)/PPM, width/PPM, height/PPM, world));
    }

    @Override
    public int getID() {
        // TODO Auto-generated method stub
        return ID;
    }
}

Here is the problem. Mouse.isButtonDown(0) doesn't work when the ship is accerating. I test it by pressing the button while the ship is moving and checking the console for output.

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException { 
    Input input = gc.getInput();
    player.update(delta);
    for (BasicObject bo : pool) {
        bo.update(delta);
    }

    if (input.isKeyDown(input.KEY_A)) {player.move("left", delta);}
    if (input.isKeyDown(input.KEY_D)) {player.move("right", delta);}
    if (input.isKeyDown(input.KEY_W)) {player.move("up", delta);}
    if (input.isKeyDown(input.KEY_S)) {player.move("down", delta);}
    if (Mouse.isButtonDown(0)) {
        System.out.println("mousepressed");
    }


    //player.getBody().applyForceToCenter(new Vec2((Mouse.getX()-(camera.x))/PPM, ((gc.getHeight() - Mouse.getY())-(camera.y))/PPM).sub(player.getBody().getPosition()));

    float deltaF = (float) delta/1000;
    world.step(deltaF, 6, 2);

    camera.x = player.getBody().getPosition().mul(PPM).mul(-1).x + gc.getWidth()/2;
    camera.y = player.getBody().getPosition().mul(PPM).mul(-1).y + gc.getHeight()/2;
}

Let me know if you need to see code from more classes, as I don't know where the problem lies, and therefore don't know what code to post :p


Solution

  • Ok. The answer to my problem is a little embarrassing.. :p Turns out it was my laptop that was the problem. It is for some reason not possible to use the buttons on the mousepad while typing xD I guess it's some kind of safety feature so you don't click random stuff with your palm while trying to type :)

    Plugged in a usb mouse and the program runs great! Thank you so much RPresle for trying to help out ;)