###前提・実現したいこと
こんにちは。毎度お世話になっております。
現在 Java でボンバーマンを作っていますが、発生しているエラーの処理の仕方がわかりません。
聞きたいことは3つです。
- 例外をどうやって補足するのか
- コンストラクタが例外を throw することはそもそも好ましくないのか
- その他修正すべきところ。
よろしくお願いいたします。
###発生している問題・エラーメッセージ
Player.java:8: エラー: クラス GameObjectのコンストラクタ GameObjectは指定された 型に適用できません。 Player(int x, int y){ ^ 期待値: String,int,int 検出値: 引数がありません 理由: 実引数リストと仮引数リストの長さが異なります Player.java:11: エラー: superの呼出しはコンストラクタの先頭文である必要がありま す super("player.png", x, y); ^ Player.java:41: エラー: クラス GameObjectのコンストラクタ GameObjectは指定された 型に適用できません。 Bomb(){ ^ 期待値: String,int,int 検出値: 引数がありません 理由: 実引数リストと仮引数リストの長さが異なります Player.java:44: エラー: superの呼出しはコンストラクタの先頭文である必要がありま す super("bomb.png", -GameObject.SIDE, -GameObject.SIDE); ^ エラー4個
###該当のソースコード1
java
1// Player.java 2public class Player extends GameObject{ 3 private static final int MAX_CAPACITY = 8; 4 5 private Bomb[] bomb; 6 private int capacity; 7 8 Player(int x, int y){ 9 //super("player.png", x, y); 10 try{ 11 super("player.png", x, y); 12 }catch(Exception e){ 13 e.printStackTrace(); 14 } 15 this.capacity = 1; 16 try{ 17 this.bomb = new Bomb[MAX_CAPACITY]; 18 for(Bomb b : bomb){ 19 // 最初は画面外に配置 20 b = new Bomb(); 21 } 22 }catch(Exception e){ 23 e.printStackTrace(); 24 } 25 } 26 27 void putBomb(){ 28 if(capacity == 0){ 29 return; 30 } 31 this.bomb[capacity-1].put(this.x, this.y); 32 this.capacity--; 33 } 34} 35 36class Bomb extends GameObject{ 37 private static final int TIME_LIMIT = 1000; 38 39 private int time_left; 40 41 Bomb(){ 42 //super("bomb.png", -GameObject.SIDE, -GameObject.SIDE); 43 try{ 44 super("bomb.png", -GameObject.SIDE, -GameObject.SIDE); 45 }catch(Exception e){ 46 e.printStackTrace(); 47 } 48 this.time_left = 0; 49 } 50 51 void put(int x, int y){ 52 this.x = x; 53 this.y = y; 54 this.time_left = this.TIME_LIMIT; 55 } 56 57 void countDown(){ 58 this.time_left--; 59 } 60}
###該当のソースコード2
java
1// GameObject.java 2import javax.swing.JPanel; 3import java.awt.Graphics; 4import java.awt.Image; 5import java.awt.image.BufferedImage; 6import java.io.File; 7import javax.imageio.ImageIO; 8import java.io.IOException; 9 10class GameObject{ 11 static final int SIDE = 40; 12 13 int dx, dy; 14 protected int x, y; 15 private Image image; 16 17 GameObject(String file_name, int x, int y) throws IOException{ 18 BufferedImage bi = ImageIO.read(new File(file_name)); 19 20 if(bi.getWidth() != this.SIDE || bi.getHeight() != this.SIDE) 21 throw new IOException("画像サイズは" + this.SIDE + "*" + this.SIDE + "でなければなりません. : " + file_name); 22 this.image = bi; 23 this.x = x; 24 this.y = y; 25 this.dx = 0; 26 this.dy = 0; 27 } 28 29 int getX(){ 30 return this.x; 31 } 32 33 int getY(){ 34 return this.y; 35 } 36 37 void setLocation(int x, int y){ 38 this.x = x; 39 this.y = y; 40 } 41 42 void move(){ 43 this.x += this.dx; 44 this.y += this.dy; 45 } 46 47 void draw(Graphics g, JPanel io){ 48 int x0 = this.x - this.SIDE/2; 49 int y0 = this.y - this.SIDE/2; 50 51 g.drawImage(this.image, x0, y0, io); 52 } 53}
###試したこと
ソースコード1に対してコメントアウトしている位置に super クラスのコンストラクタの記述を移動させたが、今度は例外が補足できない。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/14 00:33