From d2ffdb2f2b370f656b153283783d6d71932a5797 Mon Sep 17 00:00:00 2001 From: gumartinm Date: Wed, 16 Nov 2011 01:56:09 +0100 Subject: [PATCH] Retrieving the the key event and moving the blocks without trouble. TODO: To use java.concurrent instead of the "classic" Thread class. --- .../androidtetris/AndroidTetrisActivity.java | 1 - .../src/de/android/androidtetris/DrawView.java | 47 ++++++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java b/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java index bb85bf3..82b604f 100644 --- a/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java +++ b/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java @@ -3,7 +3,6 @@ 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 9230a35..9f7f1d5 100644 --- a/AndroidTetris/src/de/android/androidtetris/DrawView.java +++ b/AndroidTetris/src/de/android/androidtetris/DrawView.java @@ -50,20 +50,20 @@ public class DrawView extends SurfaceView { { while (running) { - Canvas c = view.getHolder().lockCanvas(); + try { + AndroidTetrisThread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } synchronized (view.getHolder()) { - try { - AndroidTetrisThread.sleep(1000); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + Canvas c = view.getHolder().lockCanvas(); view.move(0, 1); view.drawMap(c); //view.onDraw(c); - } - view.getHolder().unlockCanvasAndPost(c); + view.getHolder().unlockCanvasAndPost(c); + } } } } @@ -257,15 +257,36 @@ public class DrawView extends SurfaceView { super.onKeyDown(keyCode, msg); if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) { - move(-1,0); + synchronized (this.getHolder()) + { + Canvas c = this.getHolder().lockCanvas(); + this.move(-1, 0); + this.drawMap(c); + //view.onDraw(c); + this.getHolder().unlockCanvasAndPost(c); + } return(true); } if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { - this.move(1, 0); + synchronized (this.getHolder()) + { + Canvas c = this.getHolder().lockCanvas(); + this.move(1, 0); + this.drawMap(c); + //view.onDraw(c); + this.getHolder().unlockCanvasAndPost(c); + } return(true); } - if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { - move(0,1); + if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { + synchronized (this.getHolder()) + { + Canvas c = this.getHolder().lockCanvas(); + this.move(0,1); + this.drawMap(c); + //view.onDraw(c); + this.getHolder().unlockCanvasAndPost(c); + } return(true); } if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { -- 2.1.4