I have a Mother Ship that I created, and when I click on it I want the camera to follow it. Now that part is working correctly, but what is not working is deselection. Whenever the mouse is not on the Mother Ship and I left click, I want it to deselect it. In my Mother Ship class I have variables that say when the mouse is hovered, and whether the ship is selected. And in another class I have an infinite loop that updates and renders the ships in an array, and has an ‘currentShip’ object that gets set to the currently selected ship. What am I doing wrong and how can I fix or improve my code? I believe the problem lies with how I am getting the current selected ship, but I’m unsure.
In Mother Ship’s Update Method :
if(MousePos.x < Position.x + Width && MousePos.x > Position.x
&& MousePos.y < Position.y + Height && MousePos.y > Position.y){
isHovered = true;
}
if(MousePos.x < Position.x + Width && MousePos.x > Position.x
&& MousePos.y < Position.y + Height && MousePos.y > Position.y && Gdx.input.isButtonPressed(Buttons.LEFT)){
setSelected(true);
}
else if(isHovered == false && Gdx.input.isButtonPressed(Buttons.LEFT)){
setSelected(false);;
}
In other class:
for(Iterator<Ship> shipIter = shipList.iterator(); shipIter.hasNext();){
Ship ship = shipIter.next();
ship.render(batch);
ship.update(mousePos);
if(ship.isSelected()){
currentShip = ship;
camera.position.set(currentShip.getPosition().x,currentShip.getPosition().y, 0);
}
else {
currentShip = null;
}
}