From 0d58339c6b77dce869419887c6375613941e93f5 Mon Sep 17 00:00:00 2001 From: gumartinm Date: Tue, 15 Nov 2011 03:26:13 +0100 Subject: [PATCH] Finally onKeyDown method in DrawView class is working :) Don't forget again to use the setFocusable methods. Apparently they are very useful :] --- AndroidTetris/AndroidManifest.xml | 2 +- .../androidtetris/AndroidTetrisActivity.java | 1 + .../src/de/android/androidtetris/DrawView.java | 53 ++++++++++++++-------- .../src/de/android/androidtetris/Piece.java | 2 +- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/AndroidTetris/AndroidManifest.xml b/AndroidTetris/AndroidManifest.xml index edd8d88..046dc01 100644 --- a/AndroidTetris/AndroidManifest.xml +++ b/AndroidTetris/AndroidManifest.xml @@ -7,7 +7,7 @@ + android:theme="@android:style/Theme.NoTitleBar" android:configChanges="keyboard" android:screenOrientation="portrait"> diff --git a/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java b/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java index 82b604f..bb85bf3 100644 --- a/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java +++ b/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java @@ -3,6 +3,7 @@ package de.android.androidtetris; import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; +import android.view.KeyEvent; public class AndroidTetrisActivity extends Activity { DrawView drawView; diff --git a/AndroidTetris/src/de/android/androidtetris/DrawView.java b/AndroidTetris/src/de/android/androidtetris/DrawView.java index d140432..9230a35 100644 --- a/AndroidTetris/src/de/android/androidtetris/DrawView.java +++ b/AndroidTetris/src/de/android/androidtetris/DrawView.java @@ -53,6 +53,12 @@ public class DrawView extends SurfaceView { Canvas c = view.getHolder().lockCanvas(); synchronized (view.getHolder()) { + try { + AndroidTetrisThread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } view.move(0, 1); view.drawMap(c); //view.onDraw(c); @@ -75,8 +81,16 @@ public class DrawView extends SurfaceView { public DrawView(Context context) { super(context); - - this.newGame(); + + //I have so much to learn... + //The OnKeyListener for a specific View will only be called if the key is pressed + //while that View has focus. For a generic SurfaceView to be focused it first needs to be focusable + //http://stackoverflow.com/questions/975918/processing-events-in-surfaceview + setFocusableInTouchMode(true); + setFocusable(true); + + + this.newGame(); currentPiece = newBlock(); currentPiece.x = MAPWIDTH/2-2; currentPiece.y = -1; @@ -236,27 +250,28 @@ public class DrawView extends SurfaceView { return true; return false; } - + @Override public boolean onKeyDown(int keyCode, KeyEvent msg) { + super.onKeyDown(keyCode, msg); + + if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) { + move(-1,0); + return(true); + } if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { - try { - AndroidTetrisThread.sleep(1000000); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - Canvas c =null; - c = this.getHolder().lockCanvas(); - synchronized (this.getHolder()) - { - this.move(1, 0); - this.drawMap(c); - //view.onDraw(c); - } - return true; - } + this.move(1, 0); + return(true); + } + if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { + move(0,1); + return(true); + } + if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { + return(true); + } + return false; } diff --git a/AndroidTetris/src/de/android/androidtetris/Piece.java b/AndroidTetris/src/de/android/androidtetris/Piece.java index cbef91b..47718db 100644 --- a/AndroidTetris/src/de/android/androidtetris/Piece.java +++ b/AndroidTetris/src/de/android/androidtetris/Piece.java @@ -122,7 +122,7 @@ public enum Piece { //Pre-Initialization of matrix size for (int i=0; i< WIDTH; i++) - for (int j=0; j< WIDTH; j++) + for (int j=0; j< HEIGHT; j++) size[i][j]= Tile.NOCOLOR; //It depends on what kind of piece, we have to fill the square in the right way. -- 2.1.4