This site requires JavaScript, please enable it in your browser!
Greenfoot back
delijamiki
delijamiki wrote ...

2018/2/26

Need help

delijamiki delijamiki

2018/2/26

#
 public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
         
        if(!getWorld().getObjects(om.class).isEmpty()) 
        getWorld().removeObject(getWorld().getObjects(om.class).get(0));
        
        
        if (getWorld().getObjects(xm.class).isEmpty())
        getWorld().removeObject(getWorld().getObjects(xm.class).get(0));
         
         
         
        }
        
    }  
i need to delete object of "xm" and "om" classes , but with this code it throws me a indexoutofbounds expresion.Why?
danpost danpost

2018/2/26

#
What is the difference between lines 6 and 10 other than the class name?
delijamiki delijamiki

2018/2/26

#
Sorry, there is no difference, line 10 should be like this if (!getWorld().getObjects(xm.class).isEmpty())
danpost danpost

2018/2/26

#
Are you wanting to remove all objects of the 'om' and 'xm' classes, or does it have to be just one of each type.
delijamiki delijamiki

2018/2/26

#
There is only one instance of object of om and xm
danpost danpost

2018/2/26

#
delijamiki wrote...
There is only one instance of object of om and xm
Then just use:
getWorld().removeObjects(getWorld().getObjects(om.class));
and similar for the 'xm' class. To note, I do not see how you would get the IndexOutOfBoundsException with what you had above.
You need to login to post a reply.