[Solved] Selecting and deselecting objects?

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

I noticed you never seem to actually set the hovering to false? It’s either true or nothing. Try

if(MousePos.x < Position.x + Width && MousePos.x > Position.x
            && MousePos.y < Position.y + Height && MousePos.y > Position.y){
         isHovered = true;
      }else{
         isHovered = false;
}

Also, your code that checks if the ship is selected could be simply

if(isHovered && Gdx.input.isButtonPressed(Buttons.LEFT)){
         setSelected(true);
      }

EDIT:

Looks like you need some camera handling code as well to set a default location if there is no selected ship

P.S: Cute avatar :smiley:

Ok thank you, the selecting works now, but now I have another problem. When I click on the Mother Ship, the camera moves towards the ship but it doesn’t select it. Upon clicking it for the second time is when the Mother Ship actually gets selected, any ideas? Also, thank you for the avatar compliment! ;D

EDIT: Actually the mouse clicking just seems to be unresponsive with the code. I just tested it again and clicked 7 times before it actually selected the Ship, all the while each time I clicked it moved the camera to the ship.

I’m having trouble picturing what you’re describing. So the camera moves to the location of the ship and then what? It follows the ship around? It stops following?

Try throwing a break in the loop btw. I’ve had tons of issues before where breaking actually helps and also if the ship is found and selected, why would we continue looping?

 camera.position.set(currentShip.getPosition().x,currentShip.getPosition().y, 0);
break;

It immediately goes to the position of the ship but does not follow. It’s like the isSelected variables gets turned on and off really quickly.

Ok, so I believe I see what the problem is. My Mother Ship goes moving to a certain location on Start-up, and while it is moving if I click it the camera moves and I have to click again as fast as possible before the camera is not in the same position as the Mother Ship. So for some reason the camera has to be at the same position of the Mother Ship before it can be selected. Once my Mother Ship reached its destination and stopped, I could deselect and select withought a problem.

That’s… strange o.o I’m looking through the code to see if I can find the issue.

Did you change the code to this btw:

if(isHovered && Gdx.input.isButtonPressed(Buttons.LEFT)){
         setSelected(true);
      }

Yes I did. :smiley:

What about the break? I think that could be the issue.

Ok, so now I believe the problem lies with the fact that when I click on it the camera moves and somehow registers another click and ends up deselecting it because the mouse is not on it. hmmmmm

What break?

So I deleted the line that always sets the camera position to the selected ship and it works fine. Now I just need to figure out how to set it without it destroying itself. Maybe a smooth transition will work better rather than setting it instantly.

Try it ;D as I said, I’ve never dealt with this before, I usually just set the camera on the player. I’m still new to game dev ::slight_smile:

Sure enough it works, and looks much better at that!

private void updateFollowCamera(OrthographicCamera camera) {
		
		if(camera.position.x < currentShip.getPosition().x){
			camera.position.x += currentShip.getSpeed()+1;
		}
		if(camera.position.x > currentShip.getPosition().x){
			camera.position.x -= currentShip.getSpeed()+1;
		}
		if(camera.position.y < currentShip.getPosition().y){
			camera.position.y += currentShip.getSpeed()+1;
		}
		if(camera.position.y > currentShip.getPosition().y){
			camera.position.y -= currentShip.getSpeed()+1;
		}

I’m glad you fixed it! It feels great when something finally works. I’m currently learning Box2D and trying to not pull my hair out at times :persecutioncomplex: