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

2020/1/21

Inventory

So in my game you can pick up items such as a pickle, and you can trade that with a character for a key, Problem is I don't know how to keep the inventory when going to different worlds also I can't seem to find out how I'm able to trade these items... please help.....
Archerfish Archerfish

2020/1/22

#
Hey Helpard_With_A_Stick! There are a couple of options when it comes to retaining information between different worlds. Here are two ways: 1.) Passing via constructors 2.) Global variables PASSING VIA CONSTRUCTORS You can choose to pass the info to the World constructor when calling it: Calling constructor:
Greenfoot.setWorld(new World1(numberOfPickles));
Where "World1" is the name of the world you are going to, and "numberOfPickles" is the number of pickles in your inventory. Constructor in the World Class "World1":
int numberOfPickles;
public World1(int numberOfPickles)
{
  this.numberOfPickles = numberOfPickles;
}
So that the constructor of "World1" receives and stores the number of pickles. GLOBAL VARIABLES Using this method, there is no need to pass the value between worlds. The value can be accessed by any class. To declare a global variable, simple do the following (typically, this is done in a world class):
public static int numberOfPickles;
To access "numberOfPickles" from another class, do the following:
World1.numberOfPickles
To change the value of "numberOfPickles" from another class, do the following:
World1.numberOfPickles = 5;
hey, thanks for replying. I tried to use the Global Variables but I can't seem to get it to work, I keep getting a "Not a statement" error... I'm not sure what I'm missing or doing wrong.... I'll post my code in both the Pickle class world.
import greenfoot.*;
public class Pickle extends Items
{
    public void act() 
    {
        boolean mc = false;
        boolean pickle = false;
        Bronze_Room.numberOfPickles;
        if(Greenfoot.mouseClicked(this) && mc ==  false)
        {
            pickel = true;
            World world;
            world = getWorld();
            getWorld().addObject(new Pickle(),89,737);
            getWorld().removeObject(this);
        }
    }    
}
import greenfoot.*; 
public class Bronze_Room extends World
{
    public Bronze_Room()
    {    
        super(800, 800, 1); 
        Inventory inventory = new Inventory();
        addObject(inventory,89,737);
        Inventory inventory2 = new Inventory();
        addObject(inventory2,136,744);
        inventory2.setLocation(129,737);
        Inventory inventory3 = new Inventory();
        addObject(inventory3,171,742);
        inventory3.setLocation(168,737);
        Inventory inventory4 = new Inventory();
        addObject(inventory4,209,744);
        inventory4.setLocation(207,737);
        Inventory inventory5 = new Inventory();
        addObject(inventory5,254,744);
        inventory5.setLocation(242,737);
        Inventory inventory6 = new Inventory();
        addObject(inventory6,281,744);
        inventory6.setLocation(281,737);
        Inventory inventory7 = new Inventory();
        addObject(inventory7,307,739);
        inventory7.setLocation(315,737);
        prepare();
    }
    public static int numberOfPickels;
    private void prepare()
    {
        Slightly_Lighter_Room_Backdoor slightly_lighter_room_backdoor = new Slightly_Lighter_Room_Backdoor();
        addObject(slightly_lighter_room_backdoor,761,718);
        John john = new John();
        addObject(john,189,522);
        Trap_Door trap_Door = new Trap_Door();
        addObject(trap_Door,625,585);
        trap_Door.setLocation(15,658);
        trap_Door.setLocation(46,677);
        trap_Door.setLocation(46,677);
        Bob bob = new Bob();
        addObject(bob,550,490);
    }
}
danpost danpost

2020/1/22

#
Helpard_With_A_Stick? wrote...
hey, thanks for replying. I tried to use the Global Variables but I can't seem to get it to work, I keep getting a "Not a statement" error... I'm not sure what I'm missing or doing wrong.... I'll post my code in both the Pickle class world.
Line 8 in the Pickle class does not look right.
Yeah, I'm trying to create a global like variable (because java doesn't have one) But I don't know how to turn a static act like a global...
danpost danpost

2020/1/23

#
Helpard_With_A_Stick? wrote...
Yeah, I'm trying to create a global like variable (because java doesn't have one) But I don't know how to turn a static act like a global...
Instead of having inventory static, you can keep inventory in your player classes and the world can keep the players static. When other worlds add the saved player, the inventory will remain with it.
I don't have a class for my player.... you are the player, its like an escape room game.
danpost danpost

2020/1/24

#
Helpard_With_A_Stick? wrote...
I don't have a class for my player.... you are the player, its like an escape room game.
What is the name of your initial world and is it a game world or a pre-game world?
You need to login to post a reply.