So I have a timer class which holds multiple timers, and I would like to access those timers through the world-class. I have created methods that would return the timers in the timer class, I tried to access them in the world-class (no errors) in order to get my game going. However the timers are " not working " in the world-class but the timers have no problem decrementing and so forth. The problem I'm guessing it that I am not accessing the method correctly within the timer class, but I'll let you guys be the judge of that.
Here is my world-class code:
and here is my timer class code:
So am I referencing the timer class incorrectly? I'm not sure I fully understand accessing methods or variables from other classes still, I've already viewed the tutorial that people have been directed to multiple times but for some reason I still have trouble.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Galaxy here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Galaxy extends World
{
private GreenfootImage title = new GreenfootImage("Space Rush.png");
private GreenfootImage bg = getBackground();
private Score theScore;
private Timer theTimer;
private Asteroid theAsteroid;
private int asteroidCounter, speedCounter, healthCounter = 0;
public Galaxy()
{
super(1000, 900, 1);
prepare();
theTimer = new Timer();
theAsteroid = new Asteroid();
}
private void prepare()
{
Picture logo = new Picture(title,2,2);
addObject(logo, getWidth()/2, getHeight()/10);
addObject(new Timer(), getWidth()/2 - getWidth()/3, getHeight()/15);
Spacecraft spacecraft = new Spacecraft();
addObject(spacecraft,getWidth()/5,getHeight()/2);
theScore = new Score();
addObject(theScore, getWidth()/2 + getWidth()/3, getHeight()/15);
HealthBar healthBar = new HealthBar(300, 20, spacecraft);
addObject(healthBar, getWidth()/2, getHeight() - getHeight()/22);
bg.setColor(Color.WHITE);
bg.drawRect(0, 115, getWidth(), 15);
bg.drawRect(0, 800, getWidth(), 15);
bg.setColor(new Color(83, 64, 34));
bg.fillRect(0, 116, getWidth(), 14);
bg.fillRect(0, 801, getWidth(), 14);
}
public void scoreOverTime()
{
if(theTimer.getTimeLeft() > 0 && theTimer.getInitialCount() <= 0)
{
theScore.addScore(1);
}
}
public Score getScore()
{
return theScore;
}
/*
* Adds asteroids into the world, controlling the randomization
* and delay between asteroids spawned
*/
public void addingAsteroid()
{
asteroidCounter++;
if(Greenfoot.getRandomNumber(30) < 1 && asteroidCounter > 10 )
{
addObject(new Asteroid(), getWidth(), getRandomNumber(160, 760));
asteroidCounter = 0;
}
}
/*
* Adds speed boosts into the world, controlling the randomization
* and delay between speed boosts spawned
*/
public void addingSpeedPowerup()
{
speedCounter++;
if(Greenfoot.getRandomNumber(500) < 1 && speedCounter > 350)
{
addObject(new Speed(), getWidth(), getRandomNumber(160, 760));
speedCounter = 0;
}
}
public void addingHealthPowerup()
{
healthCounter++;
if(Greenfoot.getRandomNumber(300) < 1 && healthCounter > 500)
{
addObject(new Health(), getWidth(), getRandomNumber(160, 760));
healthCounter = 0;
}
}
public int getRandomNumber(int start,int end)
{
int normal = Greenfoot.getRandomNumber(end-start+1);
return normal+start;
}
public void act()
{
if(theTimer.getInitialCount() <= 0)
{
if(theTimer.getTimeLeft() > 0)
{
addingAsteroid();
addingSpeedPowerup();
addingHealthPowerup();
if(theTimer.getDelayLvl2() <= 0)
{
NextGalaxy Lvl2 = new NextGalaxy();
Greenfoot.setWorld(Lvl2);
}
}
}
scoreOverTime();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Timer here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Timer extends UI
{
public int initialCount = 660;
public int timeLeft = 3660;
public int delayLvl2 = 400;
public int getInitialCount()
{
return initialCount/60;
// Converts into real-like time
}
public int getTimeLeft()
{
return timeLeft/60;
}
public int getDelayLvl2()
{
return delayLvl2/60;
}
public void displayTimers()
{
initialCount--;
setImage(new GreenfootImage("GAME STARTS IN: " + getInitialCount(), 25, Color.WHITE, new Color(0,0,0,0)));
if(initialCount <= 0)
{
timeLeft--;
if (getTimeLeft() == 60)
{
setImage(new GreenfootImage("TIME REMAINING: 1:00", 25, Color.WHITE, new Color(0,0,0,0)));
}
else if(getTimeLeft() < 60)
{
displayTimeLeft();
}
if(getTimeLeft() <= 0)
{
nextLvlMsg();
if(getTimeLeft() <= -5)
{
delayLvl2--;
nextLvlTime();
}
}
}
}
public void displayTimeLeft()
{
setImage(new GreenfootImage("TIME REMAINING: " + getTimeLeft(), 25, Color.WHITE, new Color(0,0,0,0)));
}
public void nextLvlTime()
{
setImage(new GreenfootImage("LEVEL 2 BEGINS IN: " + getDelayLvl2(), 25, Color.WHITE, new Color(0,0,0,0)));
}
public void nextLvlMsg()
{
setImage(new GreenfootImage("Level Complete!", 25, Color.WHITE, new Color(0,0,0,0)));
}
public void act()
{
displayTimers();
}
}



