I've got a code that sets click, press and release booleans for every mouse button. However, while testing i noticed when pressing buttons simultaniously, the MouseInfo.getButton() only returned one of the two pressed buttons. This meant that the other button's pressing boolean was stuck at true.
Any idea on how to create a workaround for this?
Here's the code i'm using:
import greenfoot.*;
import java.awt.Point;
public class Base extends Actor
{
public static boolean update=false;
public static float UPDATE_FREQ=30;
private static long lastUpdate=0;
public static Point mousePos;
public static boolean[] click;
public static boolean[] pressing;
public static boolean[] clicked;
public Base()
{
mousePos=new Point();
click=new boolean[5];
pressing=new boolean[5];
clicked=new boolean[5];
}
public void act()
{
update();
if(update) //this resets the boolean arrays
{
click=new boolean[5];
clicked=new boolean[5];
}
basicMouseInfo();
}
public void update() //this lets subclasses work at a slower rate
{
long currentTime=System.currentTimeMillis();
long nextUpdate=(long)(lastUpdate+1/UPDATE_FREQ*1000);
if(nextUpdate<=currentTime)
{
lastUpdate=nextUpdate;
update=true;
return;
}
update=false;
}
public void setUpdateFreq(int freq) { if(freq>0 && freq<=120) UPDATE_FREQ=freq; }
public void basicMouseInfo() //this monitors the mouse position and status of mouse buttons
{
MouseInfo mouse=Greenfoot.getMouseInfo();
if(mouse!=null)
{
mousePos=new Point(mouse.getX(), mouse.getY());
int button=mouse.getButton();
if(button!=0)
{
if(pressing[button-1]) //if the button was already pressed (this is when the button is released)
{
pressing[button-1]=false;
clicked[button-1]=true;
return;
}
if(!click[button-1]) //if the button wasn't clicked before (this is when the button is first pressed)
{
click[button-1]=true;
pressing[button-1]=true;
}
}
}
}
}

