开发者

Reason why my loop to find an object in an Array only works for the first time?

开发者 https://www.devze.com 2023-03-12 20:06 出处:网络
I am creating a program that displays several bases and the amount of troops each base has.There are two types of bases, friendly and enemy bases.Each Base extends GCompound and consists of a GRect an

I am creating a program that displays several bases and the amount of troops each base has. There are two types of bases, friendly and enemy bases. Each Base extends GCompound and consists of a GRect and a GLabel(to display the number of troops). Two arrays are used to keep track of the bases, one for friendly, one for enemy.

I want the user to be able to press the mouse down on one friendly base and release on a different friendly base, causing the troop amount to be transferred from the first base to the second one.

My problem currently is I am only able to detect the base the user presses the mouse down on, and not the base that the mouse is released on. I am using the method getElementAt from the ACM library to return the GObject that a mouse action takes place on.

Code for the mouse press:

public void mousePressed(MouseEvent e){
    int mouseX = e.getX();
    int mouseY = e.getY();
    for(int i = 0; i < PlayerBaseArray.length; i++){
        if(getClickedObject(mouseX, mouseY) == PlayerBaseArray[i]){  //Checks to see if the clicked base is in the Array of friendly bases.
            pressedIndex = i;
            pressedBaseTroopCount = PlayerBaseArray[pressedIndex].getTroopCount();
        }
    }
}

Code for the mouse release:

public void mouseReleased(MouseEvent m){
    int mouseX = m.getX();
    int mouseY = m.getY();
    for(int i = 0; i < PlayerBaseArray.length; i++){
        if(getClickedObject(mouseX, mouseY) == PlayerBaseArray[i]){ // This is always false for some reason.
            PlayerBaseArray[i].changeTroopCount(pressedBaseTroopCount);
            PlayerBaseArray[pressedIndex].changeTroopCount(-pressedBaseTroopCount);
        }
    }
} 

Method to see what object is clicked:

    private GObject getClickedObject(int x, int y){
    GObject 开发者_Go百科clicked = getElementAt(x, y);
    if(clicked == null) return null;
    else return clicked;
}

For some reason the if statement in mouseReleased() is never true, even though it works properly in mousePressed(). Any idea on why the if statement in mouseReleased() does not work?

I've tried researching the problem to no avail, and instead of wasting another night on it I thought I would ask here. Thanks!


You shouldn't use the == operator to compare Objects. You should use the .equals() method:

if (getClickedObject(mouseX, mouseY).equals(PlayerBaseArray[i]))

Put simply, you can only use == only when comparing java primitives (eg int, long etc).

For all "normal" objects, always use objecta.equals(objectb).

This article discusses this issue in more detail.



Attention anal retentives:

Yes, you are right, under some circumstances you can safely use == between objects such as String contants, Integers between -128 and 127, etc, etc. But this guy needed a clear answer and not to be confused. Please resist the temptation to comment about this.

0

精彩评论

暂无评论...
验证码 换一张
取 消