We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Array List Problem
Page Index Toggle Pages: 1
Array List Problem (Read 521 times)
Array List Problem
May 25th, 2009, 12:07am
 
Hi there.

So I'm building a sprite class and I'm having an issue. I'm using ArrayLists and I'm having problems when it comes to retrieving objects from the list.

When I create a sprite, it adds itself to an arraylist called Sprites. So using a for loop, I can go through and update/move/etc anything in the Sprites arraylist.

Now here's the problem.

...

Drawing your attention to the output console in the bottom right of the screen. I have print(blit) and print(guy).

Blit, as you may notice, is me trying to retrieve the object from the ArrayList.

Guy, is the name of the object sprite itself.

Both print statements return the exact same value (I believe this is a memory address? Maybe?).

However guy.display() works just fine while blit.display() throws an error.

...

"The fuction display() does not exist".

Here's the whole sorrid, unoptimized code:

Code:
bSprite guy;
bSprite moon;
ArrayList Sprites;
class bSprite {
PImage b;
float _x;
float _y;
float _yspeed;
float _xspeed;
float _width;
float _height;

bSprite (String img, float tempXpos, float tempYpos, float tempXspeed, float tempYspeed){

b = loadImage(img);
_x = tempXpos;
_y = tempYpos;
image(b,_x,_y);
_xspeed = tempXspeed;
_yspeed = tempYspeed;
_width = b.width;
_height = b.height;
Sprites.add(this);

}
void move(){
_x = _x+_xspeed;
_y = _y+_yspeed;
}
void display(){
image(b,_x,_y,_width,_height);
}

boolean isHit(Object target){
return false;
}

}

void setup(){
size(500,500);
Sprites = new ArrayList();
guy = new bSprite("buster.jpg", 10, 10, 1 ,1);
moon = new bSprite("moon.png", 40,40,0,0);

Sprites.add(guy);
}
void draw(){
background(255);

if(guy._x>width){
guy._x=0-guy._width;

}
else if (guy._y>height){
guy._y=0-guy._height;
}

else{
guy.move();
}



for (int i = Sprites.size()-1; i>=0; i--){
Object blit = Sprites.get(i);
print(blit);
print(guy);
guy.display();
blit.display();
}
}





Any help would be really, really appreciated.

Thanks!
Re: Array List Problem
Reply #1 - May 25th, 2009, 1:29am
 
The only thing you should need to do is change this line:

Code:
Object blit = Sprites.get(i); 



ArrayList.get() returns an instance of Object, which is the base class all classes are derived from and hence can be used as a general reference to any class. But to use the objects from the ArrayList you need to cast them back to the class you know they should have, in your case bSprite:

Code:
bSprite blit=(bSprite)Sprites.get(i); 

Re: Array List Problem
Reply #2 - May 25th, 2009, 1:37am
 
Blaine wrote on May 25th, 2009, 12:07am:
I believe this is a memory address

Almost. More like a reference, actually.

watz gave the right solution. You get an Object from the list, which is like saying you got a human from humanity: it won't tell you the color of skin, the size, the sex, the age, etc. That's more a blueprint than a finished "product". You can be sure the human has a head, but you won't know if he can program, play a musical instrument or even read.
By applying the cast (assuming it is right, of course), you typify the object, giving it attributes and skills.
Re: Array List Problem
Reply #3 - May 25th, 2009, 7:28am
 
Thanks guys!
Page Index Toggle Pages: 1