I'm trying to call a variable from one class in another and it keeps saying its not public in Greenfoot.Actor I've declared it as public already
public class Shot extends Actor
{
private Crab myCrab;
/**
* Constructor for a Shot. You must specify the ship the shot comes from.
*/
public Shot(Crab myCrab)
{
this.setRotation(rotation);
this.myCrab = myCrab;
}
/**
* Act - do whatever the Shot wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
int ypos = getY();
move(6);
if (ypos > 0) {
ypos = ypos - 5;
setLocation(getX(), ypos);
Actor rock = getOneIntersectingObject(Worm.class);
if (rock != null) {
// We've hit an asteroid!
hitAnAsteroid();
getWorld().removeObject(rock);
getWorld().removeObject(this);
}
}
else {
// I reached the top of the screen
getWorld().removeObject(this);
}
}
/**
* This method gets called (from the act method, above) when the shot hits an
* asteroid. It needs to do only one thing: increase the score counter.
* (Everything else, such as removing the asteroid which was hit, is dealt
* with in the act method).
*/
private void hitAnAsteroid()
{
CrabWorld spaceWorld = (CrabWorld) getWorld();
}
}public class Crab extends Actor
{
/**
* Act - do whatever the Crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int shotTimer = 0;
public int rotation;
public void act()
{
rotation = this.getRotation();
moveAndTurn();
eat();
shoot();
}
public void moveAndTurn()
{
move (6);
if (Greenfoot.isKeyDown("left"))
{
this.setRotation(180);
}
if (Greenfoot.isKeyDown("right"))
{
this.setRotation(0);
}
if (Greenfoot.isKeyDown("down"))
{
this.setRotation(90);
}
if (Greenfoot.isKeyDown("up"))
{
this.setRotation(270);
}
if ((Greenfoot.isKeyDown("up"))&&(Greenfoot.isKeyDown("right")))
{
this.setRotation(315);
}
if ((Greenfoot.isKeyDown("right"))&&(Greenfoot.isKeyDown("down")))
{
this.setRotation(45);
}
if ((Greenfoot.isKeyDown("down"))&&(Greenfoot.isKeyDown("left")))
{
this.setRotation(135);
}
if ((Greenfoot.isKeyDown("left"))&&(Greenfoot.isKeyDown("up")))
{
this.setRotation(225);
}
}
public void eat()
{
Actor worm;
worm = getOneObjectAtOffset(0,0,Worm.class);
if (worm!=null)
{
World world;
world = getWorld();
world.removeObject(worm);
Greenfoot.playSound("explode.wav");
}
}
public void shoot()
{
if (shotTimer > 0) {
shotTimer = shotTimer - 1;
}
else if (Greenfoot.isKeyDown("space")) {
getWorld().addObject(new Shot(this), getX(), getY());
setRotation(getRotation());
shotTimer = 10; // delay next shot
}
}
}