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

2019/6/4

Buying and Selling an Item

1
2
3
Super_Hippo Super_Hippo

2019/6/6

#
Do you ever use "getWorld" when creating a Counter and adding it to the world?
gacha321 gacha321

2019/6/6

#
I think no, because the getWorld is for cast the recent gold that we earn to another world and resume it. But I use Counter counter = new Counter() for make a new counter :(
gacha321 gacha321

2019/6/6

#
Well what I want to do first is make a gold display that we have been earned on board1 because if we want to buy something as usual, we can look our gold in the board right? But like you said before it can't because World class can't use getWorld.. so if it can't, in the buy session, it will exactly reduce our gold .. so how can we reduce our gold if we can't cast the counter from world to world :(
Super_Hippo Super_Hippo

2019/6/6

#
Yes, so couldn't you do the same thing there? By the way, "Cast" is not the correct word here. You "pass" the value of the counter to the counter of the new world. Casting is done here for example:
Counter counter = ((FarmWorld)getWorld()).getCounter();
Or just:
(FarmWorld) getWorld()
getWorld() returns a World object and (FarmWorld) casts this World object to a FarmWorld object so you can for example use methods which are located in the FarmWorld class.
gacha321 gacha321

2019/6/6

#
yeah, but I used
Counter counter = ((FarmWorld)getWorld()).getCounter();
for display the gold that we earned :(
gacha321 gacha321

2019/6/6

#
when I tried code like this
    public void act(){
        if(Greenfoot.mouseClicked(List2.class)){
            Change();
        }
    }
     
    public void Change(){
        Counter counter = new Counter();
        BuyPlants b1 = new BuyPlants();
        b1.getCounter().walkCount(counter.getValue());
        Greenfoot.setWorld(b1);
    }
it's just display 100 gold from the counter.class, BTW, why when I clicked the List2.class, it doesn't change to BuyPlants World :((
public class Counter extends Actor
{
    private int count = 0;
    
    public Counter(){
        setImage(new GreenfootImage("Gold : 100", 20, Color.WHITE, Color.BLACK));  
    }
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void walkCount (int amount) 
    {
        // Add your action code here.
        count += amount;
        setImage(new GreenfootImage("Gold : " + (100+count),20, Color.WHITE, Color.BLACK)); 
    }    
    
    public int getValue(){
        return count;
    }
}
Super_Hippo Super_Hippo

2019/6/6

#
That line doesn't display anything. It only gets a reference to the counter. The Counter probably (and hopefully) displays the value on its own.
Super_Hippo Super_Hippo

2019/6/6

#
You can't click a class. So the condition will never be met. You can only click on Actors in the world. So the parameter is either "this" when the code would be in the List2.class, or you need a reference to the List2 object.
//outside methods
private List2 list2 = new List2();

//…

addObject(list2, 295, 190);

//…

if (Greenfoot.mousePressed(list2))
{
    Change();
}
gacha321 gacha321

2019/6/6

#
Yes, so couldn't you do the same thing there? so, what do you mean about this :(
Super_Hippo Super_Hippo

2019/6/6

#
I mean it looks like you pass the counter's value from one world to another plenty of times but now you can't.
public void Change()
{
    BuyPlants b1 = new BuyPlants();
    b1.getCounter().walkCount(theCounter.getValue());
    Greenfoot.setWorld(b1);
}
This will work if you passed the value to theCounter as well. (Which you apparently didn't. Unless you create the counter in the prepare method.)
gacha321 gacha321

2019/6/7

#
I'm so sorry :( because I'm very newbie about this.. how many times I tried and already reached this, I think what I did is right :( that's why I make a discussion, afraid if there's something wrong :(( so.. can I reduce the gold eventhough we ignore the count display? I think the important point in here is reducing our gold when we buy semething...
gacha321 gacha321

2019/6/7

#
Do you know? T^T I've fix the problem!!! omggg T^T
gacha321 gacha321

2019/6/7

#
But.. I have any problems again ...
You need to login to post a reply.
1
2
3