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

2018/1/24

Access variables defined in world in an actor

AnotherUser AnotherUser

2018/1/24

#
Hey! As the title would suggest I'm currently trying to acces the variable "short qFlapping" defined in world in an Actor. I tried using this but all I get is a NullPointerException. Here's the relevant part of the code found in world:
    public short qFlapping = 1;

    public short getQFlapping()
    {
        return this.qFlapping;
    }
Here's the relevant code found in the actor:
    PipePark thisWorld = (PipePark) getWorld();
    short qFlapping = thisWorld.getQFlapping();

        if(Greenfoot.isKeyDown("Space") && qFlapping == 1)
        {
            verticalMomentum = 12;
            qFlapping = 0;
        }
        if (!Greenfoot.isKeyDown("Space"))
        {
            thisWorld.getQFlapping() = 1;
        }
I also tried this but it also didn't work:
    PipePark thisWorld = (PipePark) getWorld();

        if (Greenfoot.isKeyDown("Space") && thisWorld.getQFlapping() == 1)
        {
            verticalMomentum = 12;
            thisWorld.getQFlapping() = 0;
        }
        if (!Greenfoot.isKeyDown("Space"))
        {
            thisWorld.getQFlapping() = 1;
        }
any possible help is highly appreciated!
danpost danpost

2018/1/24

#
The problem is probably just that you are using "Space" instead of "space" for detecting the state of the space bar.
AnotherUser AnotherUser

2018/1/24

#
I changed every "Space" to "space" but I still get a NullPointerException. My current code:
    PipePark thisWorld = (PipePark) getWorld();
    short qFlapping = thisWorld.getQFlapping();

        if(Greenfoot.isKeyDown("space") && qFlapping == 1)
        {
            verticalMomentum = 12;
            qFlapping = 0;
        }
        if (!Greenfoot.isKeyDown("space"))
        {
            qFlapping = 1;
        }
Any ideas?
danpost danpost

2018/1/24

#
Is the first line a field line outside of the code block or is it a variable declaration line in the code block? If outside, it is assigned its value during actor construction -- before the actor is actually in a world (hence 'getWorld' returns a 'null' value for the assignment of 'thisWorld').
AnotherUser AnotherUser

2018/1/24

#
Where does the code block start? The variables are located here:
public class Cube extends Actor
{
    PipePark thisWorld = (PipePark) getWorld();
    int verticalMomentum = 0;
    int qdVerticalMomentum = 0;
    short qFlapping = thisWorld.getQFlapping();

    public Cube()
    {
        GreenfootImage Image = new GreenfootImage(30, 30);
        Image.setColor(new Color(0, 0, 0));
        Image.fill();
        this.setImage(Image);
        setRotation(270);
    }

public void act()
{
 //something something
}

}
Super_Hippo Super_Hippo

2018/1/24

#
The lines outside methods are executed when a Cube object is created, before it is added to a world. So thisWorld is null (line 3) and you can't use a method on null (line 6).
AnotherUser AnotherUser

2018/1/24

#
where should I put them instead? if I put them in "public Cube()" it wont work
    public Cube()
    {
        PipePark thisWorld = (PipePark) getWorld();
        GreenfootImage Image = new GreenfootImage(30, 30);
        Image.setColor(new Color(0, 0, 0));
        Image.fill();
        this.setImage(Image);
        setRotation(270);
    }
Do I have to made additional methods to fetch the variable?
Super_Hippo Super_Hippo

2018/1/24

#
This even makes less sense than before. The constructor is executed when the object is created. Right after all the lines outside methods. And with the variable in the method, the reference to the variable is lost after the method executed. You can try this:
private short qFlapping;

//method which is automatically called when this object is added to a world
protected void addedToWorld(World world)
{
    qFlapping = ((PipePark) world).getQFlapping();
}
AnotherUser AnotherUser

2018/1/24

#
ah, I've got it. Makes all sense now. Thanks so much you two.
You need to login to post a reply.