質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

1693閲覧

テトリスオブジェクト参照がオブジェクト インスタンスに設定されていません。

LukeFukuda

総合スコア81

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2016/01/31 13:45

テトリスをつくっています。1x4のテトリスブロックが0.1秒ごとに一枠落ちて一番下の枠まできたら2つ目の1x4のブロックが0.1秒ごとに1枠落ちて行ってひとつ前のブロックの上で止まって3つ目は一番上の枠に俳画されてゲームがとまってしまいます。そのあと、少ししたら オブジェクト参照がオブジェクト インスタンスに設定されていません。と下↓↓のところにひょうじされてしまいます。
for (i = 1; i <= getout; i++)
{
g.DrawImage(fm[i], ICHI + 40 + yoko[i], tate[i], fm[i].Width + 60, fm[i].Height + 9);
}

c#

1using System; 2using System.Windows.Forms; 3using System.Drawing; 4using System.Linq; 5using System.Collections; 6 7 8class Sample1 : Form 9{ 10 private static Image WK; 11 12 private static Label [][]gb; 13 14 private static int i, p1, p2, p3, p4, LEVEL, ICHI, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, saikoro, WUI, WIU, WW, canpion; 15 private static int WWW; private static string[][] STR; 16 private static int getout = 1; 17 PictureBox[] lbx = new PictureBox[5]; 18 private static int UI; private static int IU; private static int[] yoko; private static int[][] UU; 19 private static Image[] fm; private static int[] tate; private static int[][] GPS; 20 21 public static void Main() 22 { 23 Application.Run(new Sample1()); 24 } 25 public Sample1() 26 { 27 /// MessageBox.Show("start"); 28 //通常設定 29 this.Text = "サンプル"; this.DoubleBuffered = true; 30 this.Width = 800; this.Height = 800; 31 // 32 PictureBox WAK = new PictureBox(); WAKU W = new WAKU();WAK.Image = W.GetImage();WK = WAK.Image; 33 34 saikoro = 1; 35 36 LEVEL = 100; 37 ICHI = 200; 38 39 40 ///MessageBox.Show("set timer"); 41 42 43 44 45 // 箱作成 46 tate = new int[getout + 21]; 47 yoko = new int[getout + 21]; 48 UU = new int[12][]; 49 for (int t = 0; t <= 11; t++) 50 { 51 UU[t] = new int[18]; 52 } 53 GPS=new int[12][]; 54 for(p9=0;p9<=11;p9++){ 55 GPS[p9] = new int[18]; 56 } 57 STR=new string[12][]; 58 for (int l1 = 0; l1 <= 11; l1++) 59 { 60 STR[l1] = new string[18]; 61 } 62 PictureBox[]im = new PictureBox[getout + 21]; 63 64 65 ///MessageBox.Show("newimage"); 66 67 fm = new Image[getout + 21]; 68 for (p3 = -1; p3 <= 20; p3++) 69 { 70 im[getout + p3] = new PictureBox(); 71 } 72 im[1].Image = Image.FromFile("C:\\Users\\琉久\\Pictures\\TETRIS\\bar.jpg"); 73 for (p6 = -1; p6 <= 20; p6++) 74 { 75 switch (saikoro) 76 { 77 case 1: im[getout + p6].Image = Image.FromFile("C:\\Users\\琉久\\Pictures\\TETRIS\\bar.jpg"); 78 break; 79 case 2: 80 im[getout + p6].Image = Image.FromFile("C:\\Users\\琉久\\Pictures\\TETRIS\\bar.jpg"); 81 break; 82 default: 83 break; 84 } 85 } 86 87 88 for (p2 = -1; p2 <= 20; p2++) 89 { 90 fm[getout+ p2] = im[getout + p2].Image; 91 } 92 93 94 95 96 Timer tm = new Timer(); tm.Interval = LEVEL; tm.Start(); 97 98 this.Paint += new PaintEventHandler(fm_Paint); 99 tm.Tick += new EventHandler(tm_Tick); 100 this.KeyDown += new KeyEventHandler(fm_KeyDown); 101 } 102 public void fm_KeyDown(Object sender, KeyEventArgs e) 103 { 104 if (IU <= 5) 105 { 106 if (e.KeyCode == Keys.Right) 107 { 108 IU++; 109 yoko[getout] += 33; 110 } 111 } 112 if (IU >= 1) 113 { 114 if (e.KeyCode == Keys.Left) 115 { 116 IU--; 117 yoko[getout] -= 33; 118 } 119 } 120 121 } 122 123 124 125 public void tm_Tick(Object sender, EventArgs e) 126 { 127 128 129 130 if (UI <= 15) 131 { 132 if (tate[getout] <= 420) 133 { 134 tate[getout] += 28; 135 } 136 //衝totu 137 UI++; 138 139 } 140 this.Invalidate(); 141 } 142 143 ///MessageBox.Show("eee"); 144 public void fm_Paint(Object sender, PaintEventArgs e) 145 { 146 ///MessageBox.Show("aaa"); 147 148 149 PictureBox[] im = new PictureBox[getout + 21]; fm = new Image[getout + 21]; 150 151 for (p4 = -1; p4 <= 20; p4++) 152 { 153 im[getout + p4] = new PictureBox(); 154 } 155 156 //im[1].Image = Image.FromFile("C:\\Users\\琉久\\Pictures\\TETRIS\\bar.jpg"); 157 for (p1 = -1; p1 <= 20; p1++) 158 { 159 switch (saikoro) 160 { 161 case 1: im[getout + p1].Image = Image.FromFile("C:\\Users\\琉久\\Pictures\\TETRIS\\bar.jpg"); 162 break; 163 case 2: im[getout + p1].Image = Image.FromFile("C:\\Users\\琉久\\Pictures\\TETRIS\\squre.jpg"); 164 165 break; 166 167 default: 168 break; 169 } 170 } 171 172 for (p5 = -1; p5 <= 20; p5++) 173 { 174 fm[getout + p5] = im[getout + p5].Image; 175 } 176 177 178 179 180 181 182 Graphics g = e.Graphics; 183 g.DrawImage(WK, ICHI, 0, WK.Width + 265, WK.Height + 300); 184 185 186 187 188 189 190 191 192 193 194 195 196 //衝突 197 if (UI <= 16) 198 { 199 for (p7 = 0; p7 <= 3; p7++) 200 { 201 //1x4のテトリスブロックなので 202 GPS[IU + p7][UI] = 1; 203 204 205 if (UU[IU + p7][UI] != 2) 206 { 207 208 UU[IU + p7][UI] = GPS[IU + p7][UI] + GPS[IU + p7][UI + 1]; 209 210 if (UU[IU + p7][UI] != 2) 211 { 212 UU[IU + p7][UI] = 0; 213 if (UI!=16&&UU[IU + p7][UI]!= 2) 214 { 215 GPS[IU + p7][UI] = 0; 216 } 217 } 218 219 if (UU[IU + p7][UI] == 2 || UI == 16) 220 { 221 222 WW = 1; 223 } 224 225 } 226 } 227 } 228 229 230 231 if (WW == 1) 232 { 233 getout++; 234 235 //リセット 236 UI = 0; 237 IU = 0; 238 WW = 0; 239 } 240 241 for (i = 1; i <= getout; i++) 242 { 243 g.DrawImage(fm[i], ICHI + 40 + yoko[i], tate[i], fm[i].Width + 60, fm[i].Height + 9); 244 } 245 246 } 247 } 248 249 250 251 252 253 254 255class WAKU 256{ 257 private Image WK; 258 public WAKU() 259 { 260 WK = Image.FromFile("C:\\Users\\琉久\\Pictures\\TETRIS\\WAKU.png"); 261 262 } 263 public Image GetImage() 264 { 265 return WK; 266 } 267} 268

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

こんにちは。

そのケースの場合、fm, yoko, tateの中に未設定(nullのまま)の要素があるのだと思います。
yoko, tateはint[]型なのであり得ないですが、fmはクラスの配列ですのでnullがあり得ます。
fmの設計を見るとちょっとややこしいですので設定できていない要素があるのではないでしょうか?

getoutがどのように変化するのか分かりませんので断言できないのですが、getoutが2以上になるとfm[0]がnullのままになりそうにも見えます。

投稿2016/01/31 14:29

Chironian

総合スコア23272

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

LukeFukuda

2016/02/01 09:16

ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問