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

yesterday

Score Counter Does Not Work

JaneSmith JaneSmith

yesterday

#
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 MyWorld extends World
{
    int pipeCounter = 0;
    int flappyCounter = 0;
    int SPACE_BETWEEN_PIPES = 0;
    int score = 0;
    int FIRST_PIPE = 240;
    int randomLoc;
   // int addVal=230;
    int counter =0;
    //int scoreV= 0;

    Score scoreObj = null;

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);

        setPaintOrder(GameOver.class, Score.class, FlappyBird.class, TopPipe.class);

        FlappyBird flappy = new FlappyBird();
        addObject(flappy, 100, 200);

        scoreObj = new Score();
        scoreObj.setScore(0);

        addObject(scoreObj,70,355);
        
    }

    public void act()
    {
        pipeCounter++;

        if(pipeCounter % 100 == 0)
        {
            createPipes();
        }
        if(pipeCounter >= FIRST_PIPE)
        {
            if(flappyCounter % 100 == 0)
            {
                score++;
                scoreObj.setScore(score);        
            }
            flappyCounter++;
        }

    }
    
    //public int addVal =230;
    
    private void createPipes()
    {
        if(pipeCounter==100)
        {
            BottomPipe bottompipe = new BottomPipe();
            randomLoc = Greenfoot.getRandomNumber(200)-100;

            addObject(bottompipe, getWidth(), getHeight() + SPACE_BETWEEN_PIPES /2 + randomLoc);

            TopPipe toppipe = new TopPipe();
            addObject(toppipe, getWidth(), 0 - SPACE_BETWEEN_PIPES /2 + randomLoc);

            pipeCounter = 0;
        }

    }
}
___________________________________________

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
//import java.awt.Color; 
 import greenfoot.Color;
 //import java.awt.Font;
 import greenfoot.Font;

/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    public Score()
    {
        GreenfootImage newImage = new GreenfootImage(100, 50);
        setImage(newImage);
    }

    public void setScore(int score)
    {
        GreenfootImage newImage =getImage();
        newImage.clear();
        
        Font f = new Font("Comic sans MS", 32);
        newImage.setFont(f);
        
        Color c = new Color(127,127,127,127);
        newImage.setColor(c);
        newImage.fill();
        newImage.setColor(Color.BLACK);
        newImage.drawString("" + score, 30, 30);
        setImage(newImage);

    }
}
The score does not increase when the flappy bird goes through the pipes. The first set of code is for the world class the second is for the actor class "Score." Any thoughts on this problem would be greatly appreciated.
danpost danpost

yesterday

#
JaneSmith wrote...
<< Code Omitted >> The score does not increase when the flappy bird goes through the pipes. The first set of code is for the world class the second is for the actor class "Score." Any thoughts on this problem would be greatly appreciated.
The Score class seems to be (or ... is) functional and the Score object is referenced properly. So, the problem must be in the logic of your act method.
JaneSmith JaneSmith

yesterday

#
Do you mean the act method in the World class or the Actor class?
danpost danpost

yesterday

#
JaneSmith wrote...
Do you mean the act method in the World class or the Actor class?
You only show one act method above. Alone, line 56 and 57 would show the Score object working.
JaneSmith JaneSmith

yesterday

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class FlappyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FlappyWorld extends World
{
    int pipeCounter = 0;
    int flappyCounter = 0;
    int PIPE_SPACING = 150;
    int score = 0;
    int FIRST_PIPE = 240;
    Score scoreObj = null;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public FlappyWorld()
    {         

        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false); 

        //Set paint order
        //setPaintOrder(Pipe.class, FlappyBird.class, GameOver.class);

        //Create a Flappy Bird Object
        FlappyBird flappy = new FlappyBird();
        addObject(flappy, 100, getHeight()/2); 

        //Create a Score Object
        scoreObj = new Score();
        scoreObj.setScore(0);

        //Add it to our world
        addObject(scoreObj, 70, 355); 

    }

    public void act()
    {
        pipeCounter++;

        if (pipeCounter %  100 == 0)
        {
            createPipes();
        }

        if (pipeCounter >= FIRST_PIPE)
        {
            if (flappyCounter % 100 == 0)
            {
                score++;
                scoreObj.setScore(score);
            }

            flappyCounter++;

        }

    }

    private void createPipes() {
        // Bottom Pipe
        BottomPipe botPipe = new BottomPipe();
        int offset = Greenfoot.getRandomNumber(getHeight()*2/3);
        addObject(botPipe, getWidth() + botPipe.getImage().getWidth()/2, getHeight() + botPipe.getImage().getHeight()/2 /*- 50*/ - offset);

        // Top Pipe
        TopPipe topPipe = new TopPipe();
        addObject(topPipe, botPipe.getX(), botPipe.getY() - topPipe.getImage().getHeight() - PIPE_SPACING);
    }

}
________________________________

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    public Score()
    {
        
        GreenfootImage newImage = new GreenfootImage (100, 50);
        setImage(newImage);
    }
    
    public void setScore(int score)
    {
        GreenfootImage newImage = getImage();
        newImage.clear();
        newImage.drawString("" + score, 30, 30);
        setImage(newImage);
    }
    
       
}
This code works just fine for this game and the act method is exactly the same.
JaneSmith JaneSmith

yesterday

#
I see the score in the original game it starts out as 0 but never increases.
danpost danpost

yesterday

#
JaneSmith wrote...
I see the score in the original game it starts out as 0 but never increases.
Are you saying you do not see the Score object at all in your game?
JaneSmith JaneSmith

yesterday

#
The second game works just fine but it is the first game where you see the Score object but it does not increase when the Flappy Bird goes between the pipes.
danpost danpost

yesterday

#
JaneSmith wrote...
The second game works just fine but it is the first game where you see the Score object but it does not increase when the Flappy Bird goes between the pipes.
Does it increase at all ... ever?
JaneSmith JaneSmith

18 hours ago

#
No the first one does not. The second one works. I just don't see what is getting in the way of seeing the code
danpost danpost

15 hours ago

#
JaneSmith wrote...
No the first one does not. The second one works. I just don't see what is getting in the way of seeing the code
Maybe you can upload the scenario with the Publish source code check box checks so it can be experimented with.
You need to login to post a reply.