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

2018/3/5

Gold Income

1
2
3
Lavenger Lavenger

2018/3/6

#
CubeGamer6 wrote...
Lavenger wrote...
It says: "cannot find symbol- method adjustYen(int)"
The adjustYen method is within the World, not the YenDisplay. Just remove the "YenDisplay." from the "YenDisplay.adjustYen(0)"
oh okay
Lavenger Lavenger

2018/3/6

#
Lavenger wrote...
so I tried typing out the code:
import greenfoot.*;
public class MyWorld extends World
{
    // fields usually (by convention) go first
    private int Yen;
    private int YenTimer;
    private Actor YenDisplay = new Yen();
    // class constructors usually (by convention) go next
    public MyWorld()
    {
        super(1400, 675, 1);
        adjustYen(0); // to initialize image
        addObject(YenDisplay, 100, 30); // wherever
    }     
    // methods (by convention) go last
    public int getAccumulatedYen()
    {
        return Yen;
    }
    public void adjustYen(int adjustment)
    {
        Yen += adjustment;
        YenDisplay.setImage(new GreenfootImage("Yen: "+Yen, 30, Color.BLACK, Color.WHITE));
    }
    public void act()
    {
    accumulateYen();
    }
    private void accumulateYen()
    {
    YenTimer = (YenTimer+1)%60;
    if (YenTimer == 0) adjustYen(1);
    }
        private void prepare()
    {
        addObject(new Umaru(), 700, 338);
        addObject(new Taihei(), 1356,108);
    }
}
After removing the YenDisplay, I ran the program and it seems like its gaining 1 Yen/second without me doing anything, what is happening??
Lavenger Lavenger

2018/3/6

#
@danpost @CubeGamer6 I have uploaded the scenario for you guys to check the code and see if I have messed up somewhere while following your instructions, it would've seem like whenever I placed the Umaru object into the world, the YenDisplay would display the Yen going up rapidly without me doing anything but when I don't place the Umaru object into the world and just leave it be, It would go up 1 Yen/second, with that I can safely say that the "Link" between the character and display is established but the Umaru object seems to be broken now due to that, it used to be able to gain 1 Yen per spacebar input though, I really couldn't figure out what I did wrong :/
CubeGamer6 CubeGamer6

2018/3/6

#
I looked at your code and i noticed that the Umaru's act() was constantly increasing the Yen count by one. Thats why it increased so quickly. Also, i'm confused because you keep track of an amount of Yen in the world, but the Umaru is also keeping track of an amount of Yen. Which class is actually supposed to keep track?
danpost danpost

2018/3/6

#
CubeGamer6you keep track of an amount of Yen in the world, but the Umaru is also keeping track of an amount of Yen. Which class is actually supposed to keep track?[/quote wrote...
It is best to track it in the world class since Umara is not the only thing increasing its value (time is, as well as other objects). Remove any yen code in the Umara class and put code in to change yen in the world class. Also, I noticed that the 'prepare' method in the world is not being called by the world constructor.
CubeGamer6 CubeGamer6

2018/3/6

#
danpost wrote...
It is best to track it in the world class since Umara is not the only thing increasing its value (time is, as well as other objects). Remove any yen code in the Umara class and put code in to change yen in the world class. Also, I noticed that the 'prepare' method in the world is not being called by the world constructor.
I see, well i'm working on both and it seems to be coming along well
CubeGamer6 CubeGamer6

2018/3/6

#
I've got it working @Lavenger Umanu class:
import greenfoot.*;
public class Umaru extends Actor
{
    public void act() 
    {
        GainYen();
    }
    private boolean once = false;
    public boolean repeatBuff = false; // you can set this to true and it will start repeating
    private int YenPerMin = 90;
    private int YenPerAct = 1;
    public void GainYen() 
    {
        float YenAdjustment = 0; // every cycle this value will pe deposited onto the world's yen value
        if(Greenfoot.isKeyDown("space") && (!once || repeatBuff)) 
        {
             YenAdjustment += YenPerAct;
             once = true;
        }
        else if(!Greenfoot.isKeyDown("space") && once)
        {
             once = false;
        }
        Greenfoot.setSpeed(50);
        YenAdjustment += YenPerMin / 60f / 50;
        MyWorld mw = (MyWorld)getWorld();
        mw.adjustYen(YenAdjustment);
        // When buying something, only subtract the price from AGold!
    }
}
MyWorld class:
import greenfoot.*;
public class MyWorld extends World
{
    // fields usually (by convention) go first
    public float AYen; //precise
    public int DYen; //rounded
    public Yen YenDisplay = new Yen();
    // class constructors usually (by convention) go next
    public MyWorld()
    {
        super(1400, 675, 1);
        adjustYen(0); // to initialize image
        prepare();
        addObject(YenDisplay, 100, 30); // wherever
    }     
    // methods (by convention) go last
    public int getAccumulatedYen()
    {
        return DYen;
    }
    public void adjustYen(float adjustment)
    {
        AYen += adjustment;
        DYen = (int) AYen;
        YenDisplay.setImage(new GreenfootImage("Yen: "+DYen, 30, Color.BLACK, Color.WHITE));
    }
    private void prepare()
    {
        addObject(new Umaru(), 700, 338);
        addObject(new Taihei(), 1356,108);
    }
}
What i've done is given the world a precise Yen counter and made it so that the Umanu class deposits a little bit of money each time. And in the Umanu class, i've added a YenAdjustment value that is increased by the per-minute earnings and spacebar earnings and is deposited to the world each time. I hope it helps!
Lavenger Lavenger

2018/3/6

#
danpost wrote...
It is best to track it in the world class since Umaru is not the only thing increasing its value (time is, as well as other objects). Remove any yen code in the Umara class and put code in to change yen in the world class.
So do I remove the GainYen?
CubeGamer6 CubeGamer6

2018/3/6

#
Lavenger wrote...
danpost wrote...
It is best to track it in the world class since Umaru is not the only thing increasing its value (time is, as well as other objects). Remove any yen code in the Umara class and put code in to change yen in the world class.
So do I remove the GainYen?
The gainYen method is the Umanu's way of increasing the total Yen amount, at least in the adjustments i have made.
Lavenger Lavenger

2018/3/6

#
CubeGamer6 wrote...
I've got it working @Lavenger Umanu class:
import greenfoot.*;
public class Umaru extends Actor
{
    public void act() 
    {
        GainYen();
    }
    private boolean once = false;
    public boolean repeatBuff = false; // you can set this to true and it will start repeating
    private int YenPerMin = 90;
    private int YenPerAct = 1;
    public void GainYen() 
    {
        float YenAdjustment = 0; // every cycle this value will pe deposited onto the world's yen value
        if(Greenfoot.isKeyDown("space") && (!once || repeatBuff)) 
        {
             YenAdjustment += YenPerAct;
             once = true;
        }
        else if(!Greenfoot.isKeyDown("space") && once)
        {
             once = false;
        }
        Greenfoot.setSpeed(50);
        YenAdjustment += YenPerMin / 60f / 50;
        MyWorld mw = (MyWorld)getWorld();
        mw.adjustYen(YenAdjustment);
        // When buying something, only subtract the price from AGold!
    }
}
MyWorld class:
import greenfoot.*;
public class MyWorld extends World
{
    // fields usually (by convention) go first
    public float AYen; //precise
    public int DYen; //rounded
    public Yen YenDisplay = new Yen();
    // class constructors usually (by convention) go next
    public MyWorld()
    {
        super(1400, 675, 1);
        adjustYen(0); // to initialize image
        prepare();
        addObject(YenDisplay, 100, 30); // wherever
    }     
    // methods (by convention) go last
    public int getAccumulatedYen()
    {
        return DYen;
    }
    public void adjustYen(float adjustment)
    {
        AYen += adjustment;
        DYen = (int) AYen;
        YenDisplay.setImage(new GreenfootImage("Yen: "+DYen, 30, Color.BLACK, Color.WHITE));
    }
    private void prepare()
    {
        addObject(new Umaru(), 700, 338);
        addObject(new Taihei(), 1356,108);
    }
}
What i've done is given the world a precise Yen counter and made it so that the Umanu class deposits a little bit of money each time. And in the Umanu class, i've added a YenAdjustment value that is increased by the per-minute earnings and spacebar earnings and is deposited to the world each time. I hope it helps!
Thank you very much ^^ I'm going to sleep for now and work on this project tomorrow... It's 02:28 am here and I have been coding around this project since 17:30pm @-@ Thank you danpost, CubeGamer6 and Hippo for teaching and guiding me through this ^^
CubeGamer6 CubeGamer6

2018/3/6

#
Lavenger wrote...
Thank you very much ^^ I'm going to sleep for now and work on this project tomorrow... It's 02:28 am here and I have been coding around this project since 17:30pm @-@ Thank you danpost, CubeGamer6 and Hippo for teaching and guiding me through this ^^
No problem! :)
You need to login to post a reply.
1
2
3