I am making a game where you drive around and shoot tanks for my java class at school. I am trying to use the turnToward(x, y) to make the enemy tanks follow you around, however I can't figure out how to get the location of the user tank in the enemy tank class.
This is the code for the user tank:
This is the code for the enemy tank:
This is my world code:
public class Tank extends Actor
{
/**
* Act - do whatever the Tank wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
moveAndTurn();
fire();
}
public void moveAndTurn(){
if(Greenfoot.isKeyDown("up")){
move(3);
}
if(Greenfoot.isKeyDown("down")){
move(-3);
}
if(Greenfoot.isKeyDown("right")){
turn(3);
}
if(Greenfoot.isKeyDown("left")){
turn(-3);
}
}
public void fire(){
if(Greenfoot.isKeyDown("f") || Greenfoot.isKeyDown("space")){
Greenfoot.playSound("pew.wav");
}
}
}public class EnemyTank extends Actor
{
/**
* Act - do whatever the EnemyTank wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int framecount;
public void act()
{
framecount++;
if(framecount == 5) framecount = 0;
if(framecount == 0){
moveAndTurn();
fire();
}
}
public void moveAndTurn(){
move(1);
}
public void fire(){
}
}public class MyWorld extends World
{
Tank tank;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1000, 600, 1);
prepare();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
tank = new Tank();
addObject(tank,321,241);
tank.setLocation(34,33);
EnemyTank enemytank = new EnemyTank();
addObject(enemytank,482,334);
enemytank.setLocation(935,540);
}
}
