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 › Object Oriented Programming With an Image
Page Index Toggle Pages: 1
Object Oriented Programming With an Image? (Read 543 times)
Object Oriented Programming With an Image?
May 28th, 2007, 12:22am
 
Hi all,
I'd like to have a class of 10 objects, all of which are an image (.gif) that will move across the screen.

My question is, provided this will work, how can I access the x and y position of the images (so I can change the position) in the class?  I've had experience of classes with ellipses and changing the position, but I'm not sure if Processing will directly sync up with the coordinates within PImage, or if I would have to do something extra.

Thanks!

-Will
Re: Object Oriented Programming With an Image?
Reply #1 - May 28th, 2007, 12:04pm
 
Here's a basic framework to start with:

Code:
class myImage
{
int x, y;
PImage img;
myImage(int _x, int _y, PImage _img)
{
x=_x;
y=_y;
img=_img;
}
void draw()
{
image(img,x,y);
}
//other functions e.g. move, rotate or anything go here..
}

myImage[] images;

void setup()
{
size(300,300);
images=new myImage[10];
images[0]=loadImage("1.gif");
images[1]=loadImage("2.gif");
//etc, etc...
}

void draw()
{
background(0);
if(somecondition)
{
images[1].move(-1,0); // or whatever..
}

for(int i=0;i<images.length;i++)
{
images[i].draw();
}
}


As you can see there is no "syncing up" between things, just changing a value doesn't cause things to be redrawn in the new position magically, you have to actually say "and now draw yourself wherever you should be"
Page Index Toggle Pages: 1