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/23

#
How will I be able to pass the direction my tank is pointing in to mu turret? these two are separate objects.
Victor0409 Victor0409

2013/12/23

#
And how do I have to let the turret move with the tank?
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
{
    int tankBaseAmount;
    int tankTurretAmount;

    /**
     * Creates the Tank and makes sure everything is all right.
     */
    public TankBase()
    {
        tankBaseAmount++;
    }

    /**
     * Does what the tank does.
     */
    public void act() 
    {
        drive();
        if (tankTurretAmount < tankBaseAmount)
        {
            getWorld().addObject (new TankTurret(), getX()+1, getY());
            tankTurretAmount = tankTurretAmount + 1;
        }
    }

    /**
     * Lets the Tank move with the WASD-keys.
     */
    private void drive()
    {
        if (Greenfoot.isKeyDown("w"))
        {
            move(5);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            move(-5);
        }
        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);
            }
        }
    }
}
The code from the tank
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.
     */
    private void turn()
    {
        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()
    {
        
    }
}
The Code from the turret
danpost danpost

2013/12/23

#
All you really have to do is to let the TankBase object 'know' about the'TankTurret object; and, not have the turret 'follow' the base, but have the base 'control' the location of the turret (it is the base the 'decides' where the turret should be).
// **********  in TankBase class **********
// add instance field
TankTurret turret;
// to set the turret field, either add this method
public void setTurret(TankTurret tankTurret)
{
    turret = tankTurret;
}
// or change the base constructor to this
public TankBase(TankTurret tankTurret)
{
    turret = tankTurret;
}
Then, in the world constructor, when creating your objects:
TankTurret turret = new TankTurret();
// to create base, if changing constructor, use
TankBase tank = new TankBase(turret);
// and if using the 'set' method, use
TankBase tank = new Tank();
tank.setTurret(turret);
For moving, in the TankBase act method (or wherever the movement code is, use
turret.setLocation(getX(), getY());
Victor0409 Victor0409

2013/12/23

#
Then I get an compiling error: cannot find symbol - variable TankTurret The tank turret in the tank base class in the constructor is highlighted
danpost danpost

2013/12/23

#
Did you follow the first 3 lines of code in the first set provided.
davmac davmac

2013/12/23

#
Victor0409, you need to re-post your code if you get an error. There's nothing wrong with the code danpost gave you, so you must have copied it incorrectly. From the sounds of it you are trying to use "TankTurret" as a variable name when you should be using either "turret" or "tankTurret".
Victor0409 Victor0409

2013/12/24

#
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(new TankBase(), 200, 200);
    }
}
is the code of the world
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
{
    int tankBaseAmount;
    int tankTurretAmount;
    TankTurret turret;

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

    /**
     * Does what the tank does.
     */
    public void act() 
    {
        drive();
        if (tankTurretAmount < tankBaseAmount)
        {
            getWorld().addObject (new TankTurret(), getX()+1, getY());
            tankTurretAmount = tankTurretAmount + 1;
        }
    }

    /**
     * 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);
            }
        }
    }
}
Is the code in the tank
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.
     */
    private void turn()
    {
        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()
    {
        
    }
}
Is the code in the turret.
danpost danpost

2013/12/24

#
Line 22 of your world class code creates 'another' TankBody object and adds 'it' into the world. You should be adding the one you already created on line 21 into the world -- the one called 'tank':
addObject(tank, 200, 200);
The error is coming from line 21 of the TankBase class, which should read:
this.turret = turret;
This was because you did not follow my instructions (lines 9 through 13) correctly.
Victor0409 Victor0409

2013/12/24

#
now I get an runtime error in the turning of the turret
java.lang.NullPointerException
	at TankTurret.turn(TankTurret.java:35)
	at TankTurret.act(TankTurret.java:25)
	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)
danpost danpost

2013/12/24

#
Unless a mouse action is detected, the MouseInfo object returned with 'getMouseInfo' will be 'null'. Just place a condition of executing the turn providing that the MouseInfo object does not return 'null'. Or, better yet, make sure a mouse movement has been detected using 'if (Greenfoot.mouseMoved(null))', because the turret should only have to change its rotation if the mouse has moved. This could even be checked in the act method, where only if the mouse has moved that you call 'turn'.
Victor0409 Victor0409

2013/12/24

#
Now when I run it I get this runtime error
java.lang.NullPointerException
	at TankBase.drive(TankBase.java:45)
	at TankBase.act(TankBase.java:29)
	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)
Victor0409 Victor0409

2013/12/24

#
when I try to drive
danpost danpost

2013/12/24

#
Show the class code (TankBase) as it is now. Also, show the code creating the TankBase object in your world class.
Victor0409 Victor0409

2013/12/24

#
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
{
    int tankBaseAmount;
    int tankTurretAmount;
    TankTurret turret;

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

    /**
     * Does what the tank does.
     */
    public void act() 
    {
        drive();
        if (tankTurretAmount < tankBaseAmount)
        {
            getWorld().addObject (new TankTurret(), getX()-2, getY()-1);
            tankTurretAmount = tankTurretAmount + 1;
        }
    }

    /**
     * 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
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);
    }
}
MainWorld code
danpost danpost

2013/12/24

#
Although, I do not know why this code is in your 'TankBase' act method:
if (tankTurretAmount < tankBaseAmount)
{
    getWorld().addObject (new TankTurret(), getX()+1, getY());
    tankTurretAmount = tankTurretAmount + 1;
}
or why the following is in your TankBase constructor:
tankBaseAmount++;
There are more replies on the next page.
1
2