I am trying to display the points on a specific actor. My problem is that the score continually displays at every location then stays there. I understand it does that because I use getX(), and getY() for location of the showText(). I'm trying to figure out how to display the score directly to the actor rather than its location points.
Actor code
World code just in case
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Goldfish here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Goldfish extends Actor
{
public int points;
/**
* Initialise this Goldfish.
*/
public Goldfish()
{
GreenfootImage img = getImage();
getImage().mirrorHorizontally();
img.scale(img.getWidth()/10, img.getHeight()/10);
setImage(img);
points = 0;
//showScore();
//checkLocation();
//checkCollision();
}
/**
* Act - do whatever the Goldfish wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
checkKeyPress();
checkCollision();
//showScore();
//checkLocation();
}
/**
* Check whether a keyboard key has been pressed and react if it has.
*/
private void checkKeyPress()
{
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-4);
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+4);
}
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+4, getY());
}
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX()-4, getY());
}
}
/*Check if Goldfish has collided with something and react*/
public void checkCollision()
{
if ( isTouching( Pufferfish.class))
{
/*Greenfoot.playSound("slurp.wav");
Bloodstream bloodstream = (Bloodstream)getWorld();
bloodstream.addScore(20);*/
removeTouching( Pufferfish.class);
addScore(1);
}
if ( isTouching( MEDFish.class))
{
/*Greenfoot.playSound("slurp.wav");
Bloodstream bloodstream = (Bloodstream)getWorld();
bloodstream.addScore(20);*/
removeTouching( MEDFish.class);
addScore(10);
}
}
//add score
public void addScore(int score)
{
points = points + score;
if ( points == 50 )
{
Greenfoot.stop();
Greenfoot.playSound("fanfare.wav");
}
}
//check locations to display score
/*public void checkLocation()
{
getX();
getY();
showScore();
}*/
//show score
public void showScore()
{
getWorld().showText("Points: " + points, getX(), getY());
}
}
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 Ocean extends World
{
public int time;
/**
* Constructor for objects of class Ocean.
*
*/
public Ocean()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(780, 360, 1);
setBackground("Underwater.jpeg");
setPaintOrder(Coral.class, Seaweed.class, Pufferfish.class, Shark.class,
Goldfish.class, MEDFish.class, Sand.class);
time = 5000;
prepare();
showTime();
}
/*act*/
public void act()
{
//Create Pufferfish 5% of time
if (Greenfoot.getRandomNumber(100) < 5)
{
addObject(new Pufferfish(), 779, Greenfoot.getRandomNumber(360));
}
//Create Sand 1% of time
if (Greenfoot.getRandomNumber(100) < 1)
{
addObject(new Sand(), 779, 307);
}
//Create shark 1% of time
if (Greenfoot.getRandomNumber(100) < 1)
{
addObject(new Shark(), 779, Greenfoot.getRandomNumber(360));
}
//Create shark 1% of time
if (Greenfoot.getRandomNumber(100) < 1)
{
addObject(new MEDFish(), 779, Greenfoot.getRandomNumber(360));
}
countTime();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
Goldfish goldfish = new Goldfish();
addObject(goldfish,87,203);
Sand sand = new Sand();
addObject(sand,306,307);
Sand sand2 = new Sand();
addObject(sand2,656,305);
sand.setLocation(299,307);
Coral coral = new Coral();
addObject(coral,644,232);
Seaweed seaweed = new Seaweed();
addObject(seaweed,38,118);
Seaweed seaweed2 = new Seaweed();
addObject(seaweed2,109,162);
coral.setLocation(648,289);
seaweed.setLocation(19,216);
seaweed.setLocation(16,227);
seaweed2.setLocation(33,193);
Seaweed seaweed3 = new Seaweed();
addObject(seaweed3,67,125);
seaweed3.setLocation(72,227);
}
/*show time*/
private void showTime()
{
showText("Time: " + time, 700, 25);
}
/*Counts down time to end game*/
private void countTime()
{
time = time - 1;
showTime();
if ( time == 0)
{
showEndMessage();
Greenfoot.playSound("game-over.wav");
Greenfoot.stop();
}
}
private void showEndMessage()
{
showText("Time is up---You Lose!!!", 360, 140);
//showText("Your final score: " + points, 360, 180);
}
}
