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

2017/2/6

Variables in other actors

Wasupmacuz Wasupmacuz

2017/2/6

#
I was just wondering if I wrote the programming correctly to use a variable in another actor In one sprite I have:
public static boolean jumping;
    /**
     * Act - do whatever the BottomSensor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(isTouching(Block.class))
        {
            jumping=false;
        }
        else
        {
            jumping=true;
        }
       
In the second there's:
private boolean jumping=BottomSensor.jumping;
    /**
     * Act - do whatever the Terrahead wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        jumping=BottomSensor.jumping;
    }
danpost danpost

2017/2/6

#
Jumping is something that a Terrahead object would do -- not what a BottomSensor object would do. Since it is a state of a Terrahead object, it (the field) should go in the Terrahead class as a non-static field (an instance field -- belonging to an instance of the class and not the class itself). However, since only a Terrahead object is to have a BottomSensor object, you can place the BottomSensor class directly inside the Terrahead class as a 'private' class. There, it will have full access to the 'jumping' field. You can have any Terrahead object add one into any world it is added into by making use of the 'addedToWorld' method:
protected void addedToWorld(World world)
{
    world.addObject(new BottomSensor(), getX(), getY()+getImage().getHeight()/2);
}
As a private class, the world will not be able to add one in by itself. But, by adding the Terrahead object into the world, a BottomSensor will be added also. In the act method of the BottomSensor class, you may want to include the following line:
if (Terrahead.this.getWorld() == null) getWorld().removeObject(this);
This will have the sensor removed from the world if ever its Terrahead object is removed.
Wasupmacuz Wasupmacuz

2017/2/6

#
danpost wrote...
Jumping is something that a Terrahead object would do -- not what a BottomSensor object would do. Since it is a state of a Terrahead object, it (the field) should go in the Terrahead class as a non-static field (an instance field -- belonging to an instance of the class and not the class itself). However, since only a Terrahead object is to have a BottomSensor object, you can place the BottomSensor class directly inside the Terrahead class as a 'private' class. There, it will have full access to the 'jumping' field. You can have any Terrahead object add one into any world it is added into by making use of the 'addedToWorld' method:
protected void addedToWorld(World world)
{
    world.addObject(new BottomSensor(), getX(), getY()+getImage().getHeight()/2);
}
As a private class, the world will not be able to add one in by itself. But, by adding the Terrahead object into the world, a BottomSensor will be added also. In the act method of the BottomSensor class, you may want to include the following line:
if (Terrahead.this.getWorld() == null) getWorld().removeObject(this);
This will have the sensor removed from the world if ever its Terrahead object is removed.
so I didn't do it correctly. Thanks Dan! I'll get back to you if I don't get it working.
You need to login to post a reply.