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

2013/8/13

Point System

1
2
3
ich99 ich99

2013/8/14

#
Okay here is the code from the World:
    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)      
          
/**   
 * Write a description of class endlvl here.   
 *    
 * @author (your name)    
 * @version (a version number or a date)   
 */      
public class endlvl extends World      
{      
    private Text coinsCollectedText;      
    private int coinsCollected = 0;    

    /**   
     * Constructor for objects of class endlvl.   
     *    
     */    
    public endlvl()    
    {    
        super(10, 10, 50);       
        coinsCollectedText = new Text("You have collected 0/50 coins", 20);      
        addObject(coinsCollectedText, 5, 2);      

        prepare();
    }    

    public void act()    
    {    
        Player packman = (Player) getObjects(Player.class).get(0);    
        if (coinsCollected < packman.getCoinsCollected()) {    
            coinsCollected = packman.getCoinsCollected();    
            coinsCollectedText.setText("You have collected "+coinsCollected+"/50 coins");    
        }    
    }    

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Counter counter = new Counter();
        addObject(counter, 5, 5);
        removeObject(counter);
        Player player2 = new Player();
        addObject(player2, 5, 5);
        
}  
}
ich99 ich99

2013/8/14

#
Yes i copied gevaters Code in my player .class and now also " getCoin(); " Here is the code:
private int coinsCollected = 0;
    
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    
    
    
        public void getCoin() {  
        Actor coin = getOneObjectAtOffset(0, 0, Coin.class);  
        if (coin != null) {  
            getWorld().removeObject(coin);  
        }  
        coinsCollected++;
    }  
    public void act()
    {
        getCoin();
danpost danpost

2013/8/14

#
I am aware that you (SPower) made the Text class; however, Ich99 could have tried modifying it. I think there might be a little confusion here. Ich99 wants two things: text that displays a running count of coins collected in one world and text that displays a final message showing how many coins were collected during the game in another. I believe that the 'endlvl' world would be for the final message not for the running score. I do not believe he has shown the code for the main world that the player is in; as, there is no code adding a player into the endlvl world. Therefore, the code you gave above for the endlvl world does not make sense.
ich99 ich99

2013/8/14

#
And what should i do now?
danpost danpost

2013/8/14

#
Now, I am really confused! Ich99, please name all of your worlds, the order they are used, and what each is used for.
ich99 ich99

2013/8/14

#
First i have the world: "Level1" When "Level1" is finished, My Player is in "Level2", when "Level2" is finished, "endlvl" comes where is no Player, i made the world for displaying the end score(How many coins you collected).
danpost danpost

2013/8/14

#
You just need to pass along either the number of coins collected or the player object (which has the number of coins collected in one of its fields) from world to world. When creating your endlvl world object, you need to tell it what the final score (number of coins collected) was. There are various ways of doing this. One is by adding an argument to the constructor; another is by calling a method in the endlvl world after creating it:
// constructor w/argument
public endlvl(int coinCount)
{
    super(10, 10, 50);
    coinsCollectedText = new Text("You have collected "+coinCoint+"/50 coins", 20);
    addObject(coinsCollectedText, 5, 5);
}
// create from other world with
Greenfoot.setWorld(new endlvl(coinsCollected));

// or, using a method call, with constructor
public endlvl()
{
    super(10, 10, 50);
    coinsCollectedText = new Text("", 20);
    addObject(coinsCollectedText, 5, 5);
}
// with this method
public void setCoinsCollected(int coinCount)
{
    coinsCollectedText.setText("You have collected "+coinCount+"/50 coins");
}
// created from other world with
endlvl elvl = new endlvl();
elvl.setCoinsCollected(coinsCollected);
Greenfoot.setWorld(elvl);
Using the first way, the whole endlvl class should look like this:
import greenfoot.*;

public class endlvl extends World
{
    public endlvl(int coinCount)
    {
        super(1, 1, 500);
        Text text = new Text("You have collected "+coinCount +"/50 coins", 20);
        addObject(text, 0, 0);
        Greenfoot.stop();
    }
}
Hope this helps.
ich99 ich99

2013/8/14

#
I'm really confused now. can you please write the whole endlvl code down, because i dont understand now what i have to write where.
danpost danpost

2013/8/14

#
The last code snippet above IS the entire endlvl code.
ich99 ich99

2013/8/14

#
Oh ok sorry.
You need to login to post a reply.
1
2
3