テトリスのアプリを作成しています。
現在はパソコンのキーボードでブロックを操作しているのですが、キーボード操作ではなくボタン操作に変更したいと考えています。
ボタンを取り付け、onClickメソッドを記述して試してみたのですが、ボタンを押しても反応がありません。
以下ソースコードです
public class MainActivity extends AppCompatActivity { private class FieldView extends SurfaceView implements View.OnClickListener{ Random mRand = new Random(System.currentTimeMillis()); int[][][] blocks = { //省略 }; int[][] block = blocks[mRand.nextInt(blocks.length)]; int block1[][] = blocks[mRand.nextInt(blocks.length)]; int posx = 4, posy; int mapWidth = 10; int mapHeight = 20; int count = 0; int[][] map = new int[mapHeight][]; int[][] next = new int[mapHeight][]; public FieldView(Context context) { super(context); setBackgroundColor(0xFFFFFFFF); setFocusable(true); setFocusableInTouchMode(true); requestFocus(); } public void initGame() { //省略 盤面を作ったりの処理 } void gameover() { for (int i = 0; i < 10 ; i++) { if (map[0][i] != 0) { Log.d("i=","" +i); count = 1; } } } private void paintMatrix(Canvas canvas, int[][] matrix, int offsetx, int offsety, int color) { //省略 ブロックに形をつける } boolean check(int[][] block, int offsetx, int offsety) { //省略 ブロックが存在していいかどうか判別 } void mergeMatrix(int[][] block, int offsetx, int offsety) { //ブロックをマップ上に表示 } void mergeMatrix1(int[][] block, int offsetx, int offsety) { //次落ちてくるブロックを指定位置に表示 } void clearRows() { //一列そろったら消す処理 } @Override protected void onDraw(Canvas canvas) { //盤面などに色を付ける } int[][] rotate(final int[][] block) { //ブロックの回転処理 } @Override //ボタンを押したときの処理 public void onClick(View v) { switch (v.getId()) { case R.id.ch: Log.d("ch","push"); int[][] newBlock = rotate(block); if (check(newBlock, posx, posy)) { block = newBlock; } break; case R.id.right: if (check(block, posx + 1, posy)) { posx = posx + 1; } break; case R.id.left: if (check(block, posx - 1, posy)) { posx = posx - 1; } break; case R.id.end: int y = posy; while (check(block, posx, y)) { y++; } if (y > 0) posy = y - 1; break; case R.id.sita: if (check(block, posx, posy + 1)) { posy = posy + 1; } break; } mHandler.sendEmptyMessage(INVALIDATE); } public void startAnime() { mHandler.sendEmptyMessage(INVALIDATE); mHandler.sendEmptyMessage(DROPBLOCK); } public void stopAnime() { mHandler.removeMessages(INVALIDATE); mHandler.removeMessages(DROPBLOCK); } private static final int INVALIDATE = 1; private static final int DROPBLOCK = 2; //時間経過でブロックを落とす private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { if (count == 0) { switch (msg.what) { case INVALIDATE: invalidate(); break; case DROPBLOCK: if (check(block, posx, posy + 1)) { posy++; } else { mergeMatrix(block, posx, posy); clearRows(); posx = 4; posy = 0; block = block1; block1 = blocks[mRand.nextInt(blocks.length)]; mergeMatrix1(block1, 0, 0); } invalidate(); Message massage = new Message(); massage.what = DROPBLOCK; sendMessageDelayed(massage, 500); break; } } } }; } FieldView mFieldView; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.start); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mFieldView == null) { setContentView(R.layout.activity_main); mFieldView = new FieldView(getApplication()); LinearLayout lay = (LinearLayout) findViewById(R.id.lay); lay.addView(mFieldView); setFieldView(); mFieldView.initGame(); mFieldView.startAnime(); Looper.myQueue().addIdleHandler(new Idler()); } } }); } // Allow the activity to go idle before its animation starts class Idler implements MessageQueue.IdleHandler { public Idler() { super(); } public final boolean queueIdle() { return false; } } }
となっています。ボタンのidはそれぞれch、left等割り振っています。
ボタンを押しても動作しないのですがなぜでしょうか。
キーボード入力の時は普通に動作しました。
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_ENTER: int[][] newBlock = rotate(block); if (check(newBlock, posx, posy)) { block = newBlock; } break; case KeyEvent.KEYCODE_D: if (check(block, posx + 1, posy)) { posx = posx + 1; } break; case KeyEvent.KEYCODE_A: if (check(block, posx - 1, posy)) { posx = posx - 1; } break; case KeyEvent.KEYCODE_W: int y = posy; while (check(block, posx, y)) { y++; } if (y > 0) posy = y - 1; break; case KeyEvent.KEYCODE_S: if (check(block, posx, posy + 1)) { posy = posy + 1; } break; } mHandler.sendEmptyMessage(INVALIDATE); return true; }
onClickの部分を上記に変更するとキーボードで操作が可能でした。
なぜ動かないか教えていただけませんか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/13 01:23