removing object from game in Java (eclipse)

空扰寡人 提交于 2019-12-04 04:41:51

问题


lets get right into it, i have a 'handler' class which is full of getters and setters and it includes in it code which adds and removes object, it looks like this:

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

Note that 'GameObject' is a class and all objects extend that class
And then i create an object here,

if(mouseOver(mx, my, 840/2-100, 149, 200, 64)){
        game.gameState = STATE.Game;
        handler.addObject(new Player(0, 300, ID.Player,game.playerImg, game));
        handler.addObject(new BasicEnemy(700, 300, ID.BasicEnemy, game.enemyImg, game));
        handler.addObject(new F1Jutsu(400, 300, ID.F1Jutsu, game.f1jutsuImg, game));
    }

The stuff inside parameters are the parameters of the object I want to add. Each object is its own class of course. Now i want to remove the object F1Jutsu if it's x value is outside the game, and the object moves to the right every second (which works so i wont paste it here, will do if asked)

        if(x > 800){
        handler.removeObject(this);
    }

I have this inside a 'tick' function (inside the F1Jutsu class) which is like a run function. The problem is, that as soon the removeObject method is called i get a nullpointer exception, the following error:

Exception in thread "Thread-2" java.lang.NullPointerException
at com.ninja.main.F1Jutsu.tick(F1Jutsu.java:24)
at com.ninja.main.Handler.tick(Handler.java:14)
at com.ninja.main.Game.tick(Game.java:110)
at com.ninja.main.Game.run(Game.java:87)
at java.lang.Thread.run(Unknown Source)

Basically, there is a NullPointerException (im removing a null?? but its an object) at the F1Jutsu class where it is being told to remove object, and then every place which is calling the remove object method and everywhere that is calling the place calling the remove object etc. is put causing an error.
I think the idea might be that the object's x value is null now which is the nullpointer(?) but not sure, and if so how would i fix this?
Im sorry for the long post (potato?)
EDIT:

line 24 of F1Jutsu is:

if(x > 800){
        handler.removeObject(this);
    }

MRK im not sure what you mean, i included the part where im adding the object and where im removing in the code above.
EDIT:
Okay, after much work i have come to the conclusion that when i add the object it is added as a null image. I must ask how do i set it to something (that is not null) without changing the basis of my code (the parameters)


回答1:


You shouldn't be calling the remove method in the middle of a block of code in the object your are removint; even though it is removed it will attempt to finish the code but will fail because all of it's variables would then be null. To fix this error, I suggest adding a boolean called removed in your GameObject class. Instead of calling handler.removeObject(this) simply set removed equal to true. You then need to add a few lines of code in your Handler class in the tick() method to check for and remove all objects with a removed value of true. Something like this

for (int i = 0; i < object.size(); i++) {
     if (object.get(i).removed)
          object.remove(i);
}


来源:https://stackoverflow.com/questions/30420793/removing-object-from-game-in-java-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!