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

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

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

问题

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

  1. Alien Alien;
  2. void setup() {
  3. size(950, 650);
  4. background(10, 10, 20);
  5. Alien = new Alien(90, 150, 200, 200);
  6. }
  7. void draw() {
  8. noStroke();
  9. background(10, 10, 25);
  10. Alien.drawAlien();
  11. Alien.moveAlien();
  12. Alien.checkForedge();
  13. Alien.keyReleased();
  14. Alien.keyPressed();
  15. }
  1. class Alien {
  2. // ... (省略了类中的其他部分)
  3. void keyReleased() {
  4. if (key == 'w') {
  5. moveUp = false;
  6. }
  7. if (key == 'a') {
  8. moveLeft = false;
  9. }
  10. if (key == 's') {
  11. moveDown = false;
  12. }
  13. if (key == 'd') {
  14. moveRight = false;
  15. }
  16. }
  17. void keyPressed() {
  18. if (key == 'w') {
  19. moveUp = true;
  20. }
  21. if (key == 's') {
  22. moveDown = true;
  23. }
  24. if (key == 'a') {
  25. moveLeft = true;
  26. }
  27. if (key == 'd') {
  28. moveRight = true;
  29. }
  30. }
  31. // ... (省略了类中的其他部分)
  32. }

你的代码中 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

  1. Alien Alien;
  2. void setup(){
  3. size (950, 650);
  4. background(10,10,20);
  5. Alien = new Alien(90, 150, 200, 200);
  6. }
  7. void draw() {
  8. noStroke();
  9. background(10,10,25);
  10. Alien.drawAlien();
  11. Alien.moveAlien();
  12. Alien.checkForedge();
  13. Alien.keyReleased();
  14. Alien.keyPressed();
  15. }
  1. class Alien {
  2. int alienSpeed = 5;
  3. boolean moveUp;
  4. boolean moveDown;
  5. boolean moveLeft;
  6. boolean moveRight;
  7. int boundboxXLoc = 325;
  8. int boundboxYLoc = 325;
  9. int boundboxWidth = 90;
  10. int boundboxHeight = 150;
  11. //body
  12. int BodyXLoc;
  13. int BodyYLoc;
  14. int BodyWidth;
  15. int BodyHeight;
  16. //spot1
  17. int spot1XLoc ;
  18. int spot1YLoc ;
  19. int spot1Width;
  20. int spot1Height;
  21. //spot2
  22. int spot2XLoc ;
  23. int spot2YLoc ;
  24. int spot2Width;
  25. int spot2Height;
  26. //spot23
  27. int spot3XLoc ;
  28. int spot3YLoc ;
  29. int spot3Width;
  30. int spot3Height;
  31. //Generate random colours
  32. int Red = (int) random(200);
  33. int Blue = (int) random(200);
  34. int Green = (int) random(200);
  35. int Opacity = 255;
  36. //Generate spot1 Loc, mid
  37. int spot1LocX = (int) random(-13,8);
  38. //Generate spot2Loc, small
  39. int spot2LocX = (int) random(-4,27);
  40. //Generate spot3 Loc, big
  41. int spot3LocX = (int) random(-2,20);
  42. //arms
  43. int armsXLoc ;
  44. int armsYLoc ;
  45. int armsWidth;
  46. int armsHeight;
  47. //head
  48. int HeadXLoc;
  49. int HeadYLoc;
  50. int HeadWidth;
  51. int HeadHeight;
  52. //mouth
  53. int mouthXLoc;
  54. int mouthYLoc;
  55. int mouthWidth;
  56. int mouthHeight;
  57. //eyestalk
  58. int eyestalkXLoc;
  59. int eyestalkYLoc;
  60. int eyestalkWidth;
  61. int eyestalkHeight;
  62. //eyestalk1
  63. int eyestalk1XLoc;
  64. int eyestalk1YLoc;
  65. int eyestalk1Width;
  66. int eyestalk1Height;
  67. //eyestalk2
  68. int eyestalk2XLoc;
  69. int eyestalk2YLoc;
  70. int eyestalk2Width;
  71. int eyestalk2Height;
  72. //eyestalk3
  73. int eyestalk3XLoc;
  74. int eyestalk3YLoc;
  75. int eyestalk3Width;
  76. int eyestalk3Height;
  77. //eyestalk4
  78. int eyestalk4XLoc;
  79. int eyestalk4YLoc;
  80. int eyestalk4Width;
  81. int eyestalk4Height;
  82. //eyestalk5
  83. int eyestalk5XLoc;
  84. int eyestalk5YLoc;
  85. int eyestalk5Width;
  86. int eyestalk5Height;
  87. //eyestalk6
  88. int eyestalk6XLoc;
  89. int eyestalk6YLoc;
  90. int eyestalk6Width;
  91. int eyestalk6Height;
  92. ///eyestalk7
  93. int eyestalk7XLoc;
  94. int eyestalk7YLoc;
  95. int eyestalk7Width;
  96. int eyestalk7Height;
  97. //ship
  98. int shipXLoc;
  99. int shipYLoc;
  100. int shipWidth = 180;
  101. int shipHeight = 75;
  102. //ship1
  103. int ship1XLoc;
  104. int ship1YLoc;
  105. int ship1Width = 100;
  106. int ship1Height = 30;
  107. Alien (int bW, int bH, int xL, int yL) {
  108. boundboxWidth = bW;
  109. boundboxHeight = bH;
  110. boundboxXLoc = xL;
  111. boundboxYLoc = yL;
  112. }
  113. // draw the boundary box
  114. void drawAlien() {
  115. fill (100,0,0,0);
  116. ellipse(boundboxXLoc, boundboxYLoc, boundboxWidth, boundboxHeight);
  117. // draw the Body
  118. fill (Red,Green,Blue,Opacity);
  119. ellipse(BodyXLoc, BodyYLoc, BodyWidth, BodyHeight);
  120. BodyXLoc = boundboxXLoc;
  121. BodyYLoc = boundboxYLoc + (boundboxHeight/8);
  122. BodyWidth = boundboxWidth / (175/75) ;
  123. BodyHeight = boundboxHeight / (250/100);
  124. // draw the head
  125. fill (Red,Green,Blue,Opacity);
  126. ellipse(HeadXLoc, HeadYLoc, HeadWidth, HeadHeight);
  127. HeadXLoc = boundboxXLoc;
  128. HeadYLoc = boundboxYLoc - (boundboxHeight/5);
  129. HeadWidth = boundboxWidth / (175/70);
  130. HeadHeight = boundboxHeight / (250/56);
  131. // draw the right eye stalk
  132. fill (Red,Green,Blue,Opacity);
  133. rect(eyestalkXLoc, eyestalkYLoc, eyestalkWidth, eyestalkHeight);
  134. eyestalkXLoc = boundboxXLoc + (boundboxWidth/17);
  135. eyestalkYLoc = boundboxYLoc - (boundboxHeight/2)+15;
  136. eyestalkWidth = boundboxWidth / (175/20) - 1;
  137. eyestalkHeight = boundboxHeight / (250/50);
  138. // draw the left eye stalk
  139. fill (Red,Green,Blue,Opacity);
  140. rect(eyestalk1XLoc, eyestalk1YLoc, eyestalk1Width, eyestalk1Height);
  141. eyestalk1XLoc = boundboxXLoc -(boundboxWidth/6);
  142. eyestalk1YLoc = boundboxYLoc - (boundboxHeight/2)+15;
  143. eyestalk1Width = boundboxWidth / (175/20) - 1;
  144. eyestalk1Height = boundboxHeight / (250/50);
  145. // the white part of the eye, right
  146. fill (255,255,255);
  147. ellipse(eyestalk2XLoc, eyestalk2YLoc, eyestalk2Width, eyestalk2Height);
  148. eyestalk2XLoc = boundboxXLoc + boundboxWidth / (175/20);
  149. eyestalk2YLoc = boundboxYLoc - boundboxHeight / (240/75);
  150. eyestalk2Width = boundboxWidth / (175/10);
  151. eyestalk2Height = boundboxHeight / (250/30);
  152. // the white part of the eye, left
  153. fill (255,255,255);
  154. ellipse(eyestalk3XLoc, eyestalk3YLoc, eyestalk3Width, eyestalk3Height);
  155. eyestalk3XLoc = boundboxXLoc - boundboxWidth / (175/20);
  156. eyestalk3YLoc = boundboxYLoc - boundboxHeight / (240/75);
  157. eyestalk3Width = boundboxWidth / (175/10);
  158. eyestalk3Height = boundboxHeight / (250/30);
  159. // the right pupil
  160. fill (0,0,0);
  161. ellipse(eyestalk4XLoc, eyestalk4YLoc, eyestalk4Width, eyestalk4Height);
  162. eyestalk4XLoc = boundboxXLoc + boundboxWidth / (175/20);
  163. eyestalk4YLoc = boundboxYLoc - boundboxHeight / (240/75);
  164. eyestalk4Width = boundboxWidth / (175/5);
  165. eyestalk4Height = boundboxHeight / (250/15);
  166. // the left pupil
  167. fill (0,0,0);
  168. ellipse(eyestalk5XLoc, eyestalk5YLoc, eyestalk5Width, eyestalk5Height);
  169. eyestalk5XLoc = boundboxXLoc - boundboxWidth / (175/20);
  170. eyestalk5YLoc = boundboxYLoc - boundboxHeight / (240/75);
  171. eyestalk5Width = boundboxWidth / (175/5);
  172. eyestalk5Height = boundboxHeight / (250/15);
  173. // the top part of the eye stalk, right
  174. fill (Red,Green,Blue,Opacity);
  175. ellipse(eyestalk6XLoc, eyestalk6YLoc, eyestalk6Width, eyestalk6Height);
  176. eyestalk6XLoc = boundboxXLoc + boundboxWidth / (175/19);
  177. eyestalk6YLoc = eyestalkYLoc;
  178. eyestalk6Width = boundboxWidth / (175/20);
  179. eyestalk6Height = boundboxHeight / (250/20);
  180. // the top part of the eye stalk, left
  181. fill (Red,Green,Blue,Opacity);
  182. ellipse(eyestalk7XLoc, eyestalk7YLoc, eyestalk7Width, eyestalk7Height);
  183. eyestalk7XLoc = boundboxXLoc - boundboxWidth / (175/19);
  184. eyestalk7YLoc = eyestalkYLoc;
  185. eyestalk7Width = boundboxWidth / (175/20);
  186. eyestalk7Height = boundboxHeight / (250/20);
  187. // The mouth
  188. fill (50,50,50);
  189. ellipse(mouthXLoc, mouthYLoc, mouthWidth, mouthHeight);
  190. mouthXLoc = boundboxXLoc;
  191. mouthYLoc = boundboxYLoc - boundboxHeight/6;
  192. mouthWidth = boundboxWidth / (175/40);
  193. mouthHeight = boundboxHeight / (250/10);
  194. // the arms
  195. fill (Red,Green,Blue,Opacity);
  196. ellipse(armsXLoc, armsYLoc, armsWidth, armsHeight);
  197. armsXLoc = BodyXLoc;
  198. armsYLoc = BodyYLoc-10;
  199. armsWidth = boundboxWidth ;
  200. armsHeight = boundboxHeight / (250/15);
  201. //spot1
  202. fill ((Red-40),(Green-40),(Blue-40),Opacity);
  203. ellipse(spot1XLoc, spot1YLoc, spot1Width, spot1Height);
  204. spot1XLoc = boundboxXLoc + spot1LocX + (boundboxWidth/20);
  205. spot1YLoc = boundboxYLoc + (boundboxHeight/50);
  206. spot1Width = boundboxWidth / (175/30) ;
  207. spot1Height = boundboxHeight / (250/15);
  208. //spot2
  209. fill ((Red-40),(Green-40),(Blue-40),Opacity);
  210. ellipse(spot2XLoc, spot2YLoc, spot2Width, spot2Height);
  211. spot2XLoc = boundboxXLoc + spot2LocX - (boundboxWidth / 10);
  212. spot2YLoc = boundboxYLoc - (boundboxHeight/20);
  213. spot2Width = boundboxWidth / (175/15) ;
  214. spot2Height = boundboxHeight / (250/13);
  215. //spot3
  216. fill ((Red-40),(Green-40),(Blue-40),Opacity);
  217. ellipse(spot3XLoc, spot3YLoc, spot3Width, spot3Height);
  218. spot3XLoc = boundboxXLoc + spot3LocX - (boundboxWidth / 10);
  219. spot3YLoc = boundboxYLoc + (boundboxHeight/10);
  220. spot3Width = boundboxWidth / (175/50) ;
  221. spot3Height = boundboxHeight / (250/23);
  222. // the ship
  223. fill (100,100,100);
  224. ellipse(shipXLoc, shipYLoc, shipWidth, shipHeight);
  225. shipXLoc = BodyXLoc;
  226. shipYLoc = (BodyYLoc + (boundboxHeight/5));
  227. shipWidth = boundboxWidth;
  228. shipHeight = boundboxHeight / (250/75);
  229. // the bottom of the ship
  230. fill (150,170,200);
  231. ellipse(ship1XLoc, ship1YLoc, ship1Width, ship1Height);
  232. ship1XLoc = BodyXLoc;
  233. ship1YLoc = (shipYLoc + (shipHeight/3));
  234. ship1Width = boundboxWidth / (250/100);
  235. ship1Height = boundboxHeight / (250/30);
  236. }
  237. void moveAlien() {
  238. if (moveUp) {
  239. boundboxYLoc -= alienSpeed;
  240. }
  241. if (moveRight) {
  242. if (moveUp) {
  243. boundboxXLoc += (int) (alienSpeed * Math.sqrt(2) / 2);
  244. boundboxYLoc -= (int) (alienSpeed * Math.sqrt(2) / 2);
  245. } else {
  246. boundboxXLoc += alienSpeed;
  247. }
  248. }
  249. if (moveDown) {
  250. boundboxYLoc += alienSpeed;
  251. }
  252. if (moveLeft) {
  253. if (moveDown) {
  254. boundboxXLoc -= (int) (alienSpeed * Math.sqrt(2) / 2);
  255. boundboxYLoc += (int) (alienSpeed * Math.sqrt(2) / 2);
  256. } else {
  257. boundboxXLoc -= alienSpeed;
  258. }
  259. }
  260. }
  261. void keyReleased() {
  262. if (key == 'w') {
  263. moveUp = false;
  264. }
  265. if (key == 'a') {
  266. moveLeft = false;
  267. }
  268. if (key == 's') {
  269. moveDown = false;
  270. }
  271. if (key == 'd') {
  272. moveRight = false;
  273. }
  274. }
  275. void keyPressed() {
  276. if (key == 'w') {
  277. moveUp = true;
  278. }
  279. if (key == 's') {
  280. moveDown = true;
  281. }
  282. if (key == 'a') {
  283. moveLeft = true;
  284. }
  285. if (key == 'd') {
  286. moveRight = true;
  287. }
  288. }
  289. void checkForedge() {
  290. // right wall
  291. if(boundboxXLoc >= 990) {
  292. boundboxXLoc = boundboxXLoc -1050;
  293. }
  294. // left wall
  295. if(boundboxXLoc <= -50) {
  296. boundboxXLoc = boundboxXLoc + 1050;
  297. }
  298. // bottom wall
  299. if(boundboxYLoc <= -70) {
  300. boundboxYLoc = boundboxYLoc + 780;
  301. }
  302. // top wall
  303. if(boundboxYLoc >= 700) {
  304. boundboxYLoc = boundboxYLoc - 780;
  305. }
  306. }
  307. }//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框架调用这些方法:

  1. // 这些方法位于您的草图中,而不是您的Alien类中
  2. void keyReleased() {
  3. alien.keyReleased();
  4. }
  5. void keyPressed() {
  6. alien.keyPressed();
  7. }

您无需更改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:

  1. // these methods are in your sketch, not your Alien class
  2. void keyReleased() {
  3. alien.keyReleased();
  4. }
  5. void keyPressed() {
  6. alien.keyPressed();
  7. }

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:

确定