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

2012/1/15

Printing out a variable

Oneshot Oneshot

2012/1/15

#
Hey,
1
private int Health = 100;
I have to print out that variable, but not in the console. I need a HUD which is on the top left on the map. Does anybody has a suggestion how to make that? Best Regards PS: Sorry that i ask so much in last time, but I have to conclude my game very soon and I dont have the time to look for every problem in scenarios /:
Duta Duta

2012/1/15

#
One way is to create an actor (with the name "HealthBar" or something), which you then add to the world in the top left. In the actor's code, you'll want the following: (I'm assuming that the Health variable is stored in the Player's class)
1
2
3
4
5
6
7
8
9
10
11
12
public void act()
{
    GreenfootImage img = new GreenfootImage(width, height);
    float fontSize = 35.0f; //change this to the font size that you want
    Font font = img.getFont();
    font = font.deriveFont(FONT_SIZE);
    img.setFont(font);
    ZombieWorld w = (ZombieWorld) getWorld(); //Replace ZombieWorld with the name of your world.
    Player p = (Player) w.getObjects(Player.class).get(0);
    img.drawString("" + p.Health, x, y); //this x and y position is the bottom-left of the first letter.
    setImage(img);
}
Also, you'll need to change it from
1
private int Health = 100;
to
1
public int Health = 100;
so that it can be accessed from other classes.
Oneshot Oneshot

2012/1/15

#
"cannot find symbol - class Font" I think I have to create a variable, but what does it has to contain? Best Regards
Duta Duta

2012/1/15

#
Oh yeah, forgot to mention that: at the top of your class, add the following:
1
2
import java.awt.Color;
import java.awt.Font;
(Alternatively, you could just add the following instead):
1
import java.awt.*;
and also, I forgot that in this code you need to add something:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void act() 
    GreenfootImage img = new GreenfootImage(width, height);
    img.setColor(Color.BLACK); //HERE. Change BLACK to whatever colour you want the text to be.
    float fontSize = 35.0f; //change this to the font size that you want 
    Font font = img.getFont(); 
    font = font.deriveFont(FONT_SIZE); 
    img.setFont(font); 
    ZombieWorld w = (ZombieWorld) getWorld(); //Replace ZombieWorld with the name of your world. 
    Player p = (Player) w.getObjects(Player.class).get(0); 
    img.drawString("" + p.Health, x, y); //this x and y position is the bottom-left of the first letter. 
    setImage(img); 
}
Also when I say change BLACK to whatever colour you want it to be, it must be a variable in here: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Color.html or else you have to create a new Color object. If you want a color that isn't a variable there, tell me and I'll show you how to make a Color object.
Oneshot Oneshot

2012/1/15

#
Line 7 i had to change to:
1
font = font.deriveFont(fontSize);
Now my source code looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void act()
{
    GreenfootImage img = new GreenfootImage(200, 200);  
    img.setColor(new Color(0,0,0));
    float fontSize = 35.0f; 
    Font font = img.getFont();   
    font = font.deriveFont(fontSize);   
    img.setFont(font);   
    Background w = (Background) getWorld();  
    Player p = (Player) w.getObjects(Player.class).get(0);   
    img.drawString("" + p.health, 200, 200);
    setImage(img);   
}
Now I placed the Actor anywhere on the map. Its a green foot. But it doesn´t work. If I start the game, it disappear.
Duta Duta

2012/1/15

#
Oneshot wrote...
Line 7 i had to change to:
1
font = font.deriveFont(fontSize);
Now my source code looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void act()
{
    GreenfootImage img = new GreenfootImage(200, 200);  
    img.setColor(new Color(255,255,255));
    float fontSize = 35.0f; 
    Font font = img.getFont();   
    font = font.deriveFont(fontSize);   
    img.setFont(font);   
    Background w = (Background) getWorld();  
    Player p = (Player) w.getObjects(Player.class).get(0);   
    img.drawString("" + p.health, 200, 200);
    setImage(img);   
}
Now I placed the Actor anywhere on the map. Its a green foot. But it doesn´t work. If I start the game, it disappear.
First off, when I said the x, y parameters in img.drawString are the first letters x, y co-ordinates of its base, this means that you should change 200, 200 to something like 5, 195. I've changed it in the code here:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void act()
{
    GreenfootImage img = new GreenfootImage(200, 200);  
    img.setColor(new Color(255,255,255)); //by the way, this is Color.WHITE
    float fontSize = 35.0f; 
    Font font = img.getFont();   
    font = font.deriveFont(fontSize);   
    img.setFont(font);   
    Background w = (Background) getWorld();  
    Player p = (Player) w.getObjects(Player.class).get(0);   
    img.drawString("Health: " + p.health, 5, 195); //On this line. Also I changed the empty string to "Health: "
    setImage(img);   
}
Also, the reason its a green foot at first is because there's nothing in its constructor giving it a default image - something like this should work:
1
2
3
4
5
6
7
8
9
10
11
public HealthBar()
    {
        GreenfootImage img = new GreenfootImage(200, 200);  
        img.setColor(new Color(255,255,255));
        float fontSize = 35.0f; 
        Font font = img.getFont();   
        font = font.deriveFont(fontSize);   
        img.setFont(font);   
        img.drawString("Health: 100", 5, 195);
        setImage(img);   
    }
And yeah line 7 was me being silly - I usually have the font size declared as a field in my class, with the name:
1
private static final float FONT_SIZE = 35.0f;
So when I was on autopilot, I wrote FONT_SIZE
Oneshot Oneshot

2012/1/15

#
Ah, okay. Now I understand it and it also works fine ... Thanks a lot! But a last question I have: In the other topic "How to edit a variable of another class" you said I shall look your answer here. Ive tried to assigned to my other problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
Player p = (Player) w.getObjects(Player.class).get(0);   
     
public void angreifen() {
    Actor player = getOneIntersectingObject(Player.class);
    if ( player == null )
       {
           // Nothing happens
        }
        else
        {
            p.health-=5;
        }
}
What does the "w.getObjects" mean? Because I get the error "cannot find symbol - variable w".
Duta Duta

2012/1/15

#
You need this line above the Player p = (Player) etc line:
1
Background w = (Background) getWorld();
What it does it it gets a reference on your world called w, so when you're doing
1
Player p = (Player) w.getObjects(Player.class).get(0);
what you're doing is the getObjects(class) method, which is a world method. What that does is it returns a java.util.List of all the objects of the class you entered as a parameter that are added to the world (if you enter null as a parameter, like so):
1
getObjects(null)
then you get a List of all the objects of all classes that are added to world. Because we want just one object, we do get(0) to return the first object in that list.
Oneshot Oneshot

2012/1/15

#
Okay, thanks for your helpful answer. Ive tried to use it also here, but it doesnt work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
World gameover = gameOver; 
public void angegriffenWerden() {
    Actor player = getOneIntersectingObject(Zombie.class);
    if ( player == null )
    {
        // Der Zombie ist noch nicht beim Spieler
    }
    else
    {
        health-=1;
    }
    if (health <= 0) {
       Greenfoot.setWorld(gameover);  //            Greenfoot.setWorld(new gameover());
       Greenfoot.stop();
    }
}
-> "cannot find symbol - variable gameOver" Its a method in "Player", but I think I have to use something with "getWorld", no "getObject".
Duta Duta

2012/1/15

#
Oneshot wrote...
Okay, thanks for your helpful answer. Ive tried to use it also here, but it doesnt work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
World gameover = gameOver; 
public void angegriffenWerden() {
    Actor player = getOneIntersectingObject(Zombie.class);
    if ( player == null )
    {
        // Der Zombie ist noch nicht beim Spieler
    }
    else
    {
        health-=1;
    }
    if (health <= 0) {
       Greenfoot.setWorld(gameover);  //            Greenfoot.setWorld(new gameover());
       Greenfoot.stop();
    }
}
-> "cannot find symbol - variable gameOver" Its a method in "Player", but I think I have to use something with "getWorld", no "getObject".
Let's take a second to go over your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
World gameover = gameOver;
public void angegriffenWerden() {
    Actor player = getOneIntersectingObject(Zombie.class);
    if ( player == null ) //Which would mean here you would have if(zombie == null)
    {
        // Der Zombie ist noch nicht beim Spieler
    }
    else
    {
        health-=1;
    }
    if (health <= 0) {
       Greenfoot.setWorld(gameover);  //            Greenfoot.setWorld(new gameover());
       Greenfoot.stop();
    }
}
Firstly, what are you doing in this line of code?:
1
World gameover = gameOver;
(It's what's causing the error.) Because below you have this:
1
Greenfoot.setWorld(gameover);  //            Greenfoot.setWorld(new gameover());
I'm assuming you can't be trying to get a reference on a world... Quickly, I think I should go over how Greenfoot.setWorld() works: What you do for it, is the following: Let's say you have two world classes, ZombieWorld and GameOverWorld, and you're currently in ZombieWorld, and want to go to GameOverWorld. To do that, you'd use the following line of code:
1
Greenfoot.setWorld(new GameOverWorld());
(Note that all the objects in ZombieWorld when you call the method will be removed) Next, (and this doesn't really matter, because it will still work the way you're doing it):
1
Actor player = getOneIntersectingObject(Zombie.class);
Maybe you wanted something along the lines of:
1
Zombie zombie = (Zombie) getOneIntersectingObject(Zombie.class);
I only say this because it strange to call an object of the Zombie class by the name player.. Again, this works the way you're currently doing it, its just a note. Oh, and you can change:
1
2
3
4
5
6
7
8
if ( player == null )
        {
            // Der Zombie ist noch nicht beim Spieler
        }
        else
        {
            health-=1;
        }
into:
1
if(player != null) health -= 1;
Hopefully my first section of answer answered your question in a way - basically, you don't need to get a reference on a world to use setWorld()! :) Also, sorry for my slow replies - I have other things I'm doing apart from sitting on the discussion boards :L I'm off for a while to watch tv now, hopefully you don't need much more assistance, or if you do, someone else can help while I'm away. Happy coding! :)
Oneshot Oneshot

2012/1/15

#
Okay, thanks a lot. Now everything works! Maybe you want to test? https://www.socialfan.de/test/game/game.html Move: w, a, s, d + mouse shoot: click left and/or right --- Edit: Maybe you have a better solution for the spawning system? If u played u know, what is bad. They are spawning always on the same position, mostly top left corner.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    private int rate = 200
    private int timer = 450
    int spawnX = 0
    int spawnY = 0;
public void spawnZombie() {
        if(Greenfoot.getRandomNumber(rate) < 1) {
            if (Greenfoot.getRandomNumber(5) == 0)
            {
                spawnY = Greenfoot.getRandomNumber(50) * (getWorld().getHeight() - 1);
                spawnX = Greenfoot.getRandomNumber(getWorld().getWidth());
            
            else 
            {
                spawnX = Greenfoot.getRandomNumber(50) * (getWorld().getWidth() - 1); 
                spawnY = Greenfoot.getRandomNumber(getWorld().getHeight()); 
            
            getWorld().addObject(new Zombie(), spawnX, spawnY);
            timer++;
        }
        if(timer%100 == 0 && rate > 2){
            rate--;      
        }   
    }
Duta Duta

2012/1/15

#
Back from tv, and I like the game by the way :)
1
2
3
4
5
6
7
8
9
10
private int rate = 200
private int timer = 450;
 
public void spawnZombie() {
    if(Greenfoot.getRandomNumber(rate) < 1)
        getWorld().addObject(new Zombie(), Greenfoot.getRandomNumber(getWorld.getWidth()), Greenfoot.getRandomNumber(getWorld.getHeight()));
    if(timer%100 == 0 && rate > 2)
        rate--;
    timer++;
}
Would be a better method in my opinion. Also from playing the game, I've noticed that your bullets are only removed once they hit the top of the world... This means there's something like this in your Bullet.class:
1
2
3
4
5
6
public void act()
{
    //Whatever else you have in your act() method.
     
    if(getY() <= 0) getWorld().removeObject(this);
}
...right? Well if this is the case (or something similar), it would be good to change it to this instead:
1
2
3
4
5
6
public void act()
{
    //Whatever else you have in your act() method.
     
    if(this.atWorldEdge()) getWorld().removeObject(this);
}
danpost danpost

2012/1/16

#
You need the number (2) in the getRandomNumber calls for this to work properly. Also the spawnX and spawnY variables do not need to be object instance variable -- just local to the method. With your timer increment inside the if where you have it, it will only get incremented when a zombie is spawned, so I moved it outside (where it should be). This is how it should look:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private int rate = 200
private int timer = 450
 
public void spawnZombie()
{
    if (Greenfoot.getRandomNumber(rate) < 1)
    {
        int spawnX = 0;
        int spawnY = 0;
        if (Greenfoot.getRandomNumber(2) == 0) // There are only 2 axii
        {
            spawnY = Greenfoot.getRandomNumber(2) * (getWorld().getHeight() - 1); // There are only 2 borders along this axis
            spawnX = Greenfoot.getRandomNumber(getWorld().getWidth());
        }
        else
        {
            spawnX = Greenfoot.getRandomNumber(2) * (getWorld().getWidth() - 1); // There are only 2 borders along this axis
            spawnY = Greenfoot.getRandomNumber(getWorld().getHeight());
        }
        getWorld().addObject(new Zombie(), spawnX, spawnY);
    }
    timer++; // moved out of the 'if' so it can be incremented each act cycle
    if (timer % 100 == 0 && rate > 2) rate--;
}
You need to login to post a reply.