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

2017/4/27

IllegalStateException: Actor not in world regarding three objects

MarktheBrit MarktheBrit

2017/4/27

#
So I'm trying to make a fairly basic combat ai for a small fantasy game, but I'm having a tad bit of difficulty in the removing of an object. It works fine if it's just the player and another ai (player vs ai,) but if you add another ai to the mix(player vs ai vs ai) if one ai(team 1) targets another ai(team 2) if team2 is removed from the game team1 will not recognize the removal, but instead remain locked on the removed object and crash. Below is the combat method used in the two ai, Enemies() is a brief method that returns a class of an enemy. The code used in the player is just a basic removeObject, if it would be helpful to have let me know. Thanks
public void combat(int str, int dex, int con, int intel, int wis, int cha, int type, String imageName) {
        List<Living>   possTargets  = getObjectsInRange(700, Enemies());
        if ( possTargets.size() <= 0 )
        {
            target = null;
        }
        else
        {

            if (possTargets.size() == 0) {
            }
            else  {
                if (target == null ) {
                    possTargets  = getObjectsInRange(700, Enemies());
                    target = possTargets.get(Greenfoot.getRandomNumber(possTargets.size()));

                }
                int X = getX();
                int Y = getY();
                int tX = target.getX();
                int tY = target.getY();
                if (type == 0) {
                    //Up close and personal
                    if ( X  > target.getX() + 50) {
                        setLocation(X -(2+dex/2), Y);
                        setImage(imageName+ "a4.png");
                    }
                    else if (  X  < target.getX() - 50) {
                        setLocation(X + (2 + dex/2), Y);
                        setImage(imageName+ "a2.png");
                    }
                    else if (Y  < target.getY()-50) {
                        setLocation(X , Y+ (2 + dex/2));
                        setImage(imageName+ "a1.png");
                    }
                    else if (Y > tY + 50)    {
                        setLocation(X, Y - (2 + dex/2));
                        setImage(imageName+ "a3.png");
                    }
                }
                
                }
            }
        }
    }
Any help regarding this would be greatly appreciated, thanks!
danpost danpost

2017/4/27

#
Looks like you are repeating yourself a lot here. Lines 2 and 14 do exactly the same thing (the two returned lists will be identical); and lines 3 and 10 are checking the same thing (the size of the list will never be less than zero, so the checks are virtually identical). Remove lines 10 through 12 and lines 14 and 42. Now, change line 13 to this:
if (target == null || target.getWorld() == null) {
The second part checks to see if the currently set target has been removed from the world.
MarktheBrit MarktheBrit

2017/4/27

#
That works, thanks a ton!
You need to login to post a reply.