androidのブロック崩しを作っています。ブロック10段。
drawRectで描画したブロック(レンガ)とdrawCircleで描画したボールの衝突判定を行いたいです。
どちらもRectFを使って衝突判定します。
そこで、独自メソッドcollideWidth2のreturnで
RectF で作ったブロック配列の変数 rect_block, rect_block2(2段目),rect_block3(3段目)・・・の値を使いたいのですが、スコープの範囲外なのでローカル変数を作れ、と言われます。
どうなおせばよいでしょうか。
追記
if (rectCheck.intersects(rect_ball, rect_block)) { return true;}
の rect_block部分にエラーが出ています。
forループの中のrect_block が never used となっています。
警告内容
・cannot resolve symbol rect_block
collideWidth2のローカル変数 rect_blockを作れ
・RectF rect_block = null; としてrect_blockをforループの外に出せ
・rect_block を Blockクラスのフィールド変数にせよ
といった警告がでます。
public class Block { float x; // ブロックのx座標 float y; // ブロックのy座標 int width =40; int height=10; // ブロックの幅、高さ int margin = 5; //ブロックの間のマージン final int NUM_BLOCK = 8; //コンストラクタ public Block (int _x,int _y){ this.x = (float)_x; this.y = (float)_y; } public boolean collideWieth2(int view_w, int view_h, Ball _ball,Block[] _block, Block[] _block2, Block[] _block3, Block[] _block4, Block[] _block5, Block[] _block6, Block[] _block7, Block[] _block8, Block[] _block9, Block[] _block10) { RectF rectCheck = new RectF();//判定用 RectF rect_ball = new RectF(_ball.x - _ball.size, _ball.y - _ball.size, _ball.x + _ball.size, _ball.y + _ball.size);//当たり判定用のrect ball for(int i = 0;i< NUM_BLOCK ;i++ ) { _block[i] = new Block(view_w/NUM_BLOCK + (_block[i].width*2+ margin)*i, view_h/10);//blockのx座標とy座標 マージン分横にずらしている _block2[i] = new Block (view_w/NUM_BLOCK + (_block2[i].width*2+ margin)*i, view_h/10 + (_block2[i].height*2 + margin)); _block3[i] = new Block (view_w/NUM_BLOCK + (_block3[i].width*2+ margin)*i, view_h/10 + (_block3[i].height*2 + margin)*2); _block4[i] = new Block (view_w/NUM_BLOCK + (_block4[i].width*2+ margin)*i, view_h/10 + (_block4[i].height*2 + margin)*3); _block5[i] = new Block (view_w/NUM_BLOCK + (_block5[i].width*2+ margin)*i, view_h/10 + (_block5[i].height*2 + margin)*4); _block6[i] = new Block (view_w/NUM_BLOCK + (_block6[i].width*2+ margin)*i, view_h/10 + (_block6[i].height*2 + margin)*5); _block7[i] = new Block (view_w/NUM_BLOCK + (_block7[i].width*2+ margin)*i, view_h/10 + (_block7[i].height*2 + margin)*6); _block8[i] = new Block (view_w/NUM_BLOCK + (_block8[i].width*2+ margin)*i, view_h/10 + (_block8[i].height*2 + margin)*7); _block9[i] = new Block (view_w/NUM_BLOCK + (_block9[i].width*2+ margin)*i, view_h/10 + (_block9[i].height*2 + margin)*8); _block10[i] = new Block (view_w/NUM_BLOCK + (_block10[i].width*2+ margin)*i, view_h/10 + (_block10[i].height*2 + margin)*9); } for (int i = 0; i < NUM_BLOCK; i++) { RectF rect_block = new RectF(_block[i].x - _block[i].width, _block[i].y - _block[i].height, _block[i].x + _block[i].width, _block[i].y + _block[i].height); RectF rect_block2 = new RectF(_block2[i].x - _block2[i].width, _block2[i].y - _block2[i].height, _block2[i].x + _block2[i].width, _block2[i].y + _block2[i].height); RectF rect_block3 = new RectF(_block3[i].x - _block3[i].width, _block3[i].y - _block3[i].height, _block3[i].x + _block3[i].width, _block3[i].y + _block3[i].height); RectF rect_block4 = new RectF(_block4[i].x - _block4[i].width, _block4[i].y - _block4[i].height, _block4[i].x + _block4[i].width, _block4[i].y + _block4[i].height); RectF rect_block5 = new RectF(_block5[i].x - _block5[i].width, _block5[i].y - _block5[i].height, _block5[i].x + _block5[i].width, _block5[i].y + _block5[i].height); RectF rect_block6 = new RectF(_block6[i].x - _block6[i].width, _block6[i].y - _block6[i].height, _block6[i].x + _block6[i].width, _block6[i].y + _block6[i].height); RectF rect_block7 = new RectF(_block7[i].x - _block7[i].width, _block7[i].y - _block7[i].height, _block7[i].x + _block7[i].width, _block7[i].y + _block7[i].height); RectF rect_block8 = new RectF(_block8[i].x - _block8[i].width, _block8[i].y - _block8[i].height, _block8[i].x + _block8[i].width, _block8[i].y + _block8[i].height); RectF rect_block9 = new RectF(_block9[i].x - _block9[i].width, _block9[i].y - _block9[i].height, _block9[i].x + _block9[i].width, _block9[i].y + _block9[i].height); RectF rect_block10 = new RectF(_block10[i].x - _block10[i].width, _block10[i].y - _block10[i].height, _block10[i].x + _block10[i].width, _block10[i].y + _block10[i].height); } if (rectCheck.intersects(rect_ball,rect_block)) { return true; } else if (rectCheck.intersects(rect_ball,rect_block2)) { return true;} else if (rectCheck.intersects(rect_ball,rect_block3)) { return true;} else if (rectCheck.intersects(rect_ball,rect_block4)) { return true;} else if (rectCheck.intersects(rect_ball,rect_block5)) { return true;} else if (rectCheck.intersects(rect_ball,rect_block6)) { return true;} else if (rectCheck.intersects(rect_ball,rect_block7)) { return true;} else if (rectCheck.intersects(rect_ball,rect_block8)) { return true;} else if (rectCheck.intersects(rect_ball,rect_block9)) { return true;} else if (rectCheck.intersects(rect_ball,rect_block10)) { return true;} else {return false;} } }
回答1件
あなたの回答
tips
プレビュー