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

#
If the Button4 is not in a FarmWorld world when the code is executed, but in a BuyPlants world, then getWorld() can't be casted to a FarmWorld, it could be casted to a BuyPlants world. Is the counter not in the same world as the Button?
gacha321 gacha321

2019/6/6

#
Here's my problem.. My game must can buying and selling plants and animals. okay first we look at the plants first. from the FarmWorld if the farmer touch the Garden, the Board1(World) will show. this is the code of the Board1. In board1, It has 3 list, 1. Buy (List2.class) 2. Sell (List3.class) 3 Look (List4.class)
public class Board1 extends World
{
    private Counter theCounter;
    /**
     * Constructor for objects of class Board1.
     * 
     */
    public Board1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(595, 535, 1);
        addObject(new List2(), 295, 190);
        addObject(new List3(), 295, 310);
        addObject(new List4(), 295, 435);
        showText("====GARDEN===", 295, 90);
        showText("Buy Plants", 250, 190);
        showText("Sell Plants", 250, 310);
        showText("Look Plants", 250, 435);
        if(Greenfoot.mouseClicked(List2.class)){
            Change();
        }
        prepare();
    }
    
        public void Change(){
        Counter counter = ((FarmWorld)getWorld()).getCounter();
        buyPlants b1 = new buyPlants();
        b1.getCounter().walkCount(counter.getValue());
        Greenfoot.setWorld(b1);
    }
    
    public Counter getCounter(){
        return theCounter;
    } 
}
Well on that code, I have one error in this code
Counter counter = ((FarmWorld)getWorld()).getCounter();
It said can not find symbol - method getWorld(). But I code same like this code when the actor touching the goa.class in the FarmWorld
if(isTouching(Goa1.class)){
        getWorldOfType(GoldWorld.class).stopMusic();
        Counter counter = ((GoldWorld)getWorld()).getCounter();
        FarmWorld fw = new FarmWorld();
        fw.getCounter().walkCount(counter.getValue());
        Greenfoot.setWorld(fw);
    }
No error like before code in here.. Well after that, based on the method Change() before, I wan't to make a buy session so it would go to BuyPlantsWorld.. Here's the code for BuyPlantsWorld
public class BuyPlants extends World
{
    public buyPlants()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(595, 535, 1);
        theCounter = new Counter();
        addObject(theCounter, 50, 20);
        showText("====BUY PLANTS====", 300, 70);
        showText("X 60 GOLD", 290, 162);
        showText("X 30 GOLD", 290, 287);
        showText("X 20 GOLD", 290, 408);
        showText("Buy", 450,162);
        showText("Buy", 450,282);
        showText("Buy", 450,400);
        drawMap();
        prepare();
    }
       
    public Counter getCounter(){
        return theCounter;
    } 
Well, in that world, I give 3 buy buttons because it has 3 plants (grape, guava and manggo). The First is button4 to buy grape.. Here's the code of button 4
public class Button4 extends Button
{
    /**
     * Act - do whatever the Button4 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   
    
    public void act() 
    {
        // Add your action code here.
        if(Greenfoot.mouseClicked(this)){
            Counter counter = ((BuyPlants)getWorld()).getCounter();
            if (counter.getValue() >= 100)
            {
                counter.walkCount(-100);
            }
        }
    }    
    
    
    public static void name (String[] args) throws IOException {
         String Name;
         InputStreamReader isr = new InputStreamReader(System.in);
         BufferedReader br = new BufferedReader(isr);
         System.out.print("Input yoour Grape's plant name: ");
         Name = br.readLine();
         System.out.println("Your Plants Name : " + Name);
    }
}
my program must can give plants or animals a name :(( but I don't know how to make that. first, I have an error in the first code so, I don't resume my code because I have tried any time and I can't fix the error. Can anyone help meee :(( I'm sorry it's too confuse. but I'm a newbiie using greenfoot and this is my Final Exam... I'm so sorry :((
gacha321 gacha321

2019/6/6

#
@Super_Hippo, yes the count is on the FarmWorld and GoldWorld, first world is on the FarmWorld. so When we go to GoldWorld, the counter is casted from the FarmWorld. also if we want back to the FarmWorld, we cast the counter in goldworld. but I think It can cast the counter from FarmWorld to the Board1 and BuyPlants World because I think it just same like from FarmWorld to the GoldWorld before :(
gacha321 gacha321

2019/6/6

#
when I tried to change to
Counter counter = ((Board1)getWorld()).getCounter();
and
Counter counter = ((BuyPlants)getWorld()).getCounter();
it show a same error like before.. can not find symbol - method getWorld()..
Super_Hippo Super_Hippo

2019/6/6

#
getWorld is a method in the Actor class. It returns the world in which the actor currently is (or null if it isn't in any world). The method is not "get a reference to the world object of class X that I use or will use somewhere". A world is not in a world and can't use the method. Another thing: In your constructor of the Board1 class, you ask if a List is clicked on which was added a few lines ago. That is never happening. Move that check into the act method. (Create an act method there.)
gacha321 gacha321

2019/6/6

#
omg how fool I am :(( I'm so sorry.. I just know that from youu :((
public class Board1 extends World
{
    private Counter theCounter;
    /**
     * Constructor for objects of class Board1.
     * 
     */
    public Board1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(595, 535, 1);
        addObject(new List2(), 295, 190);
        addObject(new List3(), 295, 310);
        addObject(new List4(), 295, 435);
        showText("====GARDEN===", 295, 90);
        showText("Buy", 250, 190);
        showText("Sell", 250, 310);
        showText("Look", 250, 435);
        prepare();
    }
    
    public void act(){
        if(Greenfoot.mouseClicked(List2.class)){
            Change();
        }
    }
     
    public void Change(){
        Counter counter = ((FarmWorld)getWorld()).getCounter();
        BuyPlants b1 = new BuyPlants();
        b1.getCounter().walkCount(counter.getValue());
        Greenfoot.setWorld(b1);
    }
 
    
    public Counter getCounter(){
        return theCounter;
    } 
    
well, my purpose doing that is to make a display of golds that have been claimed before.. so if we want to buy an item we can see our gold and buy an item and we can use the condition from that to reduce the golds that we have :(( do you have a solution for this ..?:(( I'm so sorry to make you so confuse T_T
gacha321 gacha321

2019/6/6

#
But the problems is when we buy the item, we must give a name to that item and must save to file .txt and it makes me so frustate T__T
Super_Hippo Super_Hippo

2019/6/6

#
I am not sure, but maybe you just have to add the Counter into this Board1 class and then pass its value to the BuyPlants world?
addObject(theCounter = new Counter(), <x>, <y>);
public void changeWorld()
{
    BuyPlants b1 = new BuyPlants();
    b1.getCounter().walkCount(theCounter.getValue());
    Greenfoot.setWorld(b1);
}
I can't help you with the part of writing something to (or reading from) a file. I have no experience with that.
gacha321 gacha321

2019/6/6

#
When I try your suggestion, the count is displayed on board1 but the gold value is 100 where's the 1st gold set from starting game(because we call 'theCounter'). It's ok, I'm so thankfull you reply what I wrote in here and I really appreciate it. you make me know about new lesson of using greenfoot. Thank you so mucchh :')) you are so kind
Super_Hippo Super_Hippo

2019/6/6

#
Do you pass the value of the counter of the last world to the Board1 similar to how you try to pass it from Board1 to BuyPlants? How many worlds do you have? Maybe it would be easier to store the value at one point instead of passing it 100 times.
gacha321 gacha321

2019/6/6

#
It's different .. because we must earn gold by make the actor (farmer) walking si I used walkcount here the code when the farmer went to goldworld
if(isTouching(Goa1.class)){
        getWorldOfType(GoldWorld.class).stopMusic();
        Counter counter = ((GoldWorld)getWorld()).getCounter();
        FarmWorld fw = new FarmWorld();
        fw.getCounter().walkCount(counter.getValue());
        Greenfoot.setWorld(fw);
    }

Based on that code, the counter value will resume from the farmworld before. I used the same method to resume the value when changing world again from goldworld to farmworld. But I don't know how can we resume the value but didn't use the actor. But when we want to buy item, the actor must touch the garden first. But if we touch the garden, it will show the board1 that has 3 list like I said before. And from that list we choose the buy list and then it will change World to buyPlants... Total world is about 6 World that I used.. (FarmWorld, GoldWorld, Board1(Garden), Board2(Farm), buyPlants, buyAnimals, and maybe it will increase 4 World for seeling and seeing list of our item :"(()
Super_Hippo Super_Hippo

2019/6/6

#
You don't have to add the Counter to the world. Only create it in the world without adding it and you can still pass the value to this counter. Then you should be able to do exactly the same as before, only without displaying the counter. (Even though I don't know why you don't want to display it there.)
gacha321 gacha321

2019/6/6

#
It's better to don't display the counter?? I only want to display the current gold that we earned so if we want to buy something it would be easy :). But, how can we use the if statement for reducing gold from the counter? It's right to code like the code I before but dont's use the counter displaay?
Super_Hippo Super_Hippo

2019/6/6

#
Wait… in your Board1 world, you don't add a Counter. I wondered why there isn't a Counter. And now you tell me that there is a Counter in the world? I guess I am lost.
gacha321 gacha321

2019/6/6

#
I tried to add a counter in Board1 but it will show an error because like you said before the getWorld is only can use in an Actor class so I don't know what to do then :(
There are more replies on the next page.
1
2
3