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

2015/9/1

Trying To make ScoreBoard on my Game!!!!(Not Working)

1
2
3
4
Sceuby_Doo23 Sceuby_Doo23

2015/9/1

#
Recently I started creatiing a game where you have to pop Balloons to increase the scoreboards points, but I can't get it to increase the score when I click the Balloon(which is actually a button but I just named the class name balloon) Please help, this is really frustrating me!!! Can someone just tell me how to keep a record of how many balloons(buttons) the user has clicked???
Super_Hippo Super_Hippo

2015/9/1

#
You can solve it in several ways. It would be the best if you would show what you have tried. Important things: - where do you check for the clicks? - world or balloon - where do you save the score? - world or a different class, i.e. Counter - where do you display the score? - Counter or only Scoreboard If you show what you have tried so far and describe which part you can't get to work, it's easier to explain what you have to do.
Sceuby_Doo23 Sceuby_Doo23

2015/9/2

#
I save the score in a counter class and I check for the clicks in the Balloon class(Recnetly change to Bubble class). And I don't get what you mean about the last question. But here is what I've done so far. Sorry just opened the project and all my bubble class got deleted!!!!!!!! I will provide you with my Counter class and my Timer class. Counter Class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class Counter extends Actor
{
    private int score;
    public void act()
    {
        Background world = (Background) getWorld();
        Bubble Bubble = world.getBubble();
        if(Greenfoot.mouseClicked(Bubble))
        {
            addScore(1);
        }
    }
    public Counter()
    {
        score = 0;
        setImage (new GreenfootImage(200, 30));
        update();
    }
    public void addScore(int amount)
    {
        score += amount;
        update();
    }
    public void update()
    {
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(Color.BLACK);
        img.drawString("Score: " + score, 4, 20);
    }
}
Timer class:
import greenfoot.*;
import java.awt.Color;
/**
 * Write a description of class Timer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Timer extends Actor
{
    public int timer = 6000;
    public void act()
    {
        update();
        if(timer>0)
        {
            timer--;
            if(timer == 0)
            {
                Greenfoot.stop();
            }
        }
    }
    public Timer()
    {
        setImage( new GreenfootImage(200, 24));
        update();
    }
    public void update()
    {
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(Color.BLACK);
        img.drawString("Time: " + timer, 4, 20);
    }
}
Sceuby_Doo23 Sceuby_Doo23

2015/9/2

#
Thank you for your help! I can't belive I didn't save my Bubble class!!! SO MAD!!!
Sceuby_Doo23 Sceuby_Doo23

2015/9/2

#
Just give me a second, I am redoing all of my Bubble Class, should be done in about 3 hours. :)
Sceuby_Doo23 Sceuby_Doo23

2015/9/2

#
My issue with the Scoreboard is I'm trying to make it when it clicks a specific object the score goes up. But it doesn't work and I can just click anywhere and the score goes up. Almost finished Bubble class again.
Super_Hippo Super_Hippo

2015/9/2

#
The last question was asking if the player sees the current points (Counter) or only sees the score after the game is over (ScoreBoard). I see that it is the first option. In my case, Greenfoot saves everything I do automatically without even asking. Show the code in your 'Background' world. I am wondering what the 'getBubble' method is returning. What might solve your problem is to put the click detection into the Bubble class and use 'if (Greenfoot.mouseClicked(this))'.
Sceuby_Doo23 Sceuby_Doo23

2015/9/2

#
I just posted my scenario here so you can have a look
Sceuby_Doo23 Sceuby_Doo23

2015/9/2

#
Here is the code for my Background class:
import greenfoot.*;

/**
 * Write a description of class Background here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{
    private Counter theCounter;
    private Bubble Bubble;
    /**
     * Constructor for objects of class Background.
     * 
     */
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }
    public void prepare()
    {
        Timer timer = new Timer();
        addObject(timer, 120, 26);
        
        Counter counter = new Counter();
        addObject(counter, 128, 56);
        
        Bubble bubble = new Bubble();
        addObject(bubble, Greenfoot.getRandomNumber(getWidth()), -5);
        
        theCounter = new Counter();
        addObject(theCounter, 5, 5);
        
        Bubble Bubble = new Bubble();
        addObject(bubble, Greenfoot.getRandomNumber(getWidth()), -5);
    }
    public Counter getCounter()
    {
        return theCounter;
    }
    public Bubble getBubble()
    {
        return Bubble;
    }
}
Sceuby_Doo23 Sceuby_Doo23

2015/9/2

#
I am still a bit of a newie to Greenfoot so don't be surprised if this code is super bad and the mistake was a super easy one to find... :P
Sceuby_Doo23 Sceuby_Doo23

2015/9/2

#
Thank you so much for your time and help and patients with me, as I said, I'm still a noob!
Sceuby_Doo23 Sceuby_Doo23

2015/9/2

#
Super_Hippo wrote...
The last question was asking if the player sees the current points (Counter) or only sees the score after the game is over (ScoreBoard). I see that it is the first option. In my case, Greenfoot saves everything I do automatically without even asking. Show the code in your 'Background' world. I am wondering what the 'getBubble' method is returning. What might solve your problem is to put the click detection into the Bubble class and use 'if (Greenfoot.mouseClicked(this))'.
Yes, I have tried this before all my code disappearead but the problem with that is I don't really know how to access the addScore(int amount) function from the Counter class in the Bubble class. This is where my problem start, trying to increase the score from another object...
Sceuby_Doo23 Sceuby_Doo23

2015/9/2

#
I would also really appreciate it if you could supply me with some code that adds the same actor more then once to my world??? I want more then one bubble to spawn and want the X coordinate to be random... so the bubbles spawn fall down from the top of the screen so the user can pop them. :)
Super_Hippo Super_Hippo

2015/9/2

#
Hm, interesting. Anyway, since you probably want to spawn more bubbles and not just one at a time, you have to check for the clicks in the Bubble class anyway. Try this:
//Bubble class
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        ((Background) getWorld()).getCounter().addScore(1);
        getWorld().removeObject(this);
    }
    
    //the following will only work if you change line 20
    //in the Background class to 'super(600,400,1,false);'
    //then objects can be outside the visible world
    //(bubbles can come in and go out without spawning out of nowhere)
    if (getY() > getWorld().getHeight()+10) removeObject(this);
}
Does it work? Some things in your code: - Don't name a variable exactly the same as a class ('Bubble' should be 'bubble' in line 12). Variables shouldn't start with an uppercase letter. - You add two Counters into the world. - You add 'bubble' twice into the world. → It is only added once and changes its location then. (Bubble != bubble) Edit: For random spawning bubbles, you can add something like this into the world:
 private int timer = 50;

public void act()
{
    if (timer > 0)
    {
        timer--;
        return;
    }
    timer = 50;
    addObject(new Bubble(), 3+Greenfoot.getRandomNumber(getWidth()-6), -10);
}
Sceuby_Doo23 Sceuby_Doo23

2015/9/2

#
Thank you! And the your first code in line 6 is saying "not a statement"???
There are more replies on the next page.
1
2
3
4