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

2018/3/5

Gold Income

1
2
3
CubeGamer6 CubeGamer6

2018/3/6

#
In your gainYen method you have put the part that adds gold per minute in the 'else if' segment. It should actually be outside of it. Also, if you want a buff that lets the spacebar action repeat, you cant actually change the HoldSpaceBuff, because it always gets set to false. Instead you should make a new boolean that, if true will allow for incrementing the gold regardless of the other boolean.
else if(Greenfoot.isKeyDown("space") && (!once || HoldSpaceBuff)) {
    // stuff
    once = false;
}
Lavenger Lavenger

2018/3/6

#
I don't quite understand what does this do (the "gold = g;" and "private Gold gold;")
Super_Hippo wrote...
//in Umaru class
private Gold gold;

public Umaru(Gold g)
{
    gold = g;
}
can you explain further so I may understand better?
Super_Hippo Super_Hippo

2018/3/6

#
private Gold gold;
Is a variable of type Gold with the name gold.
public Umaru(Gold g)
The constructor for the class Umaru with a parameter of type Gold.
gold = g;
Saves the passed Gold object (g) in the variable gold. So after this, your Umaru object has a reference to the Gold object.
Lavenger Lavenger

2018/3/6

#
oh so I could just use this
    private Gold Yen; 
    public /**Actor*/(Gold Y)
    {
        Yen = Y;
    }
to reference all of the gold generating actors to the gold object so they can update the gold?
Super_Hippo Super_Hippo

2018/3/6

#
If you have a lot of them, it may be better to have one reference in your world and update it from there.
Lavenger Lavenger

2018/3/6

#
Super_Hippo wrote...
If you have a lot of them, it may be better to have one reference in your world and update it from there.
as of now I have 8 characters to generate constant gold and 1 character that has 3 upgrades but it's a "tap to generate gold" type of gold generation
danpost danpost

2018/3/6

#
I think there is a bit of confusion here. I believe what is wanted (or needed) is one 'int' field to track the amount of gold accumulated (by any means) and one text display Actor object to show that amount. You could have the 'int' field and another field to reference the text display actor in your World subclass:
private int gold;
private Actor goldText;
Then you would add methods in the same class to (1) get (return) the amount of gold:
public int getAccumulatedGold()
{
    return gold;
}
and (2) to adjust the amount of gold:
public void adjustGold(int adjustment)
{
    gold += adjustment;
    goldText.setImage("Yen: "+gold, 30, Color.BLACK, new Color(0 0, 0, 0));
}
The constructor of the world will need to create the display actor, initialize it by calling 'adjustGold' with the initial amount of gold and add the actor into the world. You do not need a special Actor class called 'Gold' to display the Actor. A more basic class that is more versatile can be used. I usually call it 'SimpleActor' or just 'Aktor' and it looks like this:
public class Aktor extends greenfoot.Actor {}
The class can be used for all your GUI objects (value displays, timers, etc) and if you have a scrolling wold, an overriding empty 'setLocation' method can be added so these actors do not scroll with the world (they stay locked within the world window).
Lavenger Lavenger

2018/3/6

#
danpost wrote...
I think there is a bit of confusion here. I believe what is wanted (or needed) is one 'int' field to track the amount of gold accumulated (by any means) and one text display Actor object to show that amount. You could have the 'int' field and another field to reference the text display actor in your World subclass:
private int gold;
private Actor goldText;
Then you would add methods in the same class to (1) get (return) the amount of gold:
public int getAccumulatedGold()
{
    return gold;
}
and (2) to adjust the amount of gold:
public void adjustGold(int adjustment)
{
    gold += adjustment;
    goldText.setImage("Yen: "+gold, 30, Color.BLACK, new Color(0 0, 0, 0));
}
The constructor of the world will need to create the display actor, initialize it by calling 'adjustGold' with the initial amount of gold and add the actor into the world. You do not need a special Actor class called 'Gold' to display the Actor. A more basic class that is more versatile can be used. I usually call it 'SimpleActor' or just 'Aktor' and it looks like this:
public class Aktor extends greenfoot.Actor {}
The class can be used for all your GUI objects (value displays, timers, etc) and if you have a scrolling wold, an overriding empty 'setLocation' method can be added so these actors do not scroll with the world (they stay locked within the world window).
so how do I place all of these into my coding? can you show me of an example of how you would put them together? PS: sorry, I'm rather new to coding so the public and private fields and classes actually confuses me...
danpost danpost

2018/3/6

#
In general, without whatever else you might have:
import greenfoot.*;

public class MyWorld extends World
{
    // fields usually (by convention) go first
    private int gold;
    private Actor goldText = new Aktor();
    
    // class constructors usually (by convention) go next
    public MyWorld()
    {
        super(600, 400, 1);
        goldText.adjustGold(0); // to initialize image
        addObject(goldText, 100, 30); // wherever
    }
    
    // methods (by convention) go last
    public int getAccumulatedGold()
    {
        return gold;
    }
    
    public void adjustGold(int adjustment)
    {
        gold += adjustment;
        goldText.setImage(new GreenfootImage("Yen: "+gold, 30, Color.BLACK, new Color(0, 0, 0, 0)));
    }
}
Then any actor in the world can get the accumulated gold with:
MyWorld mw = (MyWorld)getWorld();
int accumulatedGold = mw.getAccumulatedGold();
and adjust it with:
MyWorld mw = (MyWorld)getWorld();
mw.adjustGold(1);
or just:
((MyWorld)getWorld()).adjustGold(1);
danpost danpost

2018/3/6

#
Of course, you will have an 'act' method added to the class and a timer field to facilitate time accumulation of gold:
// another field
private int goldTimer;

// the act method
public void act()
{
    accumulateGold();
}

// another method
private void accumulateGold()
{
    goldTimer = (goldTimer+1)%60;
    if (goldTimer == 0) adjustGold(1);
}
Lavenger Lavenger

2018/3/6

#
danpost wrote...
// class constructors usually (by convention) go next
what are constructors??
danpost danpost

2018/3/6

#
Lavenger wrote...
what are constructors??
They are blocks of code that are executed when an object is created by the class. You can recognize them by the absence of a return type and having the name of the class. There is at least one example of each (field, constructor and method) after each respective comment line above.
Lavenger Lavenger

2018/3/6

#
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);
        YenDisplay.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);
    }
}
and it seem to have an error on one part of the code:
public MyWorld()
    {
        super(1400, 675, 1);
        YenDisplay.adjustYen(0); // to initialize image
        addObject(YenDisplay, 100, 30); // wherever
    }  
this line in specific:
YenDisplay.adjustYen(0);
It says: "cannot find symbol- method adjustYen(int)"
CubeGamer6 CubeGamer6

2018/3/6

#
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)"
danpost danpost

2018/3/6

#
CubeGamer6 wrote...
The adjustYen method is within the World, not the YenDisplay. Just remove the "YenDisplay." from the "YenDisplay.adjustYen(0)"
My oversight -- my apologies.
There are more replies on the next page.
1
2
3