###前提・実現したいこと
processingでシューティングゲームを作成したのですが、エラーが出て起動できません、このエラーを治すにはどうすればよいのでしょうか?
###発生している問題・エラーメッセージ
NullPointerException
###該当のソースコード
PFont myFont; SpaceShip myShip; int numOfEnemies = 3; SpaceShip[] en1 = new SpaceShip[numOfEnemies]; int score = 0; int numOfMaxShots = 50; int nextShot = 0; Shot[] shots = new Shot[numOfMaxShots]; int numOfMaxBombs = 50; Bomb[] bombs = new Bomb[numOfMaxBombs]; int nextBomb = 0; int code=0; boolean keyLeft = false; boolean keyRight = false; boolean keyUp = false; boolean keyDown = false; void setup() { size(600, 600); background(255, 255, 255); fill(0, 0, 0); noStroke(); noCursor(); frameRate(60); myFont = createFont("YuGothic", 24, true); textFont(myFont); myShip = new SpaceShip(loadImage("fighter.png"), 100, 500, 3, 1); myShip.visible = true; for(int i = 0; i < numOfEnemies; i++) { en1[i] = new SpaceShip(loadImage("DurrrSpaceShip.png"), i * 100 + 50, i * 50 + 100, 3, 2); en1[i].visible = true; } for(int i = 0; i < numOfMaxShots; i++) { shots[i] = new Shot(0, height + 100, color(255, 255, 0)); } for(int i = 0; i < numOfMaxBombs; i++) { bombs[i] = new Bomb(0, -100, color(255, 0, 0)); } } void draw() { background(0); myShip.draw(); myShipController(); drawEnemies(); drawBombs(); enemyController(); shotsController(); collisionDetection(); } { if (code==0) { background(0); textSize(32); fill(255); text("'a Click' is clicked to start", 400, 350) ; } if (code==1) { background(0); for(int i = 0; i < numOfMaxShots; i++) { shots[i] = new Shot(0, height + 100, color(255, 255, 0)); } text("SCORE:"+str(score), width/7, height -50) ; } if (code==2) { fill(0); rect(0, 0, width, height); textSize(32); fill(255); text("GAME OVER", 600, 350) ; text("RESTART is 'd Click'", 600, 500); } } void drawEnemies() { for(int i = 0; i < numOfEnemies; i++) { en1[i].draw(); } } void enemyController() { for(int i = 0; i < numOfEnemies; i++) { if(en1[i].xpos < 0 || en1[i].xpos + en1[i].getWidth() > width) { en1[i].speed = - en1[i].speed; } en1[i].moveXaxis(); if(en1[i].visible) { if(floor(random(50)) == 5) { bombs[nextBomb].xpos = en1[i].xpos + en1[i].getWidth() / 2; bombs[nextBomb].ypos = en1[i].ypos + en1[i].getHeight(); bombs[nextBomb].visible = true; nextBomb = (nextBomb + 1) % numOfMaxBombs; } } } } void drawBombs() { for(int i = 0; i < numOfMaxBombs; i++) { bombs[i].draw(); bombs[i].move(); } } void collisionDetection() { for(int i = 0; i < numOfEnemies; i++) { for(int j = 0; j < numOfMaxShots; j++) { if(en1[i].visible && en1[i].isIn(shots[j].xpos, shots[j].ypos)) { en1[i].visible = false; shots[j].visible = false; } } } for(int i = 0; i < numOfMaxBombs; i++) { if(bombs[i].visible && myShip.isIn(bombs[i].xpos, bombs[i].ypos)) { myShip.visible = false; bombs[i].visible = false; } } } void keyPressed() { if(key == 'd') { keyRight = true; } if(key == 'a') { keyLeft = true; } if(key == 'w') { keyUp = true; } if(key == 's') { keyDown = true; } } void keyReleased() { if(key == 'a') { keyLeft = false; } else if(key == 'd') { keyRight = false; } else if(key == 'w') { keyUp = false; } else if(key == 's') { keyDown = false; } else if(keyCode == ' ') { fire(); } if (code==0) { if (key=='a') { code=1; } } if (code==2) { if (key=='d') { code=0; } } } void myShipController() { if(keyLeft) { myShip.speed = - abs(myShip.speed); if(myShip.xpos + myShip.speed >= 0) { myShip.moveXaxis(); } } else if(keyRight) { myShip.speed = abs(myShip.speed); if(myShip.xpos + myShip.getWidth() + myShip.speed <= width) { myShip.moveXaxis(); } } else if(keyUp) { myShip.speed = - abs(myShip.speed); if(myShip.ypos + myShip.speed >= 0) { myShip.moveYaxis(); } } else if(keyDown) { myShip.speed = abs(myShip.speed); if(myShip.ypos + myShip.getHeight() + myShip.speed <= height) { myShip.moveYaxis(); } } } void shotsController() { for(int i = 0; i < numOfMaxShots; i++) { shots[i].draw(); shots[i].move(); } } void fire() { if(myShip.visible) { shots[nextShot].xpos = myShip.xpos + myShip.getWidth() / 2; shots[nextShot].ypos = myShip.ypos; shots[nextShot].visible = true; nextShot = (nextShot + 1) % numOfMaxShots; } } class SpaceShip { PImage img; int xpos; int ypos; int speed; int scale; boolean visible = false; SpaceShip(PImage _img, int _xpos, int _ypos, int _speed, int _scale) { img = _img; xpos = _xpos; ypos = _ypos; speed = _speed; scale = _scale; } void draw() { if(visible) { image(img, xpos, ypos, getWidth(), getHeight()); } } void moveXaxis() { xpos += speed; } void moveYaxis() { ypos += speed; } int getWidth() { return img.width / scale; } int getHeight() { return img.height / scale; } boolean isIn(int x, int y) { if(x >= xpos && x <= xpos + getWidth() && y >= ypos && y <= ypos + getHeight()) { return true; } else { return false; } } } class Bomb { int xpos; int ypos; color c; int speed = 3; boolean visible = false; Bomb(int _xpos, int _ypos, color _c) { xpos = _xpos; ypos = _ypos; c = _c; } void draw() { fill(c); if(visible) { fill(c); ellipse(xpos, ypos, 10, 10); } } void move() { if(visible && ypos < height + 50) { ypos += speed; } else { visible = false; } } } class Shot { int xpos; int ypos; color c; int speed = 4; boolean visible = false; Shot(int _xpos, int _ypos, color _c) { xpos = _xpos; ypos = _ypos; c = _c; } void draw() { fill(c); if(visible) { rect(xpos, ypos, 5, 10); } } void move() { if(ypos > -50) { ypos -= speed; } else { visible = false; } } }
###補足情報(言語/FW/ツール等のバージョンなど)
ごちゃごちゃなソースコードですいません、エラー以外にも改善したほうが良い点などがありましたら、指摘してくれるとうれしいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。