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 & HelpPrograms › Create a class with moving images
Page Index Toggle Pages: 1
Create a class with moving images (Read 437 times)
Create a class with moving images
Feb 14th, 2009, 11:35pm
 
I am trying to make a rendition of this code that has 2 moving .png files as opposed to the drawn rectangles. Can someone please edit this code to show me how I can accomplish this? I've been unsuccessful so far.

// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 8-2: Two Car objects

Car myCar1;
Car myCar2; // Two objects!

void setup() {
 size(200,200);
 myCar1 = new Car(color(255,0,0),0,100,2); // Parameters go inside the parentheses when the object is constructed.
 myCar2 = new Car(color(0,0,255),0,10,1);
}

void draw() {
 background(255);
 myCar1.move();
 myCar1.display();
 myCar2.move();
 myCar2.display();
}

class Car { // Even though there are multiple objects, we still only need one class. No matter how many cookies we make, only one cookie cutter is needed.Isn’t object-oriented programming swell?
 color c;
 float xpos;
 float ypos;
 float xspeed;

 Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { // The Constructor is defined with arguments.
   c = tempC;
   xpos = tempXpos;
   ypos = tempYpos;
   xspeed = tempXspeed;
 }

 void display() {
   stroke(0);
   fill(c);
   rectMode(CENTER);
   rect(xpos,ypos,20,10);
 }

 void move() {
   xpos = xpos + xspeed;
   if (xpos > width) {
     xpos = 0;
   }
 }
}
Re: Create a class with moving images
Reply #1 - Feb 15th, 2009, 6:04am
 
Code:

...
myCar1 = new Car("file.png",color(255,0,0),0,100,2); //added filename
myCar2 = new Car("file2.png"...

...
class Car {
PImage todraw;
...

Car(String fname, color tempC, float tempXpos, float tempYpos, float tempXspeed) {
todraw = loadImage(fname);
...
}
...

void display() {
//stroke(0);
//fill(c);
//rectMode(CENTER);
//rect(xpos,ypos,20,10);
image(todraw,xpos,ypos,20,10);//Or use todraw.width, todraw.height
}
...
}
Re: Create a class with moving images
Reply #2 - Feb 15th, 2009, 7:13pm
 
Thank you very much Noah Smiley
Page Index Toggle Pages: 1