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

2018/5/5

How do I reference an actor's integer to the world

1
2
Lavenger Lavenger

2018/5/10

#
Yehuda wrote...
Have lines 4 and 5 just be:
int yenPerMin;

// then add the following method into the class
protected void addedToWorld(World world) {
    yenPerMin = world.MKLevel * 8192;
}
I was going to post this yesterday (right after question was asked) but I was out of time.
is there anything special a addedToWorld() does? Something like how an act() method work also, when i did that, it wouldve seem like I have broken GainYen() method in MgrKanau's actor class code. what it looks like is that YenPerMin's value wasn't given to the GainYen() method thus it wasn't passively generating Yen for the player Updated code:
import greenfoot.*;
public class MgrKanau extends Actor
{
    int YenPerMin;
    protected void addedToWorld(MyWorld mw)
    {
        YenPerMin = mw.MKLevel*8192;
    }
    public void act() 
    {
        if(getWorld().getObjects(MgrKanau.class).size() != 0) GainYen();
    }
    public void GainYen() 
    {
        MyWorld mw = (MyWorld)getWorld();
        float YenAdjustment = 0; // every cycle this value will pe deposited onto the world's yen value
        Greenfoot.setSpeed(50);
        YenAdjustment += YenPerMin / 60f /50;
        mw.adjustYen(YenAdjustment);
        // When buying something, only subtract the price from AGold!
    }
}
danpost danpost

2018/5/11

#
The Actor class addedToWorld method uses a World object for its parameter -- not a MyWorld object. Use this:
protected void addedToWorld(World world)
{
    YenPerMin = ((MyWorld)world)..MKLevel*8192;
}
Yehuda Yehuda

2018/5/11

#
...as you were instructed, and yes, the "addedToWorld" method is overriding an already existing method with that name just like the "act" method does.
danpost danpost

2018/5/11

#
Yehuda wrote...
...as you were instructed, and yes, the "addedToWorld" method is overriding an already existing method with that name just like the "act" method does.
... and is called by the greenfoot framework on actors when added into a world.
You need to login to post a reply.
1
2