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

2015/7/14

Counter in BreakOut

Newcomer123 Newcomer123

2015/7/14

#
Hi there, currently I'm working on a Counter for my BreakOut project. The counter should be shown on screen after compiling but it doesn't. And I'm not sure if it really is scoring up. Here is the relevant code: public class Counter extends Actor { public Counter(String text) { GreenfootImage img = new GreenfootImage(100,30); img.drawString(text, 10, 20); setImage(img); } public void setText (String text) { GreenfootImage img = getImage(); img.clear(); img.drawString(text, 10, 20); } } public class Ball extends Actor { private int dx; // Objektvariable dx für x private int dy; // Objektvariable dy für y public int count; private Counter BallCounter; public Ball(Counter counter) { dx = 20 - Greenfoot.getRandomNumber(31); // Zufallszahl wird von 20 subtrahiert dy = 20 - Greenfoot.getRandomNumber(31); // Zufallszahl wird von 20 subtrahier BallCounter = counter; count = 0; } private void pruefeKontaktBlock () // reflexion ds balls am block + verschwinden des blocks { Actor block = getOneIntersectingObject (Block.class); if (block != null) { dy= -dy; getWorld() . removeObject (block); count++; BallCounter.setText("Counter:"+count); } } public class Spielfeld extends World { public Spielfeld() { public void act () { Counter counter = new Counter ("Score: 0"); addObject (counter, 50, 450) ; if (Greenfoot. isKeyDown ("space") && getObjects(Ball.class).size () == 0)// Ball Implementieren,wenn kein Ball im Spiel+Leertaste { Ball ball = new Ball(counter); addObject (ball, 320, 240) ; } } } }
davmac davmac

2015/7/14

#
Newcomer123 Newcomer123

2015/7/14

#
Hi there, currently I'm working on a Counter for my BreakOut project. The counter should be shown on screen after compiling but it doesn't. And I'm not sure if it really is scoring up. Here is the relevant code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class Counter  extends Actor
{
    public Counter(String text)
    {
        
        GreenfootImage img = new GreenfootImage(100,30);   
        img.drawString(text, 10, 20);
        setImage(img);
    }
     
    public void setText (String text)
    {
        GreenfootImage img = getImage();
        img.clear();
        img.drawString(text, 10, 20);
    }   
}
 
public class Ball extends Actor
{
   private int dx;                                // Objektvariable dx für x
    
   private int dy;                                 // Objektvariable dy für y
   
   public int count;
    
   private Counter BallCounter;
    
    
   public Ball(Counter counter)
     {
       dx = 20 - Greenfoot.getRandomNumber(31);          // Zufallszahl wird von 20 subtrahiert
       dy = 20 - Greenfoot.getRandomNumber(31);          // Zufallszahl wird von 20 subtrahier
        
       BallCounter = counter;
        
       count = 0;
    
         
private void pruefeKontaktBlock ()       // reflexion ds balls am block + verschwinden des blocks
        {
            Actor block = getOneIntersectingObject (Block.class);
            if (block != null)
            {
                dy= -dy;
                getWorld() . removeObject (block);
                 
                count++;
                BallCounter.setText("Counter:"+count);
            }
             
        }
public class Spielfeld  extends World
{
    public Spielfeld()
    {   
       public void act ()
       {
          Counter counter = new Counter ("Score: 0");
             addObject (counter, 50, 450) ;
          if (Greenfoot. isKeyDown ("space") && getObjects(Ball.class).size () == 0)// Ball Implementieren,wenn kein Ball im Spiel+Leertaste
            {
               Ball ball = new Ball(counter);
                  addObject (ball, 320, 240) ;
             }
       }
  }
}
danpost danpost

2015/7/14

#
Can you please post the entire code for your Spielfeld class.
Newcomer123 Newcomer123

2015/7/14

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class Spielfeld  extends World
{
    public Spielfeld()
    {   
         
        super(640, 480, 1); // Spielfeldgröße (Kachelzahl,Kachelzahl,Kachelgröße)
         
         
         
       {
           
        for (int i=0;i<620;i=i+35) // Blöcke implementieren
        {
            Block mein_block = new Block();
            addObject(mein_block,i+23,10);
        }
         
        for (int i=0;i<620;i=i+35) // blöcke implementieren
        {
            Block mein_block = new Block();
            addObject(mein_block,i+23,30);
        }
         
        for (int i=0;i<620;i=i+35// Blöcke implementieren
        {
            Block mein_block = new Block();
            addObject(mein_block,i+23,50);
        }
         
        for (int i=0;i<620;i=i+35) // Blöcke implementieren
        {
            Block mein_block = new Block();
            addObject(mein_block,i+23,70);
        }
              
        Paddel paddel = new Paddel ();  // Paddel implementieren
            addObject (paddel, 320, 473) ;
         
         
         
        for (int i=10; i<60;i=i+20) // Leben implementieren
        {
           Leben mein_leben = new Leben ();
           addObject (mein_leben, i+10, 462);
        }
    }
}
 public void act ()
    {
      Counter counter = new Counter ("Score: 0");
             addObject (counter, 50, 450) ;
        if (Greenfoot. isKeyDown ("space") && getObjects(Ball.class).size () == 0)// Ball Implementieren,wenn kein Ball im Spiel+Leertaste
        {
            Ball ball = new Ball(counter);
            addObject (ball, 320, 240) ;
        }
    }
     
}
danpost danpost

2015/7/14

#
If you follow what your Spielfeld 'act' method is doing in your head, you should see a major issue in the creating and adding to the world of Counter objects. Remember that the 'act' method is called continuously. That means each time it is called, you will create and add one into the world. This should only be done when a ball is added. You should probably make sure no Counter objects are in the world first (before creating and adding a new one).
Newcomer123 Newcomer123

2015/7/15

#
Thank you for the help
You need to login to post a reply.