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

2017/5/19

Player's actor unable to add objects?

alittle_bit alittle_bit

2017/5/19

#
Good day, A null-pointer error is occurring upon trying to adding new 'Heart' class objects to the world through the 'Player' actor. (the method which creates new objects in the world was added to the 'Player' class constructor. I wouldn't know if this is error causing however...) Would like an extra pair of eyes which could help pin-point what is causing the error:
    private void removeHearts()
    {
        getWorld().removeObjects( getWorld().getObjects(Health.class) );
    }
    
    private int[] heartsCoordX=
    {
        92,97,102,92,97,102,92,97,102
    };
    private int[] heartsCoordY=
    {
        16,21,26
    };
    private void createHearts(int healthNo)
    {
        int iX = 0;
        int iY = 0;
        //removeHearts();
        if(healthNo <4)
        {
            while(iX < healthNo)
            {
                getWorld().addObject(new Health(), heartsCoordX[iX],heartsCoordY[iY] );
                iX += 1;
            }
        }
        if(healthNo > 3 && healthNo < 7)
        {
            
            while(iX < healthNo)
            {
                getWorld().addObject(new Health(), heartsCoordX[iX],heartsCoordY[iY] );
                iX += 1;
                if(iX == 4)
                {
                    iY += 1;
                }
            }
        }
        if(healthNo > 6)
        {
            while(iX < healthNo)
            {
                getWorld().addObject(new Health(), heartsCoordX[iX],heartsCoordY[iY] );
                iX += 1;
                if(iX == 4)
                {
                    iY += 1;
                }
                if(iX == 7)
                {
                    iY += 1;
                }
            }
        }
    }
    
    public int healthCount = 3;
    public int healthChange(int callType)
    {
        
        if(callType == 1)
        {
            healthCount -= 1;
        }
        if(callType == 2)
        {
            healthCount -= 2;
        }
        return healthCount;
    }
    int health;
    private void health()
    {
        if(health != healthChange(0))
        {
            createHearts(health);
            healthChange(-health);
        }
        
        if(health == 0)
        {
            //Greenfoot.setWorld(new GameOver());
            //getWorld().showText("Would be what you could acknowledge if you had actually won", 42,32);
            //getWorld().removeObject(this);
        }
        health = healthChange(0);
    }
It can be noted that the 'removeHearts' method is commented as that method seemed to cause errors for me when running as well of which I do not grasp as to why either... (do I need to store the actor list in something else?) Let me know if you wish to see the error report.
danpost danpost

2017/5/19

#
alittle_bit wrote...
(the method which creates new objects in the world was added to the 'Player' class constructor.
Enough said. The constructor is executed before the player is (or can be) added into the world. This means that at that time, 'getWorld' will return 'null' (no world exists that is currently referenced by the hidden 'world' field for the player supplied by the Actor class. Use the 'addedToWorld(World)' method of the Actor class to add the Heart object(s) into the world.
alittle_bit alittle_bit

2017/5/19

#
Right! I always assumed that the Player's constructor ran the moment a Player object was added to the world & so you would of already had a world to run the getWorld() method in... Thanks
You need to login to post a reply.