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

2013/12/23

How to pass direction?

1
2
Victor0409 Victor0409

2013/12/24

#
Well at first it did quite weird when I tried to let the world make the turret so I thought lets let the tank make the turret. But I already know a solution to that problem to work this crapy code out
Victor0409 Victor0409

2013/12/25

#
I still have the runtime error then.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MainWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainWorld extends World
{

    /**
     * Constructor for objects of class MainWorld.
     * 
     */
    public MainWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        TankTurret turret = new TankTurret();
        TankBase tank = new TankBase(turret);
        addObject(tank, 200, 200);
        addObject(turret, 198, 199);
    }
}
MainWorld code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class TankTurret here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TankTurret extends Actor
{
    /**
     * Creates the Turret and makes sure everything is all right.
     */
    public TankTurret()
    {

    }

    /**
     * Act - do whatever the TankTurret wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        turn();
        followTank();
    }    

    /**
     * Turn towards mouse.
     */
    public void turn()
    {
        if (Greenfoot.getMouseInfo() == null)
        {

        }
        else
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            turnTowards((int)mouse.getX(), (int)mouse.getY());
        }
    }

    /**
     * Takes care of the staying togheter of the tank and the Turret.
     */
    public void followTank()
    {

    }
    
    public void setLocation()
    {
        
    }
}
TankTurret code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class TankBase here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TankBase extends Actor
{
    TankTurret turret;

    /**
     * Creates the Tank and makes sure everything is all right.
     */
    public TankBase(TankTurret tankTurret)
    {
        this.turret = turret;
    }

    /**
     * Does what the tank does.
     */
    public void act() 
    {
        drive();
    }

    /**
     * Lets the Tank move with the WASD-keys.
     */
    private void drive()
    {
        if (Greenfoot.isKeyDown("w"))
        {
            move(5);
            turret.setLocation(getX(), getY());
        }
        if (Greenfoot.isKeyDown("s"))
        {
            move(-5);
            turret.setLocation(getX(), getY());
        }
        if (Greenfoot.isKeyDown("a"))
        {
            if(Greenfoot.isKeyDown("s"))
            {
                turn(4);
            }
            else
            {
                turn(-4);
            }
        }
        if (Greenfoot.isKeyDown("d"))
        {
            if(Greenfoot.isKeyDown("s"))
            {
                turn(-4);
            }
            else
            {
                turn(4);
            }
        }
    }
}
TankBase code
java.lang.NullPointerException
	at TankBase.drive(TankBase.java:37)
	at TankBase.act(TankBase.java:26)
	at greenfoot.core.Simulation.actActor(Simulation.java:568)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
The error
davmac davmac

2013/12/25

#
I don't think the error is in the code you've posted. It looks like, at some point, you construct another TankBase with a null turret.
Victor0409 Victor0409

2013/12/25

#
Can you tell me how to pass a variable from one actor to the other actor?
gnvrajareddy gnvrajareddy

2013/12/25

#
You can pass a variable from one actor to the other actor using constructors (parameterized) For example consider a Gun and a Bullet class code in the Gun class Bullet b=new Bullet(getRotation()); code in the Bullet class public Bullet(int rotation) { setRotation(rotation); } Here the bullet is created and at the same time it's rotation is set to the direction of the gun
Victor0409 Victor0409

2013/12/25

#
But then you won’t be able to use that variable in an other method in the bullet. And you won’t be able to change the variable of bullet1 during the program. I want to use this to pass the X and Y coordinate from the TankBase to the TankTurret .
danpost danpost

2013/12/25

#
Victor0409 wrote...
But then you won’t be able to use that variable in an other method in the bullet. And you won’t be able to change the variable of bullet1 during the program. I want to use this to pass the X and Y coordinate from the TankBase to the TankTurret .
That is where instance fields come in. These are variables that are acquired by each object created from that class. The values of the fields are independent from each other; that is, the value of a field in one object can be different from the value of the same field in another. These fields remain as long as the object remains in existence. These values are sometimes referred to as the 'state' of the object. For a bicycle, the current state could be: gear position, gear ratio, current speed, turning radius, seat height, facing direction, steering position, or even rider; anything that can be used to describe the permanent or current state of the object, There is a worms scenario where the number of worms eaten is counted. The field 'wormsEaten' is an instance field. Refer to that tutorial scenario to see how to declare instance field. Or, better yet, refer to the Java tutorial page on Member variables, which is another name for instance field.
Victor0409 Victor0409

2013/12/27

#
Thanks
You need to login to post a reply.
1
2