We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there, I'm trying to create a wee animation that uses the class Alien to display multiple aliens. It took me a few hours to make it resizable and bounce off the screen edges. But now that I tried to add another alien to the screen it overwrites the one I created first (so I still only have one bouncing around). I've only started using Processing about 2 weeks ago, so I'm probably making a stupid mistake. Would really appreciate if you guys could have a look on my code. I'm using Processing 2.1.1 on a Windows pc. So here's the first part:
Alien myAlien;
Alien anotherAlien;
void setup()
{
size(displayWidth, displayHeight-80);
frameRate(30);
ellipseMode(CORNER);
myAlien = new Alien(150, 150, 300);
anotherAlien = new Alien(10, 10, 250);
}
void draw()
{
background(84,179,232);
myAlien.display();
anotherAlien.display();
}
And the Alien class:
//Alien class
class Alien {
float wholeX; //starting X position
float wholeY; //starting Y position
float rad; //total size of the alien
float speedX = random(1, 2); // Up and down speed
float speedY = random(1, 2); // Left and right speed
int directionX = 1; //to the left or right
int directionY = 1; //Up or down
//tempRad sets the size of the object while headX and headY set it's starting position
Alien (float tempHeadX, float tempHeadY, float tempRad){
wholeX = tempHeadX;
wholeY = tempHeadY;
rad = tempRad;
}
void display(){
float leftLegX = wholeX+rad/2-rad/6;
float leftLegY = wholeY+rad/5;
float legRad1 = rad/12;
float legRad2 = rad*0.78;
float rightLegX = wholeX+rad/2+rad/12;
float rightLegY = wholeY+rad/5;
float leftHandX = wholeX;
float leftHandY = wholeY+rad/2-rad/12;
float leftArmX = wholeX+rad/12;
float leftArmY = wholeY+rad/2-rad/12;
float handRad = rad/6;
float rightHandX = wholeX+rad-rad/6;
float rightHandY = wholeY+rad/2-rad/12;
float rightArmX = wholeX+rad-rad*2/5;
float rightArmY = wholeY+rad/2-rad/12;
float bodyX = wholeX+rad/2-rad/8;
float bodyY = wholeY+rad/5;
float bodyRad = rad/2;
float headX =wholeX+rad/2-rad/8;
float headY = wholeY;
float headRad = rad/4;
float eyeX = wholeX+rad/2-rad/28;
float eyeY = wholeY+rad/14;
float eyeRad1 = rad/12;
float eyeRad2 = rad/14;
//background reset
background(86, 255, 53);
// Update the position of the shape
wholeX = wholeX + ( speedX * directionX );
wholeY = wholeY + ( speedY * directionY );
//if the object hits either right or left end of screen bounce off
if (wholeX > width-rad || wholeX < 0) {
directionX *= -1;
}
//if the object hits either top or bottom of the screen bounce off
if (wholeY > height-rad || wholeY < 0) {
directionY *= -1;
}
//draw the alien
noFill();
noStroke();
//outter boundry of the alien
ellipse(wholeX, wholeY, rad, rad);
fill(53, 236, 255);
//legs
ellipse(leftLegX, leftLegY, legRad1, legRad2);
ellipse(rightLegX, rightLegY, legRad1, legRad2);
//arms
ellipse(leftHandX, leftHandY, handRad, handRad);
rect(leftArmX, leftArmY, handRad*1.8, handRad);
ellipse(rightHandX, rightHandY, handRad, handRad);
rect(rightArmX, rightArmY, handRad*1.8, handRad);
//body
fill(255, 48, 124);
ellipse(bodyX, bodyY, bodyRad/2, bodyRad);
//head
fill(53, 236, 255);
ellipse(headX, headY, headRad, headRad);
//eye
fill(random(0, 255));
ellipse(eyeX, eyeY, eyeRad1, eyeRad2);
}
}
Answers
The only problem is that you redraw the background on line 54 (inside each
Alien
'sdisplay()
function). If you redraw the background for each instance, then all of the previously drawn instances will be erased. Removing this line should be sufficient.Thank you so much, worked perfectly! I would have spent another 10h without seeing that :)