1

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

Tholok
  • 53
  • 7
  • Please consider posting your code. We can't help you if you don't provide more details. – RPresle Oct 12 '15 at 06:25
  • You are right, I just posted some code. Please do let me know if I need to show you guys code from other classes, as I don't even know where the problem lies :p – Tholok Oct 12 '15 at 08:57
  • What is the complete path of your class Mouse? I can't find it in Slick2D. Have you considered use the inherited mouse listener? – RPresle Oct 12 '15 at 09:19
  • Here is the path to the Mouse class: org.lwjgl.input.Mouse; I have tried overriding the mouse input methods from BasicGameState, but I got the same problem. Is that what you mean by mouse listener? Or is there another way of getting mouse input I don't know about? thx so much for trying to help me out! :D – Tholok Oct 12 '15 at 14:06

2 Answers2

1

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 ;)

Tholok
  • 53
  • 7
0

I don't know much about the correspondence between LWJGL class and Slick's. Anyway I think you could use what the MouseListener give to you. You can override mouseClicked(int button, int x, int y, int clickCount) method or mousePressed.

@Override
public void mouseClicked(int button, int x, int y, int clickCount) {
    if(button == Input.MOUSE_LEFT_BUTTON){
        System.out.println("LeftMouseClicked");
    }else{
        System.out.println("Else button clicked");}
}

This is provide by the fact that BasicGameState inherits from MouseListener.

This should definitely work. Then you can set up flag to know when it is clicked from the update method

RPresle
  • 2,436
  • 3
  • 24
  • 28
  • I get the same problem. But I think I've isolated the problem. The problem isn't that the ship is accelerating, it's that other buttons are being pressed. When I press one of the movement buttons w,a,s,d, there is a delay before I can get any of the mouse methods to work. I'll try to override the key listener methods and use them instead and see if that fixes it. 2 secs – Tholok Oct 12 '15 at 14:29
  • That is quite a problem indeed... Using only Slick' Listener should do the work. I did succeed in doing such things... Hope it'll work – RPresle Oct 12 '15 at 14:40
  • It didn't work :( So the problem is: can't get mouse clicked event to fire while keyboard keys are being pressed... any time I press a key on the keyboard there is a time delay of about 2 seconds where the mouse methods do not fire. Doesn't matter if the keys are firing any methods in the program, any key on the keyboard ruins the mouse methods :'( !! – Tholok Oct 12 '15 at 14:41
  • Are you using Keyboard listener too? – RPresle Oct 12 '15 at 14:44
  • Tried adding that. But it doesn't have anything to do with that I think, cause I removed all the code reffering to any sort of key input and it still doesn't work. I'm beginning to suspect that it has something to do with my keyboard and mouse specifically but I've never had problems like this before in other games – Tholok Oct 12 '15 at 14:52