Mouse Event

5 replies [Last post]
Offline
Joined: 11/18/2012

How to get the mouse event for left and right click.  i tried the examples that i found in the scene creator

 and i tried to search for mouse event functions in the javadoc but my game still would not respond to the

mouse event i needed.

 

thank you again for always helping me 

Offline
Joined: 12/13/2010
There are two method in the

There are two method in the Env class that you can use to detect mouse events:

 

Env.getMouseButtonClicked() will return an integer indicating that a button is clicked, 0 is for the left mouse button and 1 is for the right mouse button.

 

Env.getMouseButtonDown(int) will return true or false based on if a mouse button is being held down.  The parameter tells the function which button to detect.  Once again, 0 for left mouse button and 1 for right mouse button.

 

Let me know if it works for you!

 

Jason

Offline
Joined: 11/18/2012
Mouse Events

I tried those functions and it wont respond to any of the mouse event .


 

if(env.getMouseButtonClicked() == 0){

  //do something

}

 

if(env.getMouseButtonDown(0)){

  // do something

}

 

i event tried the functions in org.lwjgl.input.Mouse; and still no luck .

 

Offline
Joined: 12/13/2010
Could you post a larger

Could you post a larger section of your code?  It looks like it would work...

Offline
Joined: 11/18/2012
Code

my code for the Game class

public Game(){
        env = new EnvAdvanced();
        getRoom = new Room(roomWidth,roomHeight,roomDepth);
        cubes = new ArrayList<GameObject>();
        zombie = new ArrayList<GameObject>();
        rand = new Random();
        cube = new Cube(0,0,0);
       
        for(int i = 0; i < 8; i++){
           if(i < 5){
               cubes.add(new Cube(rand.nextInt(60)+5,height,rand.nextInt(60)+5));
               zombie.add(new Zombie(rand.nextInt(65+5),height,rand.nextInt(65)+5));
           }
           else{
               zombie.add(new Zombie(rand.nextInt(65+5),height,rand.nextInt(65)+5));
           }
        }
       
        hunter = new Hunter(roomWidth/2,height,roomDepth/2,zombie);
    }
    public void play(){
       
        //env.setResolution(1280,780,24);
       
        env.setRoom(getRoom);
        getRoom.setCollisionNode(env,height);
       
        for(GameObject c: cubes){
            env.addObject(c.getEnvNode());
        }
        for(GameObject z: zombie){
            env.addObject(z.getEnvNode());
        }
        env.setCameraXYZ(roomWidth/2,height,roomHeight/2);
       
        env.addObject(hunter.getEnvNode());
       
        hunter.setEnv(env);
       
        env.setShowStatus(true);
        env.setDefaultControl(true);
       
        while(env.getKey() != 1){
           

            for(GameObject c: cubes){
                c.checkObjectCollision(cubes,hunter,zombie,env);
            }
            for(GameObject z: zombie){
                z.move(hunter,roomWidth,roomDepth);
            }
           
            hunter.setCameraFollow(env,hunter);
            hunter.setCameraMouseLook(env);
           
            getRoom.checkNodeCollision(hunter,zombie);
           
            if(env.getKeyDown() == Keyboard.KEY_W){
                hunter.setState(Hunter.RUN);
                hunter.playGraphics();
                hunter.moveX(0.4,roomWidth,roomDepth);
            }
            else if(env.getKeyDown() == Keyboard.KEY_S){
                hunter.setState(Hunter.BACKWARDS);
                hunter.playGraphics();
                hunter.moveX(-0.1,roomWidth,roomDepth);
            }
            else if(env.getKeyDown() == Keyboard.CHAR_NONE){
                hunter.setState(Hunter.IDLE);
                hunter.playGraphics();
            }
            else if(env.getKeyDown() == Keyboard.KEY_SPACE){
                hunter.setState(Hunter.MELEE);
                hunter.playGraphics();
            }
            //Mouse Event
             else if(env.getMouseButtonDown(0)){
                hunter.setState(Hunter.RANGED);
                hunter.playGraphics();
            }
           
            //env.setDisplayStr(zombie.getX()+" "+zombie.getZ());
           
            env.advanceOneFrame(40);
        }
        env.exit();
    }

Offline
Joined: 12/13/2010
Instead of doing an "else if"

Instead of doing an "else if" for the env.getMouseButtonDown(0) test, start a separate if branch, like so:

 

---

            else if(env.getKeyDown() == Keyboard.KEY_SPACE){
                hunter.setState(Hunter.MELEE);
                hunter.playGraphics();
            }


            //Mouse Event
            if(env.getMouseButtonDown(0)){
                hunter.setState(Hunter.RANGED);
                hunter.playGraphics();
            }

---

 

Since you are testing for keyboard all the time, if you use "else" for mouse input, it will never execute. 

 

Let me know if it works for you!

 

Jason