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

2017/2/14

Show variable

Bassieboy21 Bassieboy21

2017/2/14

#
hey, i've been using greenfoot for a while right now and started to think how could i show a variable. i know there are already other questions about but those codes wont work. i yesterday finaly found how to show a variable but it needs to be in the world class, but my variables are in a actor class so i dont know how to bring over my variable from my actor class to my world class. can anyone please help me. TNX
danpost danpost

2017/2/14

#
Most probably your variable is in an Actor object (not class). If you keep a reference to that object in your World object, then the world can get access to the value in question. For further help, you need to show the class codes of the actor in question, the class codes of your World subclass that the actor is placed into and specify the field in question and where your attempt is in bringing in the value.
Bassieboy21 Bassieboy21

2017/2/14

#
i dont get it. i want to know how to use my variables that is used in my actor (object) Rocket in my world class to get them showed as the actor (object) var. can you help me out.
danpost danpost

2017/2/14

#
Bassieboy21 wrote...
i want to know how to use my variables that is used in my actor (object) Rocket in my world class to get them showed as the actor (object) var.
danpost wrote...
For further help, you need to show the class codes of the actor in question, the class codes of your World subclass that the actor is placed into and specify the field in question and where your attempt is in bringing in the value.
Nosson1459 Nosson1459

2017/2/15

#
You can do:
Rocket rocket = new Rocket(); // do the right things for your actor and constructor
// then you can do
int rocketVariable = rocket.variableName; // use the name for your variable
Bassieboy21 wrote...
i want to know how to use my variables that is used in my actor (object) Rocket in my world class to get them showed as the actor (object) var.
danpost wrote...
For further help, you need to show the class codes of the actor in question, the class codes of your World subclass that the actor is placed into and specify the field in question and where your attempt is in bringing in the value.
Bassieboy21 Bassieboy21

2017/2/15

#
the codes worked thanks for that but the score isn't refreshing if there is a change in the actor. like if i get a point the score in rocket will change but the score variable thats showed won't refresh. so if i get a point: there will be +1 on the variable score, but that 1 point wont show.
danpost danpost

2017/2/15

#
Bassieboy21 wrote...
the codes worked thanks for that but the score isn't refreshing if there is a change in the actor. like if i get a point the score in rocket will change but the score variable thats showed won't refresh. so if i get a point: there will be +1 on the variable score, but that 1 point wont show.
Show your codes.
Nosson1459 Nosson1459

2017/2/16

#
Nosson1459 wrote...
Bassieboy21 wrote...
i want to know how to use my variables that is used in my actor (object) Rocket in my world class to get them showed as the actor (object) var.
danpost wrote...
For further help, you need to show the class codes of the actor in question, the class codes of your World subclass that the actor is placed into and specify the field in question and where your attempt is in bringing in the value.
danpost wrote...
Bassieboy21 wrote...
the codes worked thanks for that but the score isn't refreshing if there is a change in the actor. like if i get a point the score in rocket will change but the score variable thats showed won't refresh. so if i get a point: there will be +1 on the variable score, but that 1 point wont show.
Show your codes.
Bassieboy21 Bassieboy21

2017/2/16

#
    Actor Bomb;
    Bomb = getOneObjectAtOffset(0, 0, Bomb.class);
    if (Bomb != null)
    {
       World world;
       world = getWorld();
       world.removeObject(Bomb);
       Score3 -= 1;
       Greenfoot.playSound("Bomb.mp3");
    }   
    Actor Fly;
    Fly = getOneObjectAtOffset(0, 0, Fly.class);
    if (Fly != null)
    {
       World world;
       world = getWorld();
       world.removeObject(Fly);
       Score3 += 1;
       Greenfoot.playSound("Score.mp3");
    }  
        rocket rocket = new rocket();
        int Score = rocket.Score3;
        addObject(rocket, 93, 214);
     int Score3 = ((space)getWorld()).Score;    
    setImage(new GreenfootImage(""+Score3, 35, null, null));
Bassieboy21 Bassieboy21

2017/2/16

#
first is from the rocket second also third from the world class and third from Var wich will have to show the variable. sorry for being unclear
danpost danpost

2017/2/16

#
Okay, the following line in your space class:
int Score = rocket.Score3;
creates a local int variable named 'Score' that is assigned the current value of 'rocket.Score3', which is obviously zero since the object was just created and not added into the world yet. Obviously, since you already know its value, getting it in a local variable at that time really does not make any sense. It appears you put a 'Score' field in the space class as well. This is not the same variable as what that line assigns the 'Score3' rocket value to, either. In other words, you currently have three variables here -- a 'Score' field in the space class (which is accessed by your last 2-liner post); a local 'Score' variable (which is lost as soon as the method or constructor that line is in completes); and the 'Score3' field in the rocket class. Now, you should only have one field for the score and that should probably be in the rocket class. That which you might want in the space class is a reference to the rocket itself (move line 1 of the 3-liner above to outside the method or constructor and remove line 2). Now, the first line of the 2-liner (for Var) can be this:
int Score3 = ((space)getWorld()).rocket.Score3;
You need to login to post a reply.