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

#
Yes my packman is called Player with a capital letter at the start
SPower SPower

2013/8/14

#
I looked at your code again, and I see what was wrong: you just added the code without putting it in a method. If you copy my code it should work.
ich99 ich99

2013/8/14

#
How i put the text in a method? Sorry for the stupid questtion but i'm new at greenfoot.
SPower SPower

2013/8/14

#
Just copy this:
    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, 20, 20);    
        }  
          
        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");  
            }  
        }  
    }  
You see that the code was put the method called 'act'
ich99 ich99

2013/8/14

#
Okay i copied the text. When i now place a coin in the level it disappears, i also placed the counter in the level, but when i collect a coin nothin happens to it! I also tried to place the counter at the end level, but when i finished all levels and collected some coins the counter is 0. And when i'm at the end screen a new window opens which is named: Greenfoot: Terminal Window - java game In the window is this text: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:604) at java.util.ArrayList.get(ArrayList.java:382) at endlvl.act(endlvl.java:27) at greenfoot.core.Simulation.actWorld(Simulation.java:574) at greenfoot.core.Simulation.runOneLoop(Simulation.java:509) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:604) at java.util.ArrayList.get(ArrayList.java:382) at endlvl.act(endlvl.java:29) at greenfoot.core.Simulation.actWorld(Simulation.java:574) at greenfoot.core.Simulation.runOneLoop(Simulation.java:509) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
SPower SPower

2013/8/14

#
That's probably because there is no player in the world. I'll explain: getObjects is a method which returns a List object, which is very simmilar to a real list: an organised collection of objects. However, it can also be empty. This is the case when there is no player in the world: you are asking for objects of the class Player, but there are none! If you want to know more about the method getObjects, look at the documentation: http://www.greenfoot.org/files/javadoc/
ich99 ich99

2013/8/14

#
but i placed the counter also in the level where i walk with the player and there isnt displayed the number of coins too!
SPower SPower

2013/8/14

#
Well, I looked at this:
public endlvl()    
    {    
        super(10, 10, 50);       
        coinsCollectedText = new Text("You have collected 0/50 coins", 20);      
        addObject(coinsCollectedText, 20, 20);      
    }
which shows that your world is 10*10 points wide and high. You placed it at x 20 and y 20. That's out of your world: so it's not visible. Just try puting it at x 5 y 2 or something like that.
ich99 ich99

2013/8/14

#
Okay i changed it in x 5 and y 2 now i collected 3 coins, but there is displayed 0/50 coins
SPower SPower

2013/8/14

#
You just have to expiriment where to put it. Because only the right is displayed, it's too far to the left, so you have to higher the x value.
ich99 ich99

2013/8/14

#
Hm i now placed the player in the end world and first the coins are displayed correct , but when i move the points were more and more and it dont stop
SPower SPower

2013/8/14

#
Sorry, I don't understand what you mean. Do you mean that the points just keep adding?
ich99 ich99

2013/8/14

#
Yeha i mean that the points keep adding, and it dont stop.
danpost danpost

2013/8/14

#
When this happens, first right-click on the world and select Inspect from the menu options. Note the name of the object at the top of the inspection window. Whatever world that is, post the code for it here. BTW, if it is the 'endlvl' world then post the Text class code instead; because there is nothing in the endlvl class that would cause this and the Text class is the only other class that is used here.
SPower SPower

2013/8/14

#
The text class was made by me, and that can't be the problem. Ich99, did you actually copy this code from Gevater in your Player class:
public void getCoin() {  
    Actor coin = getOneObjectAtOffset(0, 0, Coin.class);  
    if (coin != null) {  
        getWorld().removeObject(coin);  
    }  
    coinsCollected++;
} 
if not, do so now. Then, in your act method in your Player class, add this:
getCoin();
it should work.
There are more replies on the next page.
1
2
3