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

2018/5/10

How do I make my score go up by one?

applecider124 applecider124

2018/5/10

#
Ive been stuck here for a bit. I bet this is just an easy fix but I cant figure it out. Thanks!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shark2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shark2 extends Animal
{   
    private GreenfootImage image1;
    private GreenfootImage image2;
    /**
     * Act - do whatever the Shark wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Shark2()
    {
        image1 = new GreenfootImage("Shark1.png");
        image2 = new GreenfootImage("Shark2.png");
    }
    public void act() 
    {
        change();
        keyPress();
        win();
        diee();
    }    
    public void change()
    {
        if (Greenfoot.isKeyDown("b"))
        {
           setImage(image2);
        }
        else
        {
            setImage(image1);
        }
    }
    public void keyPress()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            move(5);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            move(-5);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }
    }
    public void win()
    {
     if(isTouching(Checkpoint.class))
     {
      Greenfoot.playSound("fanfare.wav");
      Greenfoot.stop();
     }
    }
     public void diee()
    {
        if(isTouching(Block.class))
        {
            Greenfoot.stop(); 
           //were I want the score to go up
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class Counter extends Actor
{
    public static int score = 0;
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score,18, Color.GREEN,Color.BLACK));
    }
}
//counter 
danpost danpost

2018/5/10

#
How about this for line 72:
Counter.score++;
Only thing is, if you stop the program (line 71), the Counter object image will not update.
applecider124 applecider124

2018/5/11

#
danpost wrote...
How about this for line 72:
Counter.score++;
Only thing is, if you stop the program (line 71), the Counter object image will not update.
Thank you!
You need to login to post a reply.