Finally onKeyDown method in DrawView class is working :)
authorgumartinm <gu.martinm@gmail.com>
Tue, 15 Nov 2011 02:26:13 +0000 (03:26 +0100)
committergumartinm <gu.martinm@gmail.com>
Tue, 15 Nov 2011 02:26:13 +0000 (03:26 +0100)
Don't forget again to use the setFocusable methods. Apparently they
are very useful  :]

AndroidTetris/AndroidManifest.xml
AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java
AndroidTetris/src/de/android/androidtetris/DrawView.java
AndroidTetris/src/de/android/androidtetris/Piece.java

index edd8d88..046dc01 100644 (file)
@@ -7,7 +7,7 @@
 
     <application android:label="My AndroidTetris">
         <activity android:name=".AndroidTetrisActivity"
-                  android:theme="@android:style/Theme.NoTitleBar">
+                  android:theme="@android:style/Theme.NoTitleBar" android:configChanges="keyboard" android:screenOrientation="portrait">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
index 82b604f..bb85bf3 100644 (file)
@@ -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;
index d140432..9230a35 100644 (file)
@@ -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;
     }
     
index cbef91b..47718db 100644 (file)
@@ -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.