How is it possible for both the If AND the else statement to trigger?

  I'm making an rts with Java and swing and have come across a problem with ordering my units to build. My unit can only build the last item of a [b]Building[/b] placed in an ArrayList. The last Building works fine as far as being set as the current construction task, but any before call both the [b]if[/b] and the [b]else[/b] statements that prevent that from happening. The way I have my game working ; A button is pressed to create a building, you place the building, the currently selected builder teleports to it and begins building. The problem happens when multiple buildings are placed since the previous Buildings are not buildable since the [b]else[/b] statement is triggering for the other buildings.

Here I have the code that sets the current building task of my Builder Entity :

public void mousePressed(MouseEvent e) { 
        for(Iterator<Building> buildingIter = buildinglist.iterator();buildingIter.hasNext();){
            Building building = buildingIter.next();
            for(Iterator<game.Entity> entityIter = EntityManager.entitylist.iterator(); entityIter.hasNext();){
                game.Entity entity = entityIter.next();
                
                float mouseX = e.getX();
                float mouseY = e.getY();
              
                if(e.getButton() == MouseEvent.BUTTON3 && entity.getSelected() && entity instanceof EntityBuilder){
                    if(building.getRect().contains(mouseX, mouseY)){
                        entity.setCurrentConstruction(building);
                        System.out.println("Engineer : SIR!");
                    }
                    
                    else{
                    	entity.setCurrentConstruction(null);
                    }
                }
            }
        }
    }

Here is the mousePressed() method of my EntityBuilder:

public void mousePressed(MouseEvent e) {
		// To change body of generated methods, choose Tools | Templates.

		int mousex = e.getX();
		int mousey = e.getY();
		int speed = 3;

		if (soldierrectangle.contains(mousex, mousey) && e.getButton() == MouseEvent.BUTTON1 && selected == false) {
			selected = true;
			EntityManager.selectedEntity = this;
		}
		if (!soldierrectangle.contains(mousex, mousey) && e.getButton() == MouseEvent.BUTTON1 && selected == true && BuildingManager.currentBuilding == null) {
			speed = 0;
			selected = false;
			setPressed(false);
		}

		if (!soldierrectangle.contains(mousex, mousey) && e.getButton() == MouseEvent.BUTTON3 && selected == true) {
			double distance = Math
					.sqrt(Math.pow(mousex - soldierrectangle.x, 2) + Math.pow(mousey - soldierrectangle.y, 2));
			velocityx = ((mousex - soldierrectangle.x) / distance) * speed;
			velocityy = ((mousey - soldierrectangle.y) / distance) * speed;
			targetx = mousex;
			targety = mousey;

		}
		

	}

Here is the Update() method of my EntityBuilder, but this should not be relevant I believe:

public void update() {
		
		/* Walk towards Current Construction Assignment */
		if(currentConstruction != null ){
			
			if(currentConstruction.isPlaced()){
			 x = currentConstruction.getPositionX();
			 y = currentConstruction.getPositionY();
			}
			
		}
		
		

		if (!soldierrectangle.contains(destinationrectangle)) {
			x = x + velocityx;
			y = y + velocityy;
		}

		// If the player is in the radius of the building, begin building
		if (currentConstruction != null) {
		 if(isBuilding == false){
			if (Math.abs(x - currentConstruction.getPositionX()) < 20
					&& Math.abs(y - currentConstruction.getPositionY()) < 20) {
				System.out.println("Engineer : I'm in position!");
				isBuilding = true;
			}
		 }
		}

		// Begin building
		if (isBuilding) {
			targetx = x;
			targety = y;
			deltaBuildTime += .01f;
			
		if(currentConstruction!= null){
			if(deltaBuildTime >= currentConstruction.getBuildTime()){
				currentConstruction.setBuilt(true);
				isBuilding = false;
				currentConstruction = null;
				deltaBuildTime -= deltaBuildTime;
				System.out.println("Engineer: It's not much but it'll do");
			}
		}		
	}
		
		
		// Deselect building on movement

		if (currentConstruction == null && isBuilding == true) {
			isBuilding = false;
			System.out.println("Engineer : It was a damn prank call!");
		}

		soldierrectangle.setLocation((int) x, (int) y);
		destinationrectangle.setLocation((int) targetx, (int) targety);
		attack();

	}

This is a little hard to follow but I’m assuming you’re talking about the if-else statement in the first block of code you posted? If that’s the case then it should not be able to trigger both if-else statements in one action, due to the nature of if-else conditionals.

If you were to plug in a logger statement into the else block, would clicking once print both “Engineer : SIR!” and the logger in the else block?

Have you tried running in debug mode to see exactly what happens when you press down?

I have been using clicklisteners throughout my LibGDX project with a lot of success, which makes things much easier:


button.addListener(new ClickListener() {

            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                // Do something...
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                // Do something...
            }

            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                // Mouse enters particular area...
            }
            public void exit(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                // Mouse exits particular area..
            }
}