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 › Images with Classes/Objects
Page Index Toggle Pages: 1
Images with Classes/Objects (Read 315 times)
Images with Classes/Objects
Feb 10th, 2008, 12:05pm
 
Hello,

I have created a program which creates a class/object which I can then duplicate and create as many instances of as I want. However, this class involves displaying one image when the mouse is left clicked, and replacing it with another when it is right clicked. Although the object is being called as completely separate, self-contained instances, it is affecting every instance that is on screen in the program, ie they are all linked together so when one is left clicked and its image changes, they all change.

The code is as follows, your urgent help is very much appreciated!


Paratrooper j1;
Paratrooper j2;

PImage bg;
PImage paratrooper;
PImage paratrooperFalling;
PImage parachute;
PImage parachuteBroken;

float shakeX;
float shakeY;
float easing = .5;

void setup() {
 size(600,400);
 background(0);
 bg = loadImage("bg.jpg");
 paratrooper = loadImage("paratrooper.png");
 parachute = loadImage("parachute.png");
 j1 = new Paratrooper(209,-207,1);
 j2 = new Paratrooper(409,-207,1);
 stroke(random(255),random(255),random(255));
 strokeWeight(5);
}

void draw() {
 image (bg,0,0,600,400);
 smooth();
 j1.update(mouseX,mouseY);
 j1.display(mouseX,mouseY);
 j2.update(mouseX,mouseY);
 j2.display(mouseX,mouseY);
}

void mouseMoved() {
 j1.processMouseMoved(mouseX,mouseY);
 j2.processMouseMoved(mouseX,mouseY);
}

void mouseDragged() {
 j1.processMouseDragged(mouseX,mouseY);
 j2.processMouseDragged(mouseX,mouseY);
}

void mousePressed() {
 j1.processMousePressed(mouseX,mouseY);
 j2.processMousePressed(mouseX,mouseY);
}

// End Setup



// Begin Object

class Paratrooper {
 int x, y;
 int speed;
 boolean mouseIsOverJumper;

 Paratrooper(int _x, int _y, int _speed) {
   x = _x;
   y = _y;
   speed = _speed;
   mouseIsOverJumper = false;
 }

 void processMouseMoved(int mx, int my) {
   if (dist(mx,my,x,y) < 170) {
     mouseIsOverJumper = true;
     y = y - 1;
     x = x + 1;
     if (x>width) {
       y = -507;
       x = 50;
       parachute = loadImage("parachute.png");
     }
   } else {
     mouseIsOverJumper = false;
   }
 }

 void processMouseDragged(int mx, int my) {
   if (mouseIsOverJumper) {
     shakeX = random(-15,15) * easing;
     shakeY = random(-15,15) * easing;
   }
 }
 
 void processMousePressed(int mx, int my) {
   if (mouseIsOverJumper && (mouseButton == RIGHT)) {
     paratrooper = loadImage("paratrooper_falling.png");
     speed = 4;
   } else if (mouseIsOverJumper && (mouseButton == LEFT)) {
     parachute = loadImage("parachute_broken.png");
 }
 }

 void update(int mx, int my) {
   y=y+speed;
   if (y>height) {
     y=-207;
     paratrooper = loadImage("paratrooper.png");
     parachute = loadImage("parachute.png");
     speed = 1;
   }
}

 void display(int mx, int my) {
 image(parachute,x+shakeX,y+shakeY,140,186);
 image(paratrooper,x+59+shakeX,y+168+shakeY,39,50);
 noFill();
 beginShape();
 vertex(x+137+shakeX,y+70+shakeY);
 bezierVertex(x+30+shakeX,y+60+shakeY,x+30+shakeX,y+62+shakeY,x+3+shakeX,y+72+sha
keY);
 endShape();
 }

}
Re: Images with Classes/Objects
Reply #1 - Feb 10th, 2008, 12:24pm
 
It's breaking because you're changing what' in the GLOBAL PImages. You've not told processing in any way shape or form that each paratrooper should have it's own set of images.

Also, you can make it all run a hell of a lot quicker by just loading all the images once and only once, and having a couple of variables in the paratrooper class that say which ones should be used for each instance individually.

e.g.
Code:

PImage paratrooper;
PImage falling_paratrooper;
PImage parachute;
PImage broken_parachute;

class Paratrooper
{
boolean isFalling;
boolean isBroken;
x = _x;
y = _y;
speed = _speed;
mouseIsOverJumper = false;

void processMouseMoved(int mx, int my) {
if (dist(mx,my,x,y) < 170) {
mouseIsOverJumper = true;
y = y - 1;
x = x + 1;
if (x>width) {
y = -507;
x = 50;
}
} else {
mouseIsOverJumper = false;
}
}

void processMouseDragged(int mx, int my) {
if (mouseIsOverJumper) {
shakeX = random(-15,15) * easing;
shakeY = random(-15,15) * easing;
}
}

void processMousePressed(int mx, int my) {
if (mouseIsOverJumper && (mouseButton == RIGHT)) {
isFalling=true;
speed = 4;
} else if (mouseIsOverJumper && (mouseButton == LEFT)) {
isBroken=true;
}
}

void update(int mx, int my) {
y=y+speed;
if (y>height) {
y=-207;
isBroken=false;
isFalling=true;
speed = 1;
}
}
void display(int mx, int my) {
if(!isBroken)
image(parachute,x+shakeX,y+shakeY,140,186);
else
image(broken_parachute,x+shakeX,y+shakeY,140,186);
if(!isFalling)
image(paratrooper,x+59+shakeX,y+168+shakeY,39,50);
else
image(falling_paratroper,x+59+shakeX,y+168+shakeY,39,50);
noFill();
beginShape();
vertex(x+137+shakeX,y+70+shakeY);
bezierVertex(x+30+shakeX,y+60+shakeY,x+30+shakeX,y+62+shakeY,x+3+shakeX, y+72+shakeY);
endShape();
}
}

//same setup/draw/mouseXxxx as before but load all 4 images into the right PImages in setup.
Re: Images with Classes/Objects
Reply #2 - Feb 10th, 2008, 8:46pm
 
brilliant, thanks so much!
Page Index Toggle Pages: 1