I have looked at many tutorials and can't figure this out. I created a score variable in the SealWorld class. I am able to display the score, have score added when the seal hits certain objects, and the game will end if I get below a certain number of points. I would like to be able to have the seal touch a castle actor and the game is over and play a game over sound if I have less than 20 points but I win and play a winning sound if I touch the castle with 20 or more points. I can't figure out how to have the score variable which is in SealWorld be read by my Seal actor class. Code is below.
Seal Actor Class
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Seal here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Seal extends Actor
{
/**
* Act - do whatever the Seal wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeyPress();
checkCollision();
checkCollision2();
}
public void checkKeyPress()
{
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+4, getY());
}
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX()-4,getY());
}
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(),getY()-4);
}
if (Greenfoot.isKeyDown("Down"))
{
setLocation(getX(),getY()+4);
}
}
public void checkCollision2()
{
if (isTouching(Castle.class ))
{
if (score >=20)
{
Greenfoot.playSound("fanfare.wav");
SealWorld sealWorld = (SealWorld)getWorld();
sealWorld.showEndMessage();
Greenfoot.stop();
}
else{
Greenfoot.playSound("game-over.wav");
Greenfoot.stop();
}
}
}
public void checkCollision()
{
if (isTouching(Lemon.class))
{
removeTouching(Lemon.class);
Greenfoot.playSound("au.wav");
SealWorld sealWorld=(SealWorld)getWorld();
sealWorld.addScore(-10);
}
if (isTouching(Muffin.class))
{
removeTouching(Muffin.class);
Greenfoot.playSound("slurp.wav");
SealWorld sealWorld=(SealWorld)getWorld();
sealWorld.addScore(10);
}
if (isTouching(Elephant.class))
{
Greenfoot.playSound("game-over.wav");
removeTouching(Seal.class);
Greenfoot.stop();
}
}
}
for SealWorld
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 SealWorld extends World
{
private int score;
/**
* Constructor for objects of class MyWorld.
*
*/
public SealWorld()
{
super(600, 400, 1);
score=0;
showScore();
Seal mySeal=new Seal();
addObject(mySeal, 566,217);
Castle myCastle=new Castle();
addObject(myCastle,28,221);
Elephant myElephant=new Elephant();
addObject(myElephant,288,106);
}
public void addScore(int points)
{
if (score >= -100)
{
score = score + points;
showScore();
}
else
{
Greenfoot.playSound("game-over.wav");
Greenfoot.stop();
}
}
public void showScore()
{
showText("Score:" +score,80,25);
}
public void showEndMessage()
{
showText("You Win! Your final score is:" +score,300,300);
}
public void act()
{
if (Greenfoot.getRandomNumber (30)<1)
{
addObject(new Muffin(),30, Greenfoot.getRandomNumber(360));
}
if (Greenfoot.getRandomNumber (60)<1)
{
addObject(new Lemon(),30, Greenfoot.getRandomNumber(360));
}
}
}
