I have been making this alien game where you have to control the alien on a screen using "wasd", but when I put my code into a class, the class does not read Alien.KeyReleased in the set up, and I have no idea why
Alien Alien;
void setup(){
size (950, 650);
background(10,10,20);
Alien = new Alien(90, 150, 200, 200);
}
void draw() {
noStroke();
background(10,10,25);
Alien.drawAlien();
Alien.moveAlien();
Alien.checkForedge();
Alien.keyReleased();
Alien.keyPressed();
}
class Alien {
int alienSpeed = 5;
boolean moveUp;
boolean moveDown;
boolean moveLeft;
boolean moveRight;
int boundboxXLoc = 325;
int boundboxYLoc = 325;
int boundboxWidth = 90;
int boundboxHeight = 150;
//body
int BodyXLoc;
int BodyYLoc;
int BodyWidth;
int BodyHeight;
//spot1
int spot1XLoc ;
int spot1YLoc ;
int spot1Width;
int spot1Height;
//spot2
int spot2XLoc ;
int spot2YLoc ;
int spot2Width;
int spot2Height;
//spot23
int spot3XLoc ;
int spot3YLoc ;
int spot3Width;
int spot3Height;
//Generate random colours
int Red = (int) random(200);
int Blue = (int) random(200);
int Green = (int) random(200);
int Opacity = 255;
//Generate spot1 Loc, mid
int spot1LocX = (int) random(-13,8);
//Generate spot2Loc, small
int spot2LocX = (int) random(-4,27);
//Generate spot3 Loc, big
int spot3LocX = (int) random(-2,20);
//arms
int armsXLoc ;
int armsYLoc ;
int armsWidth;
int armsHeight;
//head
int HeadXLoc;
int HeadYLoc;
int HeadWidth;
int HeadHeight;
//mouth
int mouthXLoc;
int mouthYLoc;
int mouthWidth;
int mouthHeight;
//eyestalk
int eyestalkXLoc;
int eyestalkYLoc;
int eyestalkWidth;
int eyestalkHeight;
//eyestalk1
int eyestalk1XLoc;
int eyestalk1YLoc;
int eyestalk1Width;
int eyestalk1Height;
//eyestalk2
int eyestalk2XLoc;
int eyestalk2YLoc;
int eyestalk2Width;
int eyestalk2Height;
//eyestalk3
int eyestalk3XLoc;
int eyestalk3YLoc;
int eyestalk3Width;
int eyestalk3Height;
//eyestalk4
int eyestalk4XLoc;
int eyestalk4YLoc;
int eyestalk4Width;
int eyestalk4Height;
//eyestalk5
int eyestalk5XLoc;
int eyestalk5YLoc;
int eyestalk5Width;
int eyestalk5Height;
//eyestalk6
int eyestalk6XLoc;
int eyestalk6YLoc;
int eyestalk6Width;
int eyestalk6Height;
///eyestalk7
int eyestalk7XLoc;
int eyestalk7YLoc;
int eyestalk7Width;
int eyestalk7Height;
//ship
int shipXLoc;
int shipYLoc;
int shipWidth = 180;
int shipHeight = 75;
//ship1
int ship1XLoc;
int ship1YLoc;
int ship1Width = 100;
int ship1Height = 30;
Alien (int bW, int bH, int xL, int yL) {
boundboxWidth = bW;
boundboxHeight = bH;
boundboxXLoc = xL;
boundboxYLoc = yL;
}
// draw the boundary box
void drawAlien() {
fill (100,0,0,0);
ellipse(boundboxXLoc, boundboxYLoc, boundboxWidth, boundboxHeight);
// draw the Body
fill (Red,Green,Blue,Opacity);
ellipse(BodyXLoc, BodyYLoc, BodyWidth, BodyHeight);
BodyXLoc = boundboxXLoc;
BodyYLoc = boundboxYLoc + (boundboxHeight/8);
BodyWidth = boundboxWidth / (175/75) ;
BodyHeight = boundboxHeight / (250/100);
// draw the head
fill (Red,Green,Blue,Opacity);
ellipse(HeadXLoc, HeadYLoc, HeadWidth, HeadHeight);
HeadXLoc = boundboxXLoc;
HeadYLoc = boundboxYLoc - (boundboxHeight/5);
HeadWidth = boundboxWidth / (175/70);
HeadHeight = boundboxHeight / (250/56);
// draw the right eye stalk
fill (Red,Green,Blue,Opacity);
rect(eyestalkXLoc, eyestalkYLoc, eyestalkWidth, eyestalkHeight);
eyestalkXLoc = boundboxXLoc + (boundboxWidth/17);
eyestalkYLoc = boundboxYLoc - (boundboxHeight/2)+15;
eyestalkWidth = boundboxWidth / (175/20) - 1;
eyestalkHeight = boundboxHeight / (250/50);
// draw the left eye stalk
fill (Red,Green,Blue,Opacity);
rect(eyestalk1XLoc, eyestalk1YLoc, eyestalk1Width, eyestalk1Height);
eyestalk1XLoc = boundboxXLoc -(boundboxWidth/6);
eyestalk1YLoc = boundboxYLoc - (boundboxHeight/2)+15;
eyestalk1Width = boundboxWidth / (175/20) - 1;
eyestalk1Height = boundboxHeight / (250/50);
// the white part of the eye, right
fill (255,255,255);
ellipse(eyestalk2XLoc, eyestalk2YLoc, eyestalk2Width, eyestalk2Height);
eyestalk2XLoc = boundboxXLoc + boundboxWidth / (175/20);
eyestalk2YLoc = boundboxYLoc - boundboxHeight / (240/75);
eyestalk2Width = boundboxWidth / (175/10);
eyestalk2Height = boundboxHeight / (250/30);
// the white part of the eye, left
fill (255,255,255);
ellipse(eyestalk3XLoc, eyestalk3YLoc, eyestalk3Width, eyestalk3Height);
eyestalk3XLoc = boundboxXLoc - boundboxWidth / (175/20);
eyestalk3YLoc = boundboxYLoc - boundboxHeight / (240/75);
eyestalk3Width = boundboxWidth / (175/10);
eyestalk3Height = boundboxHeight / (250/30);
// the right pupil
fill (0,0,0);
ellipse(eyestalk4XLoc, eyestalk4YLoc, eyestalk4Width, eyestalk4Height);
eyestalk4XLoc = boundboxXLoc + boundboxWidth / (175/20);
eyestalk4YLoc = boundboxYLoc - boundboxHeight / (240/75);
eyestalk4Width = boundboxWidth / (175/5);
eyestalk4Height = boundboxHeight / (250/15);
// the left pupil
fill (0,0,0);
ellipse(eyestalk5XLoc, eyestalk5YLoc, eyestalk5Width, eyestalk5Height);
eyestalk5XLoc = boundboxXLoc - boundboxWidth / (175/20);
eyestalk5YLoc = boundboxYLoc - boundboxHeight / (240/75);
eyestalk5Width = boundboxWidth / (175/5);
eyestalk5Height = boundboxHeight / (250/15);
// the top part of the eye stalk, right
fill (Red,Green,Blue,Opacity);
ellipse(eyestalk6XLoc, eyestalk6YLoc, eyestalk6Width, eyestalk6Height);
eyestalk6XLoc = boundboxXLoc + boundboxWidth / (175/19);
eyestalk6YLoc = eyestalkYLoc;
eyestalk6Width = boundboxWidth / (175/20);
eyestalk6Height = boundboxHeight / (250/20);
// the top part of the eye stalk, left
fill (Red,Green,Blue,Opacity);
ellipse(eyestalk7XLoc, eyestalk7YLoc, eyestalk7Width, eyestalk7Height);
eyestalk7XLoc = boundboxXLoc - boundboxWidth / (175/19);
eyestalk7YLoc = eyestalkYLoc;
eyestalk7Width = boundboxWidth / (175/20);
eyestalk7Height = boundboxHeight / (250/20);
// The mouth
fill (50,50,50);
ellipse(mouthXLoc, mouthYLoc, mouthWidth, mouthHeight);
mouthXLoc = boundboxXLoc;
mouthYLoc = boundboxYLoc - boundboxHeight/6;
mouthWidth = boundboxWidth / (175/40);
mouthHeight = boundboxHeight / (250/10);
// the arms
fill (Red,Green,Blue,Opacity);
ellipse(armsXLoc, armsYLoc, armsWidth, armsHeight);
armsXLoc = BodyXLoc;
armsYLoc = BodyYLoc-10;
armsWidth = boundboxWidth ;
armsHeight = boundboxHeight / (250/15);
//spot1
fill ((Red-40),(Green-40),(Blue-40),Opacity);
ellipse(spot1XLoc, spot1YLoc, spot1Width, spot1Height);
spot1XLoc = boundboxXLoc + spot1LocX + (boundboxWidth/20);
spot1YLoc = boundboxYLoc + (boundboxHeight/50);
spot1Width = boundboxWidth / (175/30) ;
spot1Height = boundboxHeight / (250/15);
//spot2
fill ((Red-40),(Green-40),(Blue-40),Opacity);
ellipse(spot2XLoc, spot2YLoc, spot2Width, spot2Height);
spot2XLoc = boundboxXLoc + spot2LocX - (boundboxWidth / 10);
spot2YLoc = boundboxYLoc - (boundboxHeight/20);
spot2Width = boundboxWidth / (175/15) ;
spot2Height = boundboxHeight / (250/13);
//spot3
fill ((Red-40),(Green-40),(Blue-40),Opacity);
ellipse(spot3XLoc, spot3YLoc, spot3Width, spot3Height);
spot3XLoc = boundboxXLoc + spot3LocX - (boundboxWidth / 10);
spot3YLoc = boundboxYLoc + (boundboxHeight/10);
spot3Width = boundboxWidth / (175/50) ;
spot3Height = boundboxHeight / (250/23);
// the ship
fill (100,100,100);
ellipse(shipXLoc, shipYLoc, shipWidth, shipHeight);
shipXLoc = BodyXLoc;
shipYLoc = (BodyYLoc + (boundboxHeight/5));
shipWidth = boundboxWidth;
shipHeight = boundboxHeight / (250/75);
// the bottom of the ship
fill (150,170,200);
ellipse(ship1XLoc, ship1YLoc, ship1Width, ship1Height);
ship1XLoc = BodyXLoc;
ship1YLoc = (shipYLoc + (shipHeight/3));
ship1Width = boundboxWidth / (250/100);
ship1Height = boundboxHeight / (250/30);
}
void moveAlien() {
if (moveUp) {
boundboxYLoc -= alienSpeed;
}
if (moveRight) {
if (moveUp) {
boundboxXLoc += (int) (alienSpeed * Math.sqrt(2) / 2);
boundboxYLoc -= (int) (alienSpeed * Math.sqrt(2) / 2);
} else {
boundboxXLoc += alienSpeed;
}
}
if (moveDown) {
boundboxYLoc += alienSpeed;
}
if (moveLeft) {
if (moveDown) {
boundboxXLoc -= (int) (alienSpeed * Math.sqrt(2) / 2);
boundboxYLoc += (int) (alienSpeed * Math.sqrt(2) / 2);
} else {
boundboxXLoc -= alienSpeed;
}
}
}
void keyReleased() {
if (key == 'w') {
moveUp = false;
}
if (key == 'a') {
moveLeft = false;
}
if (key == 's') {
moveDown = false;
}
if (key == 'd') {
moveRight = false;
}
}
void keyPressed() {
if (key == 'w') {
moveUp = true;
}
if (key == 's') {
moveDown = true;
}
if (key == 'a') {
moveLeft = true;
}
if (key == 'd') {
moveRight = true;
}
}
void checkForedge() {
// right wall
if(boundboxXLoc >= 990) {
boundboxXLoc = boundboxXLoc -1050;
}
// left wall
if(boundboxXLoc <= -50) {
boundboxXLoc = boundboxXLoc + 1050;
}
// bottom wall
if(boundboxYLoc <= -70) {
boundboxYLoc = boundboxYLoc + 780;
}
// top wall
if(boundboxYLoc >= 700) {
boundboxYLoc = boundboxYLoc - 780;
}
}
}//end class
I tried moving about the position of ALien.KeyPressed and Alien.KeyReleased hoping it would help it read it, but thats had no affect
When you use Processing, your
initanddrawmethods, and all the other code in your sketch is inserted into a new class which is generated by Processing. This class extendsPApplet.You can see the Javadoc here: https://processing.github.io/processing-javadocs/core/processing/core/PApplet.html
That parent class is where methods like
keyPressedare declared. Processing will call your implementation of those methods, but only on thePAppletsubclass it has created.When you move your code to a class which doesn't extend
PApplet, those methods are no longer called by the framework.You can keep all your code in your own class while still having the Processing framework call those methods by delegating them to your Alien instance:
You don't need to change your
Alienimplementation.(I've renamed your field
Alientoalien-- variable names should not start with capital letters)