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

2017/1/24

can somebody help me with my winscreen?

Recorsi Recorsi

2017/1/24

#
hey im a newbie with greenfoot and programming. my first project is a breakout clone and its working right now. but now i wanted to implement a winscreen if you hit all 80 blocks it comes up with a "you won" message. i wrote the you won message in the counter class. is that wrong? i comes up with a message but its missplaced and i dont know how to place it in the middle of the screen. (sry for bad english its not my mother tongue)
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Counter here.
 *
 * @author Julian Kraus
 * @version 22.01.17
 */
public class Counter extends Actor
{
    int score = 0;
    public void act()
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.BLACK, Color.WHITE));
        Gewonnen();
    }
    public void addScore()
    {
        score++;
    }
    public void Gewonnen(){
        if (score == 80)
        {Victory victory = new Victory();
         setImage(new GreenfootImage("Du hast gewonnen!", 48, Color.WHITE, Color.BLACK));
         getWorld().addObject (new Victory(),310 , 240);
         Greenfoot.delay(60);
         Greenfoot.setWorld(new StartScreen());
        }
    }
}
Super_Hippo Super_Hippo

2017/1/24

#
A few things: 1. Move line 15 after line 19. No need to check it when the score didn't change. 2. You create a 'Victory' object in line 23. In line 25, you add a different one to the world. 3. Line 24 is also changing the image of the Counter, not of the Victory object you want to add to the world. (I am not sure if this is wanted.) 4. To place it in the middle of the world, it would usually be enough to change the numbers in line 25. You can use 'getWorld().getWidth()/2' and 'getWorld().getHeight()/2'.
Recorsi Recorsi

2017/1/25

#
Super_Hippo wrote...
A few things: 1. Move line 15 after line 19. No need to check it when the score didn't change. 2. You create a 'Victory' object in line 23. In line 25, you add a different one to the world. 3. Line 24 is also changing the image of the Counter, not of the Victory object you want to add to the world. (I am not sure if this is wanted.) 4. To place it in the middle of the world, it would usually be enough to change the numbers in line 25. You can use 'getWorld().getWidth()/2' and 'getWorld().getHeight()/2'.
now i made a new class called victory and i added this:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Eine Nachricht die erscheint, wenn alle Blöcke abgetroffen wurden.
 *
 * @author Julian Kraus
 * @version 25.01.17
 */
public class Victory extends Actor
{
    
    public void act()
    {
        setImage(new GreenfootImage("Du hast gewonnen!", 48, Color.WHITE, Color.BLACK));
        Gewonnen();
    
    public void Gewonnen(){
        World myWorld = getWorld();
        if (score == 80)
        {//Victory victory = new Victory();
         getWorld().addObject (new Victory(),310 , 240);
         Greenfoot.delay(60);
         Greenfoot.setWorld(new StartScreen());
        }
      }
}
but now i doesnt get what the counter is. how do i access the variable of an other class in the victory class?
Super_Hippo Super_Hippo

2017/1/25

#
If you look at what you have there, you will see that endless Victory object are added to the world whenever the first one is added. You can either access it with getting a reference to the Counter object like this...
1
(Counter) getWorld().getObjects(Counter.class).get(0);
... or pass the value when you create the Victory object like this:
1
2
//when creating the object
new Victory(score)
1
2
3
4
5
//victory constructor
public Victory(int score)
{
    //set Image and use the score variable
}
Recorsi Recorsi

2017/1/25

#
Super_Hippo wrote...
If you look at what you have there, you will see that endless Victory object are added to the world whenever the first one is added. You can either access it with getting a reference to the Counter object like this...
1
(Counter) getWorld().getObjects(Counter.class).get(0);
... or pass the value when you create the Victory object like this:
1
2
//when creating the object
new Victory(score)
1
2
3
4
5
//victory constructor
public Victory(int score)
{
    //set Image and use the score variable
}
i tried it with the two ways but i think i did something wrong because it isnt working. but i really dont get where the error is.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Eine Nachricht die erscheint, wenn alle Blöcke abgetroffen wurden.
 *
 * @author Julian Kraus
 * @version 25.01.17
 */
public class Victory extends Actor
{
    
    public void act()
    {
        setImage(new GreenfootImage("Du hast gewonnen!", 48, Color.WHITE, Color.BLACK));
        //Win();
    
   /* public void Win (int score)
    {
        if (score == 80)
        getWorld().getObjects(Counter.class).get(0);
        {//Victory victory = new Victory();
         getWorld().addObject (new Victory(),310 , 240);
         Greenfoot.delay(60);
         Greenfoot.setWorld(new StartScreen());
        }
       
      }
      */
      public Victory (int score)
      {
        setImage(new GreenfootImage("Du hast gewonnen!", 48, Color.WHITE, Color.BLACK));
        if (score == 80)
        {
        getWorld().addObject (new Victory(),310 , 240);
         Greenfoot.delay(60);
         Greenfoot.setWorld(new StartScreen());
        }
        }
         
}
Super_Hippo Super_Hippo

2017/1/25

#
Okay. The code is inside the Victory class. So it will only execute when a Victory object was created. → If you add a Victory object from the Victory class, no Victory object will ever be created (or if you also create it from somewhere else, you will create multiple ones). If I understood you correctly, you only want to add the Victory object when the score is 80. So you need to check for 'score == 80' in the Counter class and add the Victory object then. And from the Counter class (where the score variable is located), you can pass the score if necessary. But... if you only want to create a Victory object when the score is 80, the Victory doesn't need to access the score variable at all.
Recorsi Recorsi

2017/1/26

#
Super_Hippo wrote...
Okay. The code is inside the Victory class. So it will only execute when a Victory object was created. → If you add a Victory object from the Victory class, no Victory object will ever be created (or if you also create it from somewhere else, you will create multiple ones). If I understood you correctly, you only want to add the Victory object when the score is 80. So you need to check for 'score == 80' in the Counter class and add the Victory object then. And from the Counter class (where the score variable is located), you can pass the score if necessary. But... if you only want to create a Victory object when the score is 80, the Victory doesn't need to access the score variable at all.
Now i wrote the "Victory victory = new Victory();" in the world class and the "score == 80" thing in the counter class. for testing i set the score to 80 the object is created but there is only a green foot at the middle of the screen its doesnt say something like "you won". and i dont get what you mean with multiple victory objects, how do i create only one?
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Counter here.
 *
 * @author Julian Kraus
 * @version 22.01.17
 */
public class Counter extends Actor
{
    public int score = 0;
     
    public void act()
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.BLACK, Color.WHITE));
        Win();
    }
    public void addScore()
    {
        score = 80;
    }
    public void Win() {
    if (score == 80)
    {
        getWorld().addObject (new Victory(),310 , 240);
         Greenfoot.delay(100);
         Greenfoot.setWorld(new StartScreen());
    }
    }
   
}
danpost danpost

2017/1/26

#
What is your Victory class code?
Recorsi Recorsi

2017/1/26

#
danpost wrote...
What is your Victory class code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Eine Nachricht die erscheint, wenn alle Blöcke abgetroffen wurden.
 *
 * @author Julian Kraus
 * @version 25.01.17
 */
public class Victory extends Actor
{
    Victory victory = new Victory();
    public void act()
    {
        setImage(new GreenfootImage("Du hast gewonnen!", 48, Color.WHITE, Color.BLACK));
         
    
  
         
}
danpost danpost

2017/1/26

#
Change:
1
2
3
public void act()
// to
public Victory()
Recorsi Recorsi

2017/1/26

#
danpost wrote...
Change:
1
2
3
public void act()
// to
public Victory()
omg it works finally :D thanks super_hippo and danpost!
Super_Hippo Super_Hippo

2017/1/26

#
You can also remove line 11.
You need to login to post a reply.