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

2023/5/4

Transfer values ​​from world class to an actor class and vice versa

Ramrun Ramrun

2023/5/4

#
Hi, I'm new to the field and I have a problem that I want to pass an int/boolean value from a World subclass to an Actor subclass and vice versa. Can someone write me an example for both cases? Thanks!
danpost danpost

2023/5/4

#
Ramrun wrote...
I want to pass an int/boolean value from a World subclass to an Actor subclass and vice versa. Can someone write me an example for both cases?
There are too many different scenarios that makes for a non-so-easy answer. It would be best if you asked about a specific case (showing attempted codes, preferably; and providing class names involved), where we can then show how to with that as the example for that type of case. You can then, give another case for scrutinizing.
Ramrun Ramrun

2023/5/4

#
danpost wrote...
Ramrun wrote...
I want to pass an int/boolean value from a World subclass to an Actor subclass and vice versa. Can someone write me an example for both cases?
There are too many different scenarios that makes for a non-so-easy answer. It would be best if you asked about a specific case (showing attempted codes, preferably; and providing class names involved), where we can then show how to with that as the example for that type of case. You can then, give another case for scrutinizing.
In one of my cases, the value of three int variables is determined in the subworld class 'Bloodstream' in the following code:
    public void act(){
        if(Greenfoot.isKeyDown("1")) {
            mschwierigkeit = 1;
            mherzen= 20;
            mlevel= 1;
        }
        if(Greenfoot.isKeyDown("2")) {
            mschwierigkeit = 2;
            mherzen= 15;
            mlevel= 2;
        }
        if(Greenfoot.isKeyDown("3")) {
            mschwierigkeit = 20;
            mherzen= 10;
            mlevel= 3;
        }
}
It should then be possible to use these three variables in the actor subclass 'whitecell' That is my try:
int schwierigkeit = mschwierigkeit.Bloodstream
int herzen = mherzen.Bloodstream
int level = mlevel.Bloodstream
danpost danpost

2023/5/5

#
Ramrun wrote...
In one of my cases, the value of three int variables is determined in the subworld class 'Bloodstream' in the following code: << Code Omitted >> It should then be possible to use these three variables in the actor subclass 'whitecell' That is my try:
int schwierigkeit = mschwierigkeit.Bloodstream
int herzen = mherzen.Bloodstream
int level = mlevel.Bloodstream
Assuming the variables in question are declared static, all you need do is put the class name first (before the variable names) on the right side of the 3 lines. If they are not static, then the values must be gotten a different way.
You need to login to post a reply.