For some reason, it is saying .getCounter is undeclared I'm new and don't know how to fix this problem please help me
Code for my laser
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Laser here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Laser extends Actor
{
/**
* this code checks if the laser is touching the alien
*/
public void checkHit(){
if(isTouching(Alien.class)){
getWorld().removeObject((getOneIntersectingObject(Alien.class)));
}
}
public void addedToWorld(World world){
setRotation(getWorld().getObjects(Ship.class).get(0).checkRotation());
}
public void act()
/**
* this code runs all of the laser methods
*/
{
move(5);
if(isAtEdge()){
getWorld().removeObject(this);
World myWorld = getWorld();
Counter counter = myWorld.getCounter();
counter.addScore();
} else if(isTouching(Alien.class)){
getWorld().removeObject(getOneIntersectingObject(Alien.class));
((MyWorld) getWorld()).newAlien();
}
}
public Laser ()
/**
* this code resises the laser image
*/
{
GreenfootImage myImage = getImage();
int myNewHeight = (int)myImage.getHeight()/10;
int myNewWidth = (int)myImage.getWidth()/10;
myImage.scale(myNewWidth, myNewHeight);
}
}
Code for MyWorld
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
SimpleTimer time = new SimpleTimer();
Timer con = new Timer();
Counter counter = new Counter();
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
public MyWorld()
{
super(600, 400, 1);
addObject(counter, 550, 30);
addObject(con,40,30);
Ship ship = new Ship();
addObject(ship,309,200);
Alien alien = new Alien();
addObject(alien,562,42);
Alien alien2 = new Alien();
addObject(alien2,556,208);
Alien alien3 = new Alien();
addObject(alien3,553,370);
Alien alien4 = new Alien();
addObject(alien4,18,370);
Alien alien5 = new Alien();
addObject(alien5,24,194);
Alien alien6 = new Alien();
addObject(alien6,27,27);
removeObject(alien2);
removeObject(alien5);
time.mark();
}
public void newAlien()
/**
* This code makes the ship spawn in the middle and the 6 aliens spawn around the edge
*/{
addObject(new Alien(),Greenfoot.getRandomNumber(300),Greenfoot.getRandomNumber(300));
}
public void act()
{
con.setValue(time.millisElapsed() / 1000);
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private Counter getCounter()
{
return counter;
}
}
Code for Counter
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Counter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Counter extends Actor
{
int score = 0;
/**
* Act - do whatever the Counter wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setImage(new GreenfootImage("Score : " + score, 24, Color.WHITE, Color.BLACK));
}
public void addScore()
{
score++;
}
}
Thanks