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

2014/8/29

How to make counter show score in GameOver world?

1
2
JeppDev JeppDev

2014/8/29

#
I have a problem with making my final score in my game show in the second world called GameOver, my code for the counter is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter1 extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the Counter1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
    }    
    
    public void addScore()
    {
        score++;
    }
}
And the code that adds score is
   public void destroyEnemies()  
   {   
       Actor enemy = getOneIntersectingObject(Alien.class);  
       if(enemy != null) {  
            World myWorld = getWorld();
            getWorld().removeObject(enemy);  
            testWorld testworld = (testWorld)myWorld;
            Counter1 counter = testworld.getCounter();
            counter.addScore();
            getWorld().removeObject(this);  
            Greenfoot.playSound("explosion.wav");
       }  
   }  
And my code for creating the new map is
    public void Killed()  
    {  
       Actor enemy = getOneIntersectingObject(Alien.class);  
       if(enemy != null) {  
            getWorld().removeObject(enemy);  
            getWorld().removeObject(this);  
            Greenfoot.playSound("explosion.wav");
       }  
    }  
And
        if(getWorld().getObjects(Rocket.class).size() == 0)
        {
            music.stop();
            Greenfoot.setWorld(new GameOver());
        }
Please if anyone could help, i have tried many different things but i cant get it to work
Super_Hippo Super_Hippo

2014/8/29

#
So you just want to display the score in the 'GameOver' world? First, instead of 'int score = 0;', make this field either public or private. If you make it private, add a getter method like this: (If you make it public, you do not need the getter-method and you can insert 'score' where 'getScore()' is at the end when you change your world)
private int score = 0; //declare 'score' as private

public int getScore() //to be able to access this field from outside this class
{
    return score;
}
In your world, you save this counter object. So you create it like this:
public Counter1 counter1; //save the counter in a field to get access from outside

//when creating
counter1 = new Counter1();
addObject(counter1, /*x*/,/*y*/);
Then, when you set the new world (in case that this last piece of code is not inside your counter class. If it is, you can just pass the score), you pass the score like this:
//Instead of W, place the name of your world
W w = (W) getWorld();
Greenfoot.setWorld(new GameOver(w.counter1.getScore()));
Then you only need to change the constructor of the 'GameOver' world:
public GameOver(int score)
{
    super(.....);
    //do whatever you wanted to do with the score
}
As an alternative, if there is only one Counter1 object in your game, you can declare the score as static and public.
public static score = 0;
Then you can just do the following to change to the new world and pass the score.
Greenfoot.setWorld(new GameOver(Counter1.score));
JeppDev JeppDev

2014/8/29

#
Thank you for the quick answer, but i still can't get it to work, would it help if i send you my whole project so you can see everything that i have done?
danpost danpost

2014/8/30

#
All you need to do is add an argument to the GameOver constructor.
public GameOver(int finalScore)
and adjust your game over check to this:
if(getWorld().getObjects(Rocket.class).isEmpty())
{
    music.stop();
    int score = ((testWorld)getWorld()).getCounter().score;
    Greenfoot.setWorld(new GameOver(score));
}
JeppDev JeppDev

2014/8/30

#
How do i make the counter in the new world call the score?
JeppDev JeppDev

2014/8/30

#
I dont know how to use the int in the GameOver
danpost danpost

2014/8/30

#
JeppDev wrote...
I dont know how to use the int in the GameOver
You stated you wanted the final score to show in the GameOver world. That will require an image with the 'int' value shown on it. The image could be set to or drawn on the GameOver world background or the image of an actor that is added to the world. How ever you do it, it should be coded in the GameOver constructor (where the 'int' value is already set to the local variable 'finalScore').
JeppDev JeppDev

2014/8/30

#
I am very new to tihis, like i started two days ago so i don't really know how i do that
Super_Hippo Super_Hippo

2014/8/30

#
What exactly doesn't you understand? Did you understand how to pass the score to the new world?
danpost danpost

2014/8/30

#
JeppDev wrote...
I am vwey new to tihis, like i started two days ago so i don't really know how i do that
Take it one step at a time. Try something out. If you run into a block, post the code you tried and explain what you want it to do. You will need to create a GreenfootImage object or make a reference to one that already exists (the world background is one that will already be in existence as well as the images of any actors you already had created). Refer to the GreenfootImage class API to see what constructors and methods are available.
JeppDev JeppDev

2014/8/30

#
I don't understand any of the stuff that you are saying and all i want to do is make a text actor that shows the number that i send to the
public GameOver(int finalScore)
danpost danpost

2014/8/30

#
Like I said, take it one step at a time: * create an actor * create an image with text * set the image of the actor to that image * add the actor into the world These are basic things you should know how to do. If you are not sure how to do one or more of these steps, at least attempt some code before asking for help. Post the code you attempted if you cannot get it to work. Only ask for help on one specific thing at a time. That is the only way you are going to be able to learn from what is given you.
JeppDev JeppDev

2014/8/30

#
I know how to add an actor and give it text both static and dynamic i just dont know how to make it use the score from testWorld as the text in GameOver
danpost danpost

2014/8/30

#
You can convert the number to a String in several ways. Here are a few:
String text = ""+finalScore;
// or
String text = new String(""+finalScore);
// or
String text = String.valueof(finalScore);
Super_Hippo Super_Hippo

2014/8/30

#
You create the new world with 'public GameOver(int finalScore)'. If you want an actor to display this score, you can pass this score to the actor the same way as you passed it to the new world. (Change 'Test' to the name of the actor which you want to display the score.
public GameOver(int finalScore)
{
    super(/*...*/);
    
    addObject(new Test(finalScore));
}
public Test(int finalScore)
{
    //now, if you draw the score on the image or something similar, you can use the 'finalScore'
}
There are more replies on the next page.
1
2