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!