Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Trouble working with ArrayList  (Read 325 times)
0 Members and 1 Guest are viewing this topic.
Offline xsi3rr4x

Junior Devvie





« Posted 2014-05-01 21:04:31 »

Hey guys, I'm currently working on adding items to an inventory (which is an arraylist) and making them display when I go to bring up my inventory, heres the code that I have for it so far.

This is in my "InventoryState" class
1  
2  
3  
4  
5  
for(ItemEntity p: Player.playerInventory){
     if(Player.playerInventory.get(0) != null)                //my problem is in this for statement somehow
     g.drawImage(p.getSprite(),50,50,this);
     if(Player.playerInventory.get(1) != null)               //if i take out this "if statment" than I'm able to display the 1st item fine.
        g.drawImage(p.getSprite(),100,100,this);}


This is in my "Player" class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public static ArrayList<ItemEntity> playerInventory = new ArrayList <ItemEntity>(16);

if(GameLoop.keysPressed[69]==true){     // picks up an item and adds it to the player inventory
        for(Entity p : GamePlayState.entities){
             if (p instanceof ItemEntity){
                if(xCord-p.getX()<15&&yCord-p.getY()<15&&xCord-p.getX()>-15&&yCord-p.getY()>-15){
                   playerInventory.add((ItemEntity) p);
                   p.Hit(p,Entity.HitEnum.pickup,0);}}}}
    if(GameLoop.keysDown[73] == true)//opens inventory state
         StateStack.pushState("InventoryState");


Ok so, if I go in my game and pick up 2 or more items with the code I have above and then switch to my inventory state than everything will display as expected, but if I were to pick up just 1 item and switch to my inventory than I get an ArrayOutOfBounds Exception.
If I were to remove the 2nd "if statement" so it would just be.
1  
2  
3  
for(ItemEntity p: Player.playerInventory){
     if(Player.playerInventory.get(0) != null)        
     g.drawImage(p.getSprite(),50,50,this);}

Than the 1 item displays properly in the inventory without an exception. I really feel like its somthing very simple thats going on here, but after messing with it for awhile I cant seem to pinpoint how to work with this. Any help would be greatly appreciated (:
Offline ags1

JGO Wizard


Medals: 75
Projects: 3
Exp: 5 years


Make code not war!


« Reply #1 - Posted 2014-05-01 21:15:31 »

If the list is 1 item long then get(1) will give an out of bounds exception, not null.

You want to do a loop like this, it will work for any size of list:

for (InventoryItem item : player.playerInventory) {
   ...
}


Your variables look wrong - I assume Player is an instance not a  class, so it should be named "player".

As sirkarfpen says below, why do you loop over the items twice? I misread your code, I thought the outer loop was iterating over the players, and the inner loop over the inventories, which would make a bit more sense.

Offline sirkarpfen

Junior Devvie


Medals: 2
Exp: 2 years



« Reply #2 - Posted 2014-05-01 21:16:06 »

1  
2  
3  
4  
5  
for(ItemEntity p: Player.playerInventory){
     if(Player.playerInventory.get(0) != null)                //my problem is in this for statement somehow
     g.drawImage(p.getSprite(),50,50,this);
     if(Player.playerInventory.get(1) != null)               //if i take out this "if statment" than I'm able to display the 1st item fine.
        g.drawImage(p.getSprite(),100,100,this);}


Why would you check the playerInventory with get(), when your allready getting each ItemEntity with:
for(ItemEntity p : Player.playerInventory)
That would be my solution to looping through your inventory:

1  
2  
3  
4  
5  
6  
7  
8  
9  
int pX = 50;
int pY = 50;
for(ItemEntity p : Player.playerInventory()) {
    if(p != null) {
        g.drawImage(p.getSprite(),pX, pY, this);
        pX+=50;
        pY+=50;
    }
}


Your variables look wrong - I assume Player is an instance not a  class, so it should be named "player".

Or maybe Player is a class and he is accessing it in a static way^^.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ags1

JGO Wizard


Medals: 75
Projects: 3
Exp: 5 years


Make code not war!


« Reply #3 - Posted 2014-05-01 21:21:06 »

A static inventory on a Player - that makes no sense to me. I suppose it might be possible in a single player game where the player code is totally different to every other entity's code...

Offline xsi3rr4x

Junior Devvie





« Reply #4 - Posted 2014-05-01 21:29:22 »

Ah, I didnt mean for the inventory to be static, lol that wouldv definitely given me some problems later on, but anyways now I'm not sure how I'm going to render the sprites in playerInventory, looks like i'll need to figure out that first x.x, thanks for pointing that out to me though!
Offline ags1

JGO Wizard


Medals: 75
Projects: 3
Exp: 5 years


Make code not war!


« Reply #5 - Posted 2014-05-01 21:31:34 »

Just loop over the inventory, and render anything that's not null. See sirkarfpen's snippet.

Even better, write the code so you don't have any nulls in your inventory. Having nulls in the inventory could lead to conversations like this:

Bilbo: "what have I got in my pocket?"

Gollum: "I give up."

Bilbo: "A handkerchief, a nothing, a ring, two more nothings, and a halfpenny."

Offline xsi3rr4x

Junior Devvie





« Reply #6 - Posted 2014-05-01 21:33:04 »

yeah, I suppose I'm going to make a method in my player instance that handles the inventory so that I can use that code for it, thanks so much guys (:
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!