在Java(eclipse)中从游戏中删除对象 [英] removing object from game in Java (eclipse)

查看:119
本文介绍了在Java(eclipse)中从游戏中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们直接进入它,我有一个处理程序类,它充满了getters和setters,它包括在其中添加和删除对象的代码,它看起来像这样:

  public void addObject(GameObject object){
this.object.add(object);
}
public void removeObject(GameObject object){
this.object.remove(object);请注意,GameObject是一个类,所有对象扩展该类




$ b $然后我在这里创建一个对象,

  if(mouseOver(mx,my,840 / 2-100,149, 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));
}

内部参数是要添加的对象的参数。每个对象是它自己的类当然。现在我想删除对象F1Jutsu如果它的x值在游戏之外,并且对象每秒向右移动(这是我不会粘贴在这里,如果问的话,会做)

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



我有一个'tick'函数就像一个运行函数。问题是,一旦removeObject方法被调用我得到一个nullpointer异常,以下错误:

 线程中的异常 Thread-2java.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)
在java。 lang.Thread.run(未知源)

基本上,有一个NullPointerException ?但它的一个对象)在F1Jutsu类,它被告知要删除对象,然后每个地方,调用remove对象方法和调用的地方调用remove对象等引起错误。

我想这个想法可能是对象的x值是null现在是nullpointer(?)但不确定,如果是这样,我将如何解决这个问题?
对于长篇文章(马铃薯?)很抱歉
编辑:



F1Jutsu的第24行是:

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

MRK不知道你的意思,对象和其中im删除在上面的代码。

编辑:

好​​吧,经过很多工作后,我得出结论,当我添加对象时,它被添加为空图像。我必须问我如何设置它的东西(不是null),而不改变我的代码的基础(参数)

解决方案

p>你不应该在对象中的代码块的中间调用remove方法,你的removevint;即使它被删除,它会尝试完成代码,但会失败,因为它的所有变量然后将 null 。要解决此错误,我建议在您的 GameObject 布尔 c $ c>类。而不是调用 handler.removeObject(this)只需设置删除等于 true 。然后,您需要在处理程序类中的 tick()方法中添加几行代码,以检查和删除删除 true 的所有对象。类似

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


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)

解决方案

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);
}

这篇关于在Java(eclipse)中从游戏中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆