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

2016/5/23

end game issue

smcgee smcgee

2016/5/23

#
import greenfoot.*;

/**
 * Apples are falling from trees with different speed. 
 * When the basket catch them, they will disappear. 
 * But if apples hit the ground, they will explode.
 * 
 * Kim & Iris
 * 5/19/2016
 */
public class Apple extends Actor
{
    private int speed;
    private GreenfootImage image2;
    private int appleFell;
    private int score;
    private int time;
    /**
     * Constructor: Initialise the speed to a somewhat random value.
     */
    public Apple()
    {
        speed = Greenfoot.getRandomNumber(2) + 1;
        image2 = new GreenfootImage("explode.png");
        appleFell = 0;

    }

    /**
     * Fall from the tree
     */
    public void act() 
    {
        setLocation(getX(), getY()+speed);
        switchImage();
        stopGame(6);
    }

    /**
     * When apples hit the ground, they will explode.
     */
    public void switchImage()
    {
        if (getY() == 570)
        {
            setImage(image2);
            Greenfoot.delay(5);
            getWorld().removeObject(this);
            appleFell = appleFell +1;
        }
    }

    public void stopGame(int points)
    {
        if (appleFell == 6)
        {
            appleFell++;
            appleFell = appleFell +1;
            Greenfoot.stop();
        }
    }

}
       
smcgee wrote...
I am unable to make the game stop after 6 apples have fallen.
SPower SPower

2016/5/23

#
The only place where you increase appleFell is in the method stopGame, and there it only happens if appleFell is equal to 6. Because appleFell starts off as being 0, you never get to this count, so it never goes up, so it never gets to six, so it never tells Greenfoot to stop.
smcgee smcgee

2016/5/23

#
public void stopGame(int points)
    {
        if (appleFell == 6)
        {
            appleFell++;
            appleFell = appleFell +1;
            Greenfoot.stop();
        }
    }
smcgee wrote...
I thought this method would increment the apple falling to the ground. The idea is that each time an apple touches the ground it would add an apple so when we reached 6 the game would stop.
smcgee smcgee

2016/5/23

#
How should I write this code?
smcgee smcgee

2016/5/23

#
import greenfoot.*;

/**
 * Apples are falling from trees with different speed. 
 * When the basket catch them, they will disappear. 
 * But if apples hit the ground, they will explode.
 * 
 * Kim & Iris
 * 5/19/2016
 */
public class Apple extends Actor
{
    private int speed;
    private GreenfootImage image2;
    private int appleFell;
     /**
     * Constructor: Initialise the speed to a somewhat random value.
     */
    public Apple()
    {
        speed = Greenfoot.getRandomNumber(2) + 1;
        image2 = new GreenfootImage("explode.png");
        appleFell = 0;
    }
    
     /**
     * Fall from the tree
     */
    public void act() 
    {
        setLocation(getX(), getY()+speed);
        switchImage();
        stopGame();
    }
    
    /**
     * When apples hit the ground, they will explode.
     */
    public void switchImage()
    {
        if (getY() == 570)
        {
            setImage(image2);
            Greenfoot.delay(5);
            getWorld().removeObject(this);
            appleFell = appleFell + 1;
        }
    }
    
    public void stopGame()
    {
        if (appleFell >= 6)
        {
            // appleFell++;
            Greenfoot.stop();
        }
    }
}
        
       
smcgee wrote...
!he code is running and the number does increase now it just won't stop! Any suggestions on how I might rewrite the code. Making progress...
danpost danpost

2016/5/24

#
From what I can tell, it will only increase once when it being removed from the world after making it to ground level. What is the purpose of the 'appleFell' field? How is the number 6 to be used for the falling apple?
smcgee smcgee

2016/5/24

#
Thank you - Right now everything works just fine except the stopGame method. The apples fall, the basket captures some apples, the apples that reach the ground switchImage to splat, and the score increases. But the stopGame method does not seem to work. The goal is that once the total score of apples reaching the ground not the basket - reaches 6 - we want the game to stop. This is the one feature that does not work.
smcgee smcgee

2016/5/24

#
I solved it! Yea. All I was missing was an if statement in the World Class act method that stops the game when it totals 6 apples fallen to the ground: public void act() { if (score == 6) { Greenfoot.stop(); } }
SPower SPower

2016/5/24

#
That I understood from your original comment, but I don't think you understand what I meant. Look again at your method stopGame:
public void stopGame(int points)
{
    if (appleFell == 6)
    {
        appleFell++;
        Greenfoot.stop();
    }
}
Here, you check if appleFell is equal to six. If this is the case (only then), then you increate appleFell by one, and then you let Greenfoot stop the game. But this means you only increase appleFell if it is already 6! You haven't shown any code that is supposed to increase it in any other place, so only here in this if-statement could it be increased. But appleFell never gets to six. Why? Because it starts out as 0, and then nowhere gets increased (again, you only put code to increase appleFell inside the if-statement, but that will never get executed). You need to move appleFell++ somewhere else, namely to the place where you actually catch apples. Also, I don't think you should be storing this appleFell instance variable in Apple, nor should you create the stopGame method in Apple. It's better to implement them in Basket (or whatever name you have for that class): that is the object that actually does the catching of the apples. An apple doesn't know how many apples have been caught in the past (after all, it's only one lonely apple!), but the basket does. Hence, you should move that code to Basket.
SPower SPower

2016/5/24

#
smcgee wrote...
I solved it! Yea. All I was missing was an if statement in the World Class act method that stops the game when it totals 6 apples fallen to the ground:
That's the easier way to solve it, but I hope you understand why it didn't do anything in your previous solution.
danpost danpost

2016/5/24

#
As you currently have it, the 'appleFell' field will only exist in each Apple object for as long as that object exists. Once an apple is removed from the world, it and the field 'appleFell' field it contained is lost. And to clarify, any change in the value of that field is not reflected in the field of any other apples that may exist. In other words, using an instance field in the apple class will not suffice for what you want that field to do. You can use a class field in the Apple class or you can use either a class field or an instance field in your World subclass. To make a class field out of an instance field, just add the 'static' modifier to the line declaring the field:
private static int appleFell;
You will need to remove line 23 where you set its value to zero or else every time you spawn an apple the field will reset back to zero. You will, instead, need to set its value to zero when your World object is created as resetting the project will not reset any static fields.
smcgee smcgee

2016/5/24

#
Thank you for your time and excellent explanations - they help a great deal.
danpost danpost

2016/5/24

#
@SPower, the field was to count the number of apples that missed the basket; so, putting the field in the class of the basket does not make any sense. I realize that the name of the field 'score' is misleading -- that catching an apple would normally be when point or points are scored; but, the following is what was previously stated:
smcgee wrote...
The goal is that once the total score of apples reaching the ground not the basket - reaches 6 - we want the game to stop.
Also, the 'appleFell' field is being increased at line 49, when the ground is encountered.
SPower SPower

2016/5/24

#
You're right, I misread. My mistake.
You need to login to post a reply.