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/5

#
I would like to make 2 forms of gold income, one by gaining a certain amount of gold my pressing spacebar(gold/action) and the other one is gaining gold at a steady pace(gold/min). anyone has any idea how to do that?
CubeGamer6 CubeGamer6

2018/3/5

#
I hope this code works You should just be able to put this method right in the act() Also when getting upgrades, you can just change the moneyPerAct and moneyPerMin values no prob
private float preciseGold = 0;
public int gold = 0;

private boolean once = false;
private int moneyPerMin = 80;
private int moneyPerAct = 10;
private void gainMoney() {
    if(Greenfoot.isKeyDown("space") && !once) {
        preciseGold += moneyPerAct;
        once = true;
    } else if(!Greenfoot.isKeyDown("space) && once) once = false;
    // assuming greenfoot's speed is 50
    // if you want to make sure it stays at 50, uncomment the next line
    // Greenfoot.setSpeed(50);
    preciseGold += moneyPerMin / 60f / 50;
    gold = (int) preciseGold;
    // When buying something, only subtract the price from preciseGold!
}
I made this on my phone but i hope it helps. Also, the preciseGold value is used so that i could make it increase smoothly overtime. The gold value is just for displaying purposes, hence why it is rounded to int
Lavenger Lavenger

2018/3/5

#
I'm using a code to display the gold(Yen):
GreenfootImage image = new GreenfootImage("Yen: " + DYen, 20, Color.BLACK, Color.WHITE);
setImage(image);
but it seems to be stuck at 0 no matter how many times I pressed/held space am I doing something wrong?? PS: what is 60f?
danpost danpost

2018/3/5

#
Lavenger wrote...
I'm using a code to display the gold(Yen): << Code Omitted >> but it seems to be stuck at 0 no matter how many times I pressed/held space am I doing something wrong??
Probably, show the entire class code.
PS: what is 60f?
The number '60' as a 'float' type value.
CubeGamer6 CubeGamer6

2018/3/5

#
Lavenger wrote...
I'm using a code to display the gold(Yen):
GreenfootImage image = new GreenfootImage("Yen: " + DYen, 20, Color.BLACK, Color.WHITE);
setImage(image);
Have you put the "gainMoney()" method in the act()? I have put this code together and it seems to work fine:
import greenfoot.*;

public class Item extends Actor
{
    private GreenfootImage graphics;
    
    public void act() {
        gainMoney();
        graphics = new GreenfootImage("Yen: " + gold, 20, Color.BLACK, Color.WHITE);
        setImage(graphics);
    }
    
    private float preciseGold = 0;
    public int gold = 0;
    private boolean once = false;
    private int moneyPerMin = 80;
    private int moneyPerAct = 10;

    private void gainMoney() {
        if(Greenfoot.isKeyDown("space") && !once) {
            preciseGold += moneyPerAct;
            once = true;
        } else if(!Greenfoot.isKeyDown("space") && once) once = false;
        preciseGold += moneyPerMin / 60f / 50;
        gold = (int) preciseGold;
    }
}
Also like danpost said, the 60f is a float type. I used it so that the calculations would be at float precision. (7 decimals)
Lavenger Lavenger

2018/3/6

#
Here's what I did:
import greenfoot.*;
public class Umaru extends Actor
{
    public void act() 
    {
        GainYen();
    }
    public int DYen = 0;
    private float AYen = 0; 
    private boolean HoldSpaceBuff = false;
    private int YenPerMin = 90;
    private int YenPerAct = 1;
    public void GainYen() 
    {
        if(Greenfoot.isKeyDown("space") && !HoldSpaceBuff) 
            {
             AYen += YenPerAct;
             HoldSpaceBuff = true;
            }
        else if(!Greenfoot.isKeyDown("space") && HoldSpaceBuff)
            {
             HoldSpaceBuff = false;
             Greenfoot.setSpeed(50);
             AYen += YenPerMin / 60f / 50;
            }
        DYen = (int) AYen;
        // When buying something, only subtract the price from AGold!
    }
}
I have placed the Yen display into another actor:
import greenfoot.*;
public class Gold extends Actor
{
    public int DYen = 0;
    public void act() 
    {
        GreenfootImage image = new GreenfootImage("Yen: " + DYen, 20, Color.BLACK, Color.WHITE);
        setImage(image);
    }
}
Super_Hippo Super_Hippo

2018/3/6

#
Both are using a different variable. It has the same name but that doesn't mean that it is the same.
Lavenger Lavenger

2018/3/6

#
Super_Hippo wrote...
Both are using a different variable. It has the same name but that doesn't mean that it is the same.
oh... how do I link those 2 variables up then?
Super_Hippo Super_Hippo

2018/3/6

#
You don't link the two variables. You will only use one variable. The Umaru object could have a reference to the Gold object and then, the Umaru object can control the Gold object (when the variable changes, the Umaru object tells the Gold object to change its image).
Lavenger Lavenger

2018/3/6

#
Super_Hippo wrote...
You don't link the two variables. You will only use one variable. The Umaru object could have a reference to the Gold object and then, the Umaru object can control the Gold object (when the variable changes, the Umaru object tells the Gold object to change its image).
how do I reference to the Gold object from Umaru in this type of situation then?
Super_Hippo Super_Hippo

2018/3/6

#
Are they created from the same "position" in your code, for example right at the beginning? You could do that then:
Gold g = new Gold();
addObject(g, /**x-coord, y-coord*/);
addObject(new Umaru(g), /**x-coord, y-coord*/);
//Gold class
public void updateImage(int yen) 
{
    setImage(new GreenfootImage("Yen: " + yen, 20, Color.BLACK, Color.WHITE));
}
//in Umaru class
private Gold gold;

public Umaru(Gold g)
{
    gold = g;
}

//after a change to DYen was made:
gold.updateImage(DYen);
CubeGamer6 CubeGamer6

2018/3/6

#
If you want to get the DYen from the Umaru object, you could make a constructor for the Gold class that needs an Umaru object, like this
private Umaru umaru = null;
public Gold(Umaru umaru) {
    this.umaru = umaru;
}
Then in the Umaru class, you can add the addedToWorld() method that greenfoot calls automatically. In that method you can create a Gold object that is 'linked' with the Umaru. The method should look something like this:
public void addedToWorld(World world) {
    world.addObject(new Gold(this), 100, 100);
}
Lavenger Lavenger

2018/3/6

#
Super_Hippo wrote...
//in Umaru class
private Gold gold;

public Umaru(Gold g)
{
    gold = g;
}

//after a change to DYen was made:
gold.updateImage(DYen);
do I add this under act() in Umaru?
Lavenger Lavenger

2018/3/6

#
CubeGamer6 wrote...
If you want to get the DYen from the Umaru object, you could make a constructor for the Gold class that needs an Umaru object, like this
private Umaru umaru = null;
public Gold(Umaru umaru) {
    this.umaru = umaru;
}
Then in the Umaru class, you can add the addedToWorld() method that greenfoot calls automatically. In that method you can create a Gold object that is 'linked' with the Umaru. The method should look something like this:
public void addedToWorld(World world) {
    world.addObject(new Gold(this), 100, 100);
}
ahh this was the one that I saw in a video but I had no idea what it was for xD
CubeGamer6 CubeGamer6

2018/3/6

#
Lavenger wrote...
Super_Hippo wrote...
//in Umaru class
private Gold gold;

public Umaru(Gold g)
{
    gold = g;
}

//after a change to DYen was made:
gold.updateImage(DYen);
do I add this under act() in Umaru?
Yes that should work
There are more replies on the next page.
1
2
3