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

2012/11/24

Oh boy! Here we go again!

Minion1 Minion1

2012/11/24

#
This one has brought me to my knees. I've been trying to figure it out for weeks! I'm trying to make a simple game in a bird's eye style. A character runs about shooting monsters with his weapon. So far so good. The character has a life value (a variable called "life"), and what I would like to do is display that value on the world. It sounds so simple doesn't it?? I believe I have to do this from the subworld constructor through an instance of the "Counter" class that I pulled from the "Asteroids" book example scenario. I have tried everything, but I just cannot seem to pass the value of "life" from my Hero class to the counter object. Is there an easier way to do this?? Or has anyone tried to do something like it? I will post code if I must, but if I did I'm not sure where to begin in posting it. Please let me know if you can help, and thanks again.
Minion1 Minion1

2012/11/24

#
Essentially I'm hoping that anyone who has ever had a number displayed in the visual part of their world (that updates regularly) will tell me how they did it. I've been digging through the Asteroids example "scoreCounter" and trying to reverse engineer from there, but I'm hitting walls. The truth is, I have NO clue how the scoreCounter is doing what it's doing.
Gevater_Tod4711 Gevater_Tod4711

2012/11/24

#
If you just want the value from your variable life in your counter class it's easy. Probably you have got only one hero in your world so you can just use static variables:
//in your hero class;
//instead of private int life do this:
private static int life;

//and some methods to get and set this value;
public static int getLife() {
    return life;
}
public static void setLive(int number) {//this is not necessary yet but probably will be somwhen;
    life += number;
}
If you have added this code to your hero you can easily get this values from your counter class:
//in your counter class;
private int herosLife;

public void act() {
    herosLife = Hero.getLife();//Hero has to be the classname of your hero class. If it's called somehow else you have to change the Hero.getLife() into theRealClassname.getLife();
}
now the variable herosLive in your counter class has got the same value as your live variable in your hero class.
Minion1 Minion1

2012/11/24

#
Thanks for the reply. Well, it all compiles but the counter still stays "0" on the screen. I have no doubt that the Counter class is now receiving the value from the Hero.class, but it doesn't know what to do with it. Still, it's a step in the right direction, I feel that I'm closer. Also, I'm not sure that I want to change the "Counter" class at all. That class is set up to display values on the screen, and it should work for everything. In the Asteroids example, the counter class is not modified in any way, but an instance of the class is called and stored into a variable called "scoreCounter" and then modified through the variable. That's what I'm trying to get to happen in my scenario.
danpost danpost

2012/11/24

#
You say it say zero on the screen; but, did you right click on the object when the scenario was stopped and inspect the value? Well, if it says zero on the screen, the inspected value probably is zero. One possibility that would result in what you are experiencing is that the object on the screen is not the same object that you are changing the value of. This could happen if you set the counter variable to a new counter object in the field declaration area and then add a NEW counter (instead of the one already declared) to the world.
danpost danpost

2012/11/25

#
I edited my last reply.
You need to login to post a reply.