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

2020/12/5

Transfering variable value of actor into variable of another actor

Gbasire Gbasire

2020/12/5

#
Hey, I'm trying to make the value of the variable "speed" of "Boy" actor to the variable "speed" of "Girl" actor but don't know how to do it. I've already managed to make it with a World class, but not with an actor class. For the world, it looked like this and worked well : (boyspeed is the variable in myWorld and speed is the variable in Boy)
public void setBoySpeed()
    {
        ((myWorld)getWorld()).boyspeed = speed;
    }
What code do I need to type to do the same thing for an actor ?
danpost danpost

2020/12/5

#
Add field in world:
publiic Boy boy;
Change line:
Boy boy = new Boy();
// to this
boy = new Boy();
Now girl can use:
speed = ((myWorld)getWorld()).boy.speed;
Without a field in world, it gets a little more complicated:
if (getWorld().getObjects(Boy.class).size() > 0)
{
   speed = ((Boy)getWorld().getObjects(Boy.class).get(0)).speed;
}
Gbasire Gbasire

2020/12/5

#
it works, thanks !
You need to login to post a reply.