我的代码在单独运行时有效,但当我将它放入一个类中时,它出错。

huangapple go评论59阅读模式
英文:

My code works on its own, but when I put it into a class, it breaks

问题

我一直在制作一个外星人游戏,其中你必须使用 "wasd" 键来控制屏幕上的外星人,但是当我将我的代码放入一个类中时,该类在设置中不读取 Alien.KeyReleased,我不知道为什么。

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 {

  // ... (省略了类中的其他部分)

  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;
    }
  }

  // ... (省略了类中的其他部分)

}

你的代码中 keyReleasedkeyPressed 方法似乎是正确的,但是请确保在 draw 函数中正确调用这两个方法。如果你的问题仍然存在,可能需要检查其他部分的代码,以确保没有其他错误导致 Alien.KeyReleased 方法没有按预期执行。

英文:

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

答案1

得分: 1

当您使用Processing时,您的initdraw方法以及您的草图中的所有其他代码都会被插入到Processing生成的新类中。这个类扩展了PApplet

您可以在这里查看Javadoc:https://processing.github.io/processing-javadocs/core/processing/core/PApplet.html

这个父类是keyPressed等方法的声明位置。Processing会调用您对这些方法的实现,但只在它创建的PApplet子类上调用这些方法。

当您将代码移到一个不扩展PApplet的类中时,这些方法将不再被框架调用。

您可以保留您的所有代码在您自己的类中,同时通过将它们委托给您的Alien实例来实现Processing框架调用这些方法:

// 这些方法位于您的草图中,而不是您的Alien类中
void keyReleased() {
  alien.keyReleased();
}

void keyPressed() {
  alien.keyPressed();
}

您无需更改Alien的实现。

(我已将您的字段Alien重命名为alien - 变量名不应以大写字母开头)

英文:

When you use Processing, your init and draw methods, and all the other code in your sketch is inserted into a new class which is generated by Processing. This class extends PApplet.

You can see the Javadoc here: https://processing.github.io/processing-javadocs/core/processing/core/PApplet.html

That parent class is where methods like keyPressed are declared. Processing will call your implementation of those methods, but only on the PApplet subclass 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:

// these methods are in your sketch, not your Alien class
void keyReleased() {
alien.keyReleased();
}
void keyPressed() {
alien.keyPressed();
}

You don't need to change your Alien implementation.

(I've renamed your field Alien to alien -- variable names should not start with capital letters)

huangapple
  • 本文由 发表于 2023年3月12日 07:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定