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

2017/6/6

Timer, Counter, Scoreboard. Need help in my game

1
2
3
FeVo FeVo

2017/6/6

#
Hi, First of all sorry for my bad English, I'm from Germany. Now to my questions: How can I make a Counter/Scoreboard in my game, which counts the coins collected of my 2 player classes. One for the player/class and another one for the other player/class. If u need the code of the game, I can copy it right here. Also, I want to make a Timer in my game, which counts down from 30 Seconds to 0 and which is visible in the game. After it counted down to 0 there should be a message in the Game, not the one with system.outprint. In the message should be written: "Red boxer, blue boxer (depends on who collected more coins during the 30 seconds) won, because he collected more coins". How would be a code for this and how can I make, that there is standing, that the red boxer won if he collected more coins or that the blue boxer won if he collected more coins. I hope u know what I mean xD thanks in front (?) FeVo
Super_Hippo Super_Hippo

2017/6/6

#
I think the easiest way to implement a Counter is by importing the Counter class with Edit → Import Class (Bearbeiten → Importortiere -sic- Klasse...). You add two instances of this class to the world (one for red, one for blue). Save a reference to the objects in your world so you can access them later.
private Counter counter[] = {new Counter("Roter Boxer: "), new Counter("Blauer Boxer: ")};

//when adding the objects
addObject(counter[0], 70, 30);
addObject(counter[1], 120, 30);


//method to return the counter
public Counter getCounter(int i) {return counter[i];}
Then, when your red boxer scores:
((MyWorld) getWorld()).getCounter(0).add(1); //change MyWorld to the name of the world
For the blue one, you use a 1 instead of the 0. The timer can also be another object of the Counter class.
private Counter timer = new Counter("Zeit: ");
private int time = 30*55; //about 30 seconds

//adding it
addObject(timer, 170, 30);

//decrease it every 55th act cycle
public void act()
{
    if (--time % 55 == 0)
    {
        timer.add(-1);
        if (time==0)
        {
            if (counter[0].getValue() > counter[1].getValue()) //red won
            else if (counter[0].getValue() < counter[1].getValue()) //blue won
            else //draw -- Unentschieden
        }
    }
}
To show a message, you can either use the showText method or you create a new class which you can give an image showing a text (similar to what is done in the Counter class's update method).
danpost danpost

2017/6/6

#
My Value Display Tutorial scenario has all the information you need on this (in fact, you could have all the functionality in one class -- timer, both player scores and final scoreboard (could be called a Stats, for statistics, class). The only thing missing then, would be the players' access to to scoring, which is not too difficult. Get the class(es) created first, though EDIT: or import and use the Counter class as Hippo suggests.
FeVo FeVo

2017/6/6

#
danpost wrote...
My Value Display Tutorial scenario has all the information you need on this (in fact, you could have all the functionality in one class -- timer, both player scores and final scoreboard (could be called a Stats, for statistics, class). The only thing missing then, would be the players' access to to scoring, which is not too difficult. Get the class(es) created first, though EDIT: or import and use the Counter class as Hippo suggests.
Thanks, but the link doesnt work
FeVo FeVo

2017/6/6

#
Super_Hippo wrote...
I think the easiest way to implement a Counter is by importing the Counter class with Edit → Import Class (Bearbeiten → Importortiere -sic- Klasse...). You add two instances of this class to the world (one for red, one for blue). Save a reference to the objects in your world so you can access them later.
private Counter counter[] = {new Counter("Roter Boxer: "), new Counter("Blauer Boxer: ")};

//when adding the objects
addObject(counter[0], 70, 30);
addObject(counter[1], 120, 30);


//method to return the counter
public Counter getCounter(int i) {return counter[i];}
Then, when your red boxer scores:
((MyWorld) getWorld()).getCounter(0).add(1); //change MyWorld to the name of the world
For the blue one, you use a 1 instead of the 0. The timer can also be another object of the Counter class.
private Counter timer = new Counter("Zeit: ");
private int time = 30*55; //about 30 seconds

//adding it
addObject(timer, 170, 30);

//decrease it every 55th act cycle
public void act()
{
    if (--time % 55 == 0)
    {
        timer.add(-1);
        if (time==0)
        {
            if (counter[0].getValue() > counter[1].getValue()) //red won
            else if (counter[0].getValue() < counter[1].getValue()) //blue won
            else //draw -- Unentschieden
        }
    }
}
To show a message, you can either use the showText method or you create a new class which you can give an image showing a text (similar to what is done in the Counter class's update method).
Where do i need to copy the text?
Super_Hippo Super_Hippo

2017/6/6

#
Everything except the one-liner in the middle to the world, the line in the middle to the boxer when he should get a point. That's the correct link: http://www.greenfoot.org/scenarios/14834
FeVo FeVo

2017/6/6

#
Super_Hippo wrote...
Everything except the one-liner in the middle to the world, the line in the middle to the boxer when he should get a point. That's the correct link: http://www.greenfoot.org/scenarios/14834
sorry i still dont unterstand in which class.. haha
Super_Hippo Super_Hippo

2017/6/6

#
You should have a world subclass. Maybe it is called MyWorld (default) or you renamed it to something else (e. g. Welt).
FeVo FeVo

2017/6/6

#
Super_Hippo wrote...
You should have a world subclass. Maybe it is called MyWorld (default) or you renamed it to something else (e. g. Welt).
ok, can i copy u the code of my world subclass and u put the text in, so i dont do mistakes? thanks in front
Super_Hippo Super_Hippo

2017/6/6

#
You could, but you could also tell us what you don't understand and maybe we can guide you so you figure it out yourself.
FeVo FeVo

2017/6/6

#
Super_Hippo wrote...
You could, but you could also tell us what you don't understand and maybe we can guide you so you figure it out yourself.
i onl,y need it for school and dont wanna understand xD code comes soon
FeVo FeVo

2017/6/6

#
here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Diese Klasse erstellt eine Welt für Kara. Sie enthaelt Einstellungen ueber
 * die Hoehe und Breite der Welt und initialisiert alle Actors.
 *
 * @author Marco Jakob
 * @version 04.04.2011
 */
public class Wiese extends World
{  
    private static final int WORLD_WIDTH = 20;  // Anzahl der Zellen horizontal
    private static final int WORLD_HEIGHT = 20;  // Anzahl der Zellen vertikal

    private static final int CELL_SIZE = 28; // Groesse einer einzelnen Zelle

    /**
     * Erstellt eine Welt fuer Kara.
     */

    private int time = 0;
 
    public Wiese()
    {
        // Erzeugt eine neue Welt
        super(WORLD_WIDTH, WORLD_HEIGHT, CELL_SIZE);

        setPaintOrder(Kara.class, Tree.class, Mushroom.class, Leaf.class);
        Greenfoot.setSpeed(15);

        // Erzeuge die Anfangs-Objekte fuer die Welt.
        prepare();
    }
    public void makeNewCoin()
    {
        if(this.getObjects(Leaf.class).size() <= 0)
        {
            this.newCoin();
        }
    }

    /**
     * Bereite die Welt fuer den Programmstart vor. Das heisst: Erzeuge die Anfangs-
     * Objekte und fuege sie der Welt hinzu.
     *
     * Tipp: Die Objekte zuerst mit der Maus in der Welt platzieren. Anschliessend
     * Rechtsklick auf die Welt, dann 'Die Welt speichern' auswaehlen. So werden alle
     * Objekte automatisch in diese Methode geschrieben.
     */
    private void prepare()
    {
        Tree tree = new Tree();
        addObject(tree,12,6);
        Tree tree2 = new Tree();
        addObject(tree2,13,7);
        tree2.setLocation(12,7);
        tree.setLocation(11,6);
        tree2.setLocation(11,7);
        Tree tree3 = new Tree();
        addObject(tree3,11,8);
        Tree tree4 = new Tree();
        addObject(tree4,3,6);
        Tree tree5 = new Tree();
        addObject(tree5,3,8);
        Tree tree6 = new Tree();
        addObject(tree6,3,7);
        MyKara mykara = new MyKara();
        addObject(mykara,4,7);
        removeObject(tree3);
        removeObject(tree2);
        removeObject(tree);
        removeObject(tree5);
        removeObject(tree6);
        removeObject(tree4);
        Player1 player1 = new Player1();
        addObject(player1,4,6);
        mykara.setLocation(0,14);
        mykara.setLocation(2,12);
        player1.setLocation(12,2);
        removeObject(player1);
        Player1 player12 = new Player1();
        addObject(player12,13,13);
        removeObject(player12);
        Player1 player13 = new Player1();
        addObject(player13,13,12);
        player13.setLocation(3,3);
        mykara.setLocation(12,12);
        mykara.setLocation(15,16);
        Tree tree7 = new Tree();
        addObject(tree7,18,18);
        Tree tree8 = new Tree();
        addObject(tree8,17,18);
        Tree tree9 = new Tree();
        addObject(tree9,17,18);
        Tree tree10 = new Tree();
        addObject(tree10,17,18);
        Tree tree11 = new Tree();
        addObject(tree11,18,18);
        Tree tree12 = new Tree();
        addObject(tree12,17,18);
        Tree tree13 = new Tree();
        addObject(tree13,16,18);
        Tree tree14 = new Tree();
        addObject(tree14,16,18);
        Tree tree15 = new Tree();
        addObject(tree15,16,18);
        Tree tree16 = new Tree();
        addObject(tree16,15,18);
        Tree tree17 = new Tree();
        addObject(tree17,14,18);
        Tree tree18 = new Tree();
        addObject(tree18,13,18);
        Tree tree19 = new Tree();
        addObject(tree19,13,18);
        Tree tree20 = new Tree();
        addObject(tree20,12,18);
        Tree tree21 = new Tree();
        addObject(tree21,11,18);
        Tree tree22 = new Tree();
        addObject(tree22,11,18);
        Tree tree23 = new Tree();
        addObject(tree23,10,18);
        Tree tree24 = new Tree();
        addObject(tree24,10,18);
        Tree tree25 = new Tree();
        addObject(tree25,9,18);
        Tree tree26 = new Tree();
        addObject(tree26,8,18);
        Tree tree27 = new Tree();
        addObject(tree27,8,18);
        Tree tree28 = new Tree();
        addObject(tree28,7,18);
        Tree tree29 = new Tree();
        addObject(tree29,7,18);
        Tree tree30 = new Tree();
        addObject(tree30,5,18);
        Tree tree31 = new Tree();
        addObject(tree31,5,18);
        Tree tree32 = new Tree();
        addObject(tree32,4,18);
        Tree tree33 = new Tree();
        addObject(tree33,4,18);
        Tree tree34 = new Tree();
        addObject(tree34,4,18);
        Tree tree35 = new Tree();
        addObject(tree35,3,18);
        Tree tree36 = new Tree();
        addObject(tree36,2,18);
        Tree tree37 = new Tree();
        addObject(tree37,2,18);
        Tree tree38 = new Tree();
        addObject(tree38,1,18);
        Tree tree39 = new Tree();
        addObject(tree39,1,18);
        Tree tree40 = new Tree();
        addObject(tree40,1,17);
        Tree tree41 = new Tree();
        addObject(tree41,1,16);
        Tree tree42 = new Tree();
        addObject(tree42,1,16);
        Tree tree43 = new Tree();
        addObject(tree43,1,15);
        Tree tree44 = new Tree();
        addObject(tree44,1,14);
        Tree tree45 = new Tree();
        addObject(tree45,1,14);
        Tree tree46 = new Tree();
        addObject(tree46,1,14);
        Tree tree47 = new Tree();
        addObject(tree47,1,15);
        Tree tree48 = new Tree();
        addObject(tree48,1,16);
        Tree tree49 = new Tree();
        addObject(tree49,1,17);
        Tree tree50 = new Tree();
        addObject(tree50,1,16);
        Tree tree51 = new Tree();
        addObject(tree51,1,14);
        Tree tree52 = new Tree();
        addObject(tree52,1,13);
        Tree tree53 = new Tree();
        addObject(tree53,1,13);
        Tree tree54 = new Tree();
        addObject(tree54,1,13);
        Tree tree55 = new Tree();
        addObject(tree55,1,11);
        Tree tree56 = new Tree();
        addObject(tree56,1,11);
        Tree tree57 = new Tree();
        addObject(tree57,1,10);
        Tree tree58 = new Tree();
        addObject(tree58,1,9);
        Tree tree59 = new Tree();
        addObject(tree59,1,9);
        Tree tree60 = new Tree();
        addObject(tree60,1,8);
        Tree tree61 = new Tree();
        addObject(tree61,1,7);
        Tree tree62 = new Tree();
        addObject(tree62,1,7);
        Tree tree63 = new Tree();
        addObject(tree63,1,6);
        Tree tree64 = new Tree();
        addObject(tree64,1,6);
        Tree tree65 = new Tree();
        addObject(tree65,1,5);
        Tree tree66 = new Tree();
        addObject(tree66,1,4);
        Tree tree67 = new Tree();
        addObject(tree67,1,3);
        Tree tree68 = new Tree();
        addObject(tree68,1,3);
        Tree tree69 = new Tree();
        addObject(tree69,1,1);
        Tree tree70 = new Tree();
        addObject(tree70,1,1);
        Tree tree71 = new Tree();
        addObject(tree71,1,1);
        Tree tree72 = new Tree();
        addObject(tree72,1,1);
        Tree tree73 = new Tree();
        addObject(tree73,2,1);
        Tree tree74 = new Tree();
        addObject(tree74,2,1);
        Tree tree75 = new Tree();
        addObject(tree75,3,1);
        Tree tree76 = new Tree();
        addObject(tree76,3,1);
        Tree tree77 = new Tree();
        addObject(tree77,4,1);
        Tree tree78 = new Tree();
        addObject(tree78,4,1);
        Tree tree79 = new Tree();
        addObject(tree79,5,1);
        Tree tree80 = new Tree();
        addObject(tree80,5,1);
        Tree tree81 = new Tree();
        addObject(tree81,6,1);
        Tree tree82 = new Tree();
        addObject(tree82,6,1);
        Tree tree83 = new Tree();
        addObject(tree83,7,1);
        Tree tree84 = new Tree();
        addObject(tree84,8,1);
        Tree tree85 = new Tree();
        addObject(tree85,9,1);
        Tree tree86 = new Tree();
        addObject(tree86,9,1);
        Tree tree87 = new Tree();
        addObject(tree87,10,1);
        Tree tree88 = new Tree();
        addObject(tree88,11,1);
        Tree tree89 = new Tree();
        addObject(tree89,11,1);
        Tree tree90 = new Tree();
        addObject(tree90,12,1);
        Tree tree91 = new Tree();
        addObject(tree91,11,1);
        Tree tree92 = new Tree();
        addObject(tree92,11,1);
        Tree tree93 = new Tree();
        addObject(tree93,12,1);
        Tree tree94 = new Tree();
        addObject(tree94,13,1);
        Tree tree95 = new Tree();
        addObject(tree95,13,1);
        Tree tree96 = new Tree();
        addObject(tree96,13,1);
        Tree tree97 = new Tree();
        addObject(tree97,14,1);
        Tree tree98 = new Tree();
        addObject(tree98,15,1);
        Tree tree99 = new Tree();
        addObject(tree99,16,1);
        Tree tree100 = new Tree();
        addObject(tree100,16,1);
        Tree tree101 = new Tree();
        addObject(tree101,16,1);
        Tree tree102 = new Tree();
        addObject(tree102,16,1);
        Tree tree103 = new Tree();
        addObject(tree103,17,1);
        Tree tree104 = new Tree();
        addObject(tree104,17,1);
        Tree tree105 = new Tree();
        addObject(tree105,18,1);
        Tree tree106 = new Tree();
        addObject(tree106,18,1);
        Tree tree107 = new Tree();
        addObject(tree107,18,1);
        Tree tree108 = new Tree();
        addObject(tree108,18,1);
        Tree tree109 = new Tree();
        addObject(tree109,18,2);
        Tree tree110 = new Tree();
        addObject(tree110,18,2);
        Tree tree111 = new Tree();
        addObject(tree111,18,3);
        Tree tree112 = new Tree();
        addObject(tree112,18,2);
        Tree tree113 = new Tree();
        addObject(tree113,18,2);
        Tree tree114 = new Tree();
        addObject(tree114,18,2);
        Tree tree115 = new Tree();
        addObject(tree115,18,3);
        Tree tree116 = new Tree();
        addObject(tree116,18,3);
        Tree tree117 = new Tree();
        addObject(tree117,18,4);
        Tree tree118 = new Tree();
        addObject(tree118,18,4);
        Tree tree119 = new Tree();
        addObject(tree119,18,5);
        Tree tree120 = new Tree();
        addObject(tree120,18,5);
        Tree tree121 = new Tree();
        addObject(tree121,18,5);
        Tree tree122 = new Tree();
        addObject(tree122,18,6);
        Tree tree123 = new Tree();
        addObject(tree123,18,7);
        Tree tree124 = new Tree();
        addObject(tree124,18,8);
        Tree tree125 = new Tree();
        addObject(tree125,18,8);
        Tree tree126 = new Tree();
        addObject(tree126,18,9);
        Tree tree127 = new Tree();
        addObject(tree127,18,11);
        Tree tree128 = new Tree();
        addObject(tree128,18,11);
        Tree tree129 = new Tree();
        addObject(tree129,18,12);
        Tree tree130 = new Tree();
        addObject(tree130,18,12);
        Tree tree131 = new Tree();
        addObject(tree131,18,12);
        Tree tree132 = new Tree();
        addObject(tree132,18,11);
        Tree tree133 = new Tree();
        addObject(tree133,18,10);
        Tree tree134 = new Tree();
        addObject(tree134,18,9);
        Tree tree135 = new Tree();
        addObject(tree135,18,6);
        Tree tree136 = new Tree();
        addObject(tree136,18,6);
        Tree tree137 = new Tree();
        addObject(tree137,18,7);
        Tree tree138 = new Tree();
        addObject(tree138,18,8);
        Tree tree139 = new Tree();
        addObject(tree139,18,9);
        Tree tree140 = new Tree();
        addObject(tree140,18,10);
        Tree tree141 = new Tree();
        addObject(tree141,18,11);
        Tree tree142 = new Tree();
        addObject(tree142,18,12);
        Tree tree143 = new Tree();
        addObject(tree143,18,13);
        Tree tree144 = new Tree();
        addObject(tree144,18,13);
        Tree tree145 = new Tree();
        addObject(tree145,18,14);
        Tree tree146 = new Tree();
        addObject(tree146,18,14);
        Tree tree147 = new Tree();
        addObject(tree147,18,15);
        Tree tree148 = new Tree();
        addObject(tree148,18,16);
        Tree tree149 = new Tree();
        addObject(tree149,18,17);
        Tree tree150 = new Tree();
        addObject(tree150,18,18);
        Tree tree151 = new Tree();
        addObject(tree151,18,18);
        Tree tree152 = new Tree();
        addObject(tree152,18,18);
        Tree tree153 = new Tree();
        addObject(tree153,17,18);
        Tree tree154 = new Tree();
        addObject(tree154,17,18);
        Tree tree155 = new Tree();
        addObject(tree155,16,18);
        Tree tree156 = new Tree();
        addObject(tree156,16,18);
        Tree tree157 = new Tree();
        addObject(tree157,1,12);
        makeNewCoin();
        removeObject(tree49);
        removeObject(tree40);
        removeObject(tree50);
        removeObject(tree51);
        removeObject(tree48);
        removeObject(tree42);
        removeObject(tree37);
        removeObject(tree35);
        removeObject(tree34);
        removeObject(tree29);
        tree97.setLocation(12,6);

        removeObject(tree59);
        removeObject(tree57);
        removeObject(tree56);
        removeObject(tree60);
        removeObject(tree58);
        removeObject(tree55);
        removeObject(tree157);
        removeObject(tree46);
        removeObject(tree47);
        removeObject(tree39);
        removeObject(tree38);
        removeObject(tree68);
        removeObject(tree67);
        removeObject(tree66);
        removeObject(tree64);
        removeObject(tree63);
        removeObject(tree65);
        removeObject(tree74);
        removeObject(tree73);
        removeObject(tree72);
        removeObject(tree71);
        removeObject(tree70);
        removeObject(tree69);
        removeObject(tree76);
        removeObject(tree78);
        removeObject(tree77);
        removeObject(tree75);
        removeObject(tree80);
        removeObject(tree79);
        removeObject(tree82);
        removeObject(tree81);
        removeObject(tree83);
        removeObject(tree84);
        removeObject(tree86);
        removeObject(tree85);
        removeObject(tree87);
        removeObject(tree92);
        removeObject(tree91);
        removeObject(tree89);
        removeObject(tree93);
        removeObject(tree90);
        removeObject(tree88);
        removeObject(tree96);
        removeObject(tree95);
        removeObject(tree94);
        removeObject(tree43);
        removeObject(tree41);
        removeObject(tree54);
        removeObject(tree102);
        removeObject(tree98);
        removeObject(tree101);
        removeObject(tree100);
        removeObject(tree104);
        removeObject(tree103);
        removeObject(tree99);
        removeObject(tree116);
        removeObject(tree118);
        removeObject(tree108);
        removeObject(tree107);
        removeObject(tree106);
        removeObject(tree114);
        removeObject(tree105);
        removeObject(tree113);
        removeObject(tree112);
        removeObject(tree117);
        removeObject(tree121);
        removeObject(tree137);
        removeObject(tree123);
        removeObject(tree136);
        removeObject(tree135);
        removeObject(tree122);
        removeObject(tree120);
        removeObject(tree119);
        removeObject(tree115);
        removeObject(tree111);
        removeObject(tree110);
        removeObject(tree109);
        removeObject(tree138);
        removeObject(tree125);
        removeObject(tree139);
        removeObject(tree124);
        removeObject(tree45);
        removeObject(tree53);
        removeObject(tree52);
        removeObject(tree44);
        removeObject(tree36);
        removeObject(tree33);
        removeObject(tree32);
        removeObject(tree31);
        removeObject(tree30);
        removeObject(tree28);
        removeObject(tree27);
        removeObject(tree26);
        removeObject(tree25);
        removeObject(tree24);
        removeObject(tree23);
        removeObject(tree22);
        removeObject(tree21);
        removeObject(tree20);
        removeObject(tree19);
        removeObject(tree18);
        removeObject(tree17);
        removeObject(tree16);
        removeObject(tree156);
        removeObject(tree155);
        removeObject(tree154);
        removeObject(tree15);
        removeObject(tree14);
        removeObject(tree13);
        removeObject(tree153);
        removeObject(tree12);
        removeObject(tree10);
        removeObject(tree9);
        removeObject(tree8);
        removeObject(tree152);
        removeObject(tree151);
        removeObject(tree150);
        removeObject(tree149);
        removeObject(tree11);
        removeObject(tree7);
        removeObject(tree148);
        removeObject(tree147);
        removeObject(tree146);
        removeObject(tree145);
        removeObject(tree144);
        removeObject(tree143);
        removeObject(tree142);
        removeObject(tree131);
        removeObject(tree130);
        removeObject(tree129);
        removeObject(tree141);
        removeObject(tree132);
        removeObject(tree128);
        removeObject(tree127);
        removeObject(tree140);
        removeObject(tree133);
        removeObject(tree134);
        removeObject(tree126);
        removeObject(tree62);
        removeObject(tree61);
    }
    
    private void newCoin()
    {
        int rand = Greenfoot.getRandomNumber(18);
        int rand2 = Greenfoot.getRandomNumber(18);
        
        while(this.getObjectsAt(rand, rand2, Tree.class).size() > 0 ||
        this.getObjectsAt(rand, rand2, Kara.class).size() > 0)
        {
            rand = Greenfoot.getRandomNumber(18);
            rand2 = Greenfoot.getRandomNumber(18);
        }
        
        Leaf coin = new Leaf();
        addObject(coin, Math.floorMod(rand+2, 18), Math.floorMod(rand2+2, 18));
    

    } 
    
    }
FeVo FeVo

2017/6/6

#
Super_Hippo wrote...
You could, but you could also tell us what you don't understand and maybe we can guide you so you figure it out yourself.
or u do it for me and after that u explain me how it works?
Super_Hippo Super_Hippo

2017/6/6

#
I also started with Greenfoot in school like four and a half years ago, but I continued programming with it and it became a hobby. Give it a chance, it's a lot of fun if you understand what you are doing. If a line starts with a for example "private", it can't be in a method. It is either a method itself (e. g. the theCounter method) or a variable (e. g. counter, timer, time...). The variables belong to the beginning of the class, just where in line 21, you already have a time variable. So instead of this line, insert these:
private Counter counter[] = {new Counter("Roter Boxer: "), new Counter("Blauer Boxer: ")};
private Counter timer = new Counter("Zeit: ");
private int time = 30*55; //about 30 seconds
In the prepare method, you add objects. (I really dislike the Save the World feature since it usually results in a mess like that for new programmers...) Anyways, I told you to add the three objects, so you can place this code in the prepare method:
//the coordinates should be altered so they are added to the correct position
addObject(counter[0], 70, 30);
addObject(counter[1], 120, 30);
addObject(timer, 170, 30);
The getter-method for the counters is, as the name says, a method. So it could for example be in line 563:
public Counter getCounter(int i) {return counter[i];}
Then, you only need to add the act method in the same way. A new method must not be placed in another method, but anywhere in the class. I personally place the act method right after the constructor, so after line 33:
public void act()
{
    if (--time % 55 == 0)
    {
        timer.add(-1);
        if (time==0)
        {
            if (counter[0].getValue() > counter[1].getValue()) //red won
            else if (counter[0].getValue() < counter[1].getValue()) //blue won
            else //draw -- Unentschieden
        }
    }
}
FeVo FeVo

2017/6/6

#
Super_Hippo wrote...
I also started with Greenfoot in school like four and a half years ago, but I continued programming with it and it became a hobby. Give it a chance, it's a lot of fun if you understand what you are doing. If a line starts with a for example "private", it can't be in a method. It is either a method itself (e. g. the theCounter method) or a variable (e. g. counter, timer, time...). The variables belong to the beginning of the class, just where in line 21, you already have a time variable. So instead of this line, insert these:
private Counter counter[] = {new Counter("Roter Boxer: "), new Counter("Blauer Boxer: ")};
private Counter timer = new Counter("Zeit: ");
private int time = 30*55; //about 30 seconds
In the prepare method, you add objects. (I really dislike the Save the World feature since it usually results in a mess like that for new programmers...) Anyways, I told you to add the three objects, so you can place this code in the prepare method:
//the coordinates should be altered so they are added to the correct position
addObject(counter[0], 70, 30);
addObject(counter[1], 120, 30);
addObject(timer, 170, 30);
The getter-method for the counters is, as the name says, a method. So it could for example be in line 563:
public Counter getCounter(int i) {return counter[i];}
Then, you only need to add the act method in the same way. A new method must not be placed in another method, but anywhere in the class. I personally place the act method right after the constructor, so after line 33:
public void act()
{
    if (--time % 55 == 0)
    {
        timer.add(-1);
        if (time==0)
        {
            if (counter[0].getValue() > counter[1].getValue()) //red won
            else if (counter[0].getValue() < counter[1].getValue()) //blue won
            else //draw -- Unentschieden
        }
    }
}
Thanks so much, I think i understand. I can try it tomorrow, bcos i dont have time anymore today
There are more replies on the next page.
1
2
3