This site requires JavaScript, please enable it in your browser!
Greenfoot back
oltmanac
oltmanac wrote ...

2019/2/9

Using a variable defined in a World class in an Actor Class

oltmanac oltmanac

2019/2/9

#
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));
        }
    }
}

   
danpost danpost

2019/2/9

#
Looks like you need a getter method to return the value of the score field in your SealWorld class.
oltmanac oltmanac

2019/2/9

#
I tried adding the following to my SealWorld class and Seal class.
    public  int getScore()
    {
        return score;
    }
danpost danpost

2019/2/9

#
oltmanac wrote...
I tried adding the following to my SealWorld class and Seal class. << Code Omitted >>
Needed only in SealWorld class.
oltmanac oltmanac

2019/2/9

#
Thank you so much for helping. It still didn't work. Below is my updated code. The same thing is happening when I didn't have the getter method. 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();
        getScore();
        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  int getScore()
    {
        return score;
    }
    


    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));
        }
    }
}

   
Seal 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 { private int score; /** * 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(); } } }
danpost danpost

2019/2/9

#
Remove line 11 from the Seal class -- this line:
private int score;
In your checkCollision2 method, get the value of the SealWorld object score field.
oltmanac oltmanac

2019/2/9

#
Thank you so much! Everything works now! I really appreciate you taking the time to help me.
You need to login to post a reply.