How will I be able to pass the direction my tank is pointing in to mu turret? these two are separate objects.
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);
}
}
}
}
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()
{
}
}
// ********** 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;
}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);
turret.setLocation(getX(), getY());
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);
}
}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);
}
}
}
}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()
{
}
}addObject(tank, 200, 200);
this.turret = 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)
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)
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);
}
}
}
}
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);
}
}if (tankTurretAmount < tankBaseAmount)
{
getWorld().addObject (new TankTurret(), getX()+1, getY());
tankTurretAmount = tankTurretAmount + 1;
}tankBaseAmount++;