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

2017/5/7

Add Life when Heart is removed

suge5 suge5

2017/5/7

#
I have been trying for a couple hours to add a life when the heart is pressed on and removed, not when it reaches the end of the world. Thank you very much.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Life extends Animals
{
    boolean remove = false; //creates boolean variable of remove
    public void act(){
        setLocation(getX(), getY() -5); //sets x and speed the heart crosses
        if (getY() == 0) {
            remove=true;
            FrogHitter();  //removes heart when hit
        }
    }  
    public int getCount(){
        if(remove != true){
            return 0;
        }
        return 0; //heart has no score value, only increases lives
    }
    public void playHitSound(){
        Greenfoot.playSound("pop.wav"); //plays hezrt sound when hit
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
public class FrogWorld extends World
{
    Counter counter = new Counter("Score: "); //creates string to display you cumulative score
    Counter FrogLife = new Counter("Frogs Remaining: "); //Creats counter displaying the amont of frogs left 
    GreenfootSound backgroundMusic = new GreenfootSound("Greenfoot Song.mp3"); //sets sound as background music
    public FrogWorld(){
        super(600, 600, 1); //creats screen
        createWorldObjects(); // calls the method createWorldObjects
        backgroundMusic.playLoop(); //plays the background music when game is being played
    }
    public void act() {
        scores(); //calls the method of scores
    }
    public void countHit(FrogsHit hit){
        counter.add(hit.getCount()); //counts the hits of animaks
    }
    public void lose(){
        if(FrogLife.getValue()==1){ // if you have no lives left
            addObject(new ScoreLose(counter.getValue()), getWidth()/2, getHeight()/2); //displays a scoreboard from the ScoreLose class
            Greenfoot.playSound("Lose.wav"); //plays a clip that says "you lose"
            backgroundMusic.stop(); //ends music when the game ends
            Greenfoot.stop(); // and stops the greenfoot game
        }
        else{ //if you have more than a life left
            FrogLife.subtract(1); //it takes away one life from
        }
    }
    public void win(){
        if(FrogLife.getValue()>=1 && FrogLife.getValue() < 10){ // if you have more than 1 life and less than 10
            FrogLife.add(1); //you are given an extra life when heart is hit
        }
    }
    private void createWorldObjects(){
        addObject(new FrogHunter(), 300,300); //calls frog hunter and starts baseball bat in middle of scree 
        addObject(counter, 90, 560); //shows the counter of score at places it on screen
        addObject(FrogLife, 170, 540); // shows "frogs left: " on screen and places in bottom corner
        FrogLife.add(5); //adds 5 lives to begin the game
    
    public void scores(){
        int score = counter.getValue(); // creates variable score which is the value of the counter
        int num = Greenfoot.getRandomNumber(100); // sets random number between 1-100     
        int num1 = Greenfoot.getRandomNumber(100); // sets random number between 1-100  
        int num2 = Greenfoot.getRandomNumber(200); // sets random number between 1-200
        int num3 = Greenfoot.getRandomNumber(750); // sets random number between 1-750
        if (score > 0 && score <= 250 ){ //whenever score is below or at 250 at above 1
            if(num > 1 && num < 6){    
                addObject(new SwimmingFrog(), Greenfoot.getRandomNumber(599), 600); //adds a frog whenever the random number is between 1-6 
            }
        }
         if (score > 250 && score <= 500){ //whenever score is between 250-500
            if(num > 1 && num < 7){   
                addObject(new SwimmingFrog(), Greenfoot.getRandomNumber(599), 600); //adds a frog whenever the random number is between 1-7 
            }      
            if(num2 == 1 || num2 == 2){   
                addObject(new Fish(), Greenfoot.getRandomNumber(599), 600); //adds a fish when the random number is equal to 1 or 2
            }
        }
        if (score > 500 && score <= 2000){ //whenever score is between 500 and 2000
            if(num > 1 && num < 8){   
                addObject(new SwimmingFrog(), Greenfoot.getRandomNumber(599), 600); //adds a frog whenever the random number is between 1-8 
            
            if(num2 == 1 || num2 == 2){   
                addObject(new Fish(), Greenfoot.getRandomNumber(599), 600); //adds a fish when the random number is equal to 1 or 2  
            }
            if(num2 == 3 || num2 == 4){   
                addObject(new PolarBear(), Greenfoot.getRandomNumber(599), 600); //adds a polar bear when the random number is equal to 3 or 4 
            }
        }
        if(score >= 1 && score < 3000){ //between 0 and 3000 or anytime game is being played
            if(num3 == 375){   
                addObject(new Fly(), 600, Greenfoot.getRandomNumber(599)); // adds a fly whenever random number 3 equals 1
            }
        }
        if (score > 2000 ){ //whenever score is above 2000
            if(num > 1 && num < 9){   
                addObject(new SwimmingFrog(), Greenfoot.getRandomNumber(599), 600); //adds a frog whenever the random number is between 1-9  
            }
            if(num2 == 1 || num2 == 2){   
                addObject(new Fish(), Greenfoot.getRandomNumber(599), 600);   //adds a fish when the random number is equal to 1 or 2
            }
            if(num2 == 3|| num2 == 4){   
                addObject(new PolarBear(), Greenfoot.getRandomNumber(599), 600);  //adds a polar bear when the random number is equal to 3 or 4
            }
        }
        if (score == 499 || score == 999 || score == 1499 ||score == 1999 || score == 2499){
            addObject(new Life(), Greenfoot.getRandomNumber(599), 600); //adds a new heart (for new life) every 500 points
        }
        if((FrogLife.getValue()>=1  && score >= 3000)){ //if you have a life and a score above 3000
            addObject(new ScoreWin(counter.getValue()), getWidth()/2, getHeight()/2); //displays score board from the score win class
            Greenfoot.playSound("Win.wav"); //plays a winnner sound
            backgroundMusic.stop(); //ends music when the game ends
            Greenfoot.stop(); //stops the game
        }
    }
}
danpost danpost

2017/5/8

#
You could consider adding the life in the 'playHitSound' method, which is executed only when the actor is "hit". You could also consider changing the name of the method to just 'hit' or 'wasHit'. If you do change the name, make sure you change it in its superclasses and other classes the extend them.
suge5 suge5

2017/5/8

#
It's adding a life whenever it reaches end of world also, I dont want that. How do I fix this? public void playHitSound(){ Greenfoot.playSound("pop.wav"); //plays heart sound when hit ((FrogWorld) getWorld()).win(); }
danpost danpost

2017/5/8

#
In the Life class, you are only calling 'FrogHitter' when the actor reaches the end of the world. Switch lines 8 and 9 and then see what happens.
suge5 suge5

2017/5/8

#
The heart doesn't move now
danpost danpost

2017/5/8

#
suge5 wrote...
The heart doesn't move now
Please show your current Life class codes.
suge5 suge5

2017/5/8

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Life extends Animals
{
    boolean remove = false; //creates boolean variable of remove
    public void act(){
        setLocation(getX(), getY() -5); //sets x and speed the heart crosses
        if (getY() == 0) {
            remove=true;
           FrogHitter();  //removes heart when hit
        }
    }  
    public int getCount(){
        if(remove != true){
            return 0;
        }
        return 0; //heart has no score value, only increases lives
    }
    public void playHitSound(){
        Greenfoot.playSound("pop.wav"); //plays heart sound when hit
        ((FrogWorld) getWorld()).win();
    }
}
danpost danpost

2017/5/8

#
suge5 wrote...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Life extends Animals
{
    boolean remove = false; //creates boolean variable of remove
    public void act(){
        setLocation(getX(), getY() -5); //sets x and speed the heart crosses
        if (getY() == 0) {
            remove=true;
           FrogHitter();  //removes heart when hit
        }
    }  
    public int getCount(){
        if(remove != true){
            return 0;
        }
        return 0; //heart has no score value, only increases lives
    }
    public void playHitSound(){
        Greenfoot.playSound("pop.wav"); //plays heart sound when hit
        ((FrogWorld) getWorld()).win();
    }
}
Change the act method to this:
1
2
3
4
5
6
7
8
9
public void act()
{
    setLocation(getX(), getY()-5);
    if (getY() == 0)
    {
        remove = true;
    }
    FrogHitter();
}
suge5 suge5

2017/5/8

#
That makes it so the heart does not appear on the screen. When a new heart is added, it just gives you a life instead of making the user press on the heart.
danpost danpost

2017/5/8

#
suge5 wrote...
That makes it so the heart does not appear on the screen. When a new heart is added, it just gives you a life instead of making the user press on the heart.
Maybe I am getting confused on your methods. Please post the FrotHit class (I think that is what I need to see).
suge5 suge5

2017/5/8

#
1
2
3
4
5
6
7
8
9
10
11
abstract public class FrogsHit extends Actor
{
   public abstract void act(); //abstract to be used by all subclasses
   public void FrogHitter(){ 
       //Greenfoot.playSound("Ribbit.wav");  //sound to play
        playHitSound();
       ((FrogWorld) getWorld()).countHit(this); //plays when hit and records hit
        getWorld().removeObject(this);} //removes hit object
   public abstract int getCount(); //abstract of getCount for all subclasses
   public abstract void playHitSound(); //abstarct so each animals has respective sound
}
danpost danpost

2017/5/8

#
Okay, I was thinking the check for click was in the FrogHitter method -- apparently not; and I did not (and still do not) see anything detecting a mouse click in the Life class. Wait, I remember, it was in a FrogHunter class. Just remove the 'FrogHitter();' line from the Life class.
You need to login to post a reply.