简体   繁体   中英

How to check if a certain method has been called?

I am trying to make a pop effect for this game that I am creating, and I have run into a problem when trying to check if the method has been called.

I have already took a stab at what I think should work the only problem is the Boolean method, and I have created always returns true?

Here is the code for the class that I think is causing the problem:

public class Handler {
    LinkedList<GameObject> object = new LinkedList<GameObject>();
    public static boolean blank = false;
    public void tick(){
        for(int i = 0; i < object.size();i++){
            GameObject tempObject = object.get(i);

            tempObject.tick();
        }

    }

    public void render(Graphics g){
        for(int i = 0; i < object.size();i++){
            GameObject tempObject = object.get(i);

            tempObject.render(g);
        }

    }

    public void addObject(GameObject object){
        this.object.add(object);
    }

    public void removeObject(GameObject object){
        blank = true;
        this.object.remove(object);
    }
    public static boolean hasObjectRemoved(){
        if(blank = true){
            blank = false;
            return true;
        }
        return false;
    }

}

As you can see, I am trying to check whether or not the removeObject method has been called or not.

if(blank = true)

Should be

if(blank == true)

= is assigning the value of true to the variable blank

== will ask the question is blank equal to true

把它放在你的测试方法中

System.out.println("removeObject method");

When you use blank = true , you are assigning a value rather than comparing the value.

While you can replace it with blank == true , which will work fine, it's unnecessary to use the == operator at all. Instead, use if (blank) to check if it is true, and if (!blank) to check if it is false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM