Trodden
YaBB Newbies
Offline
Posts: 8
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(); } }