简体   繁体   中英

How to click to remove objects from array list in processing

Im trying to create a game where you click on an object to remove it. The code below is what I have for this section of the game but it gives the following error:

"Cannot invoke remove(int) on the array type mouseClick.Enemy[]"

Here is my code:

void mousePressed() {
    for(int i = 0; i < enemies.length; i++){
        float distance = dist(mouseX, mouseY, enemies[i].x, enemies[i].y); 

        if(distance < enemies[i].radius + 10) {
            enemies.remove(i);
        }
    }
}

Are there any other methods/functions I could use to implement this into my game?

As azurefrog said: The remove function can't be used on arrays. However, you can use that function with arrayLists. You can make a new ArrayList like this:

ArrayList<Enemy> yourList = new ArrayList<Enemy>();

Of course "yourList" is just a name you can choose. Just as Enemy is the class I assumed you are using. If you want an object out of that list you would also have to use

yourList.get(i); 

instead of

yourList[i];

Hope this helps!

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