Replace your code in onCreate() with something like this:

    super.onCreate(savedInstanceState);
    GameView gv = new GameView(this);
    setContentView(gv);

    gv.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            GameView gv = (GameView)v;
            if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                gv.decrY(3);
                v.invalidate();
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                gv.decrX(3);
                v.invalidate();
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                gv.incrY(3);
                v.invalidate();
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                gv.incrX(3);
                v.invalidate();
            }
            return true;
        }
    });


You need to provide the incrX(), incrY(), decrX() and decrY()
functions in your GameView class.


--
Jack Ha
Open Source Development Center
・T・ ・ ・Mobile・ stick together

The views, opinions and statements in this email are those of
the author solely in their individual capacity, and do not
necessarily represent those of T-Mobile USA, Inc.



On Jul 21, 10:50 am, klirr <[email protected]> wrote:
> Thanks, that works. However it doesn't redraw when I press the keypad.
> Why not?
>
> This is what I have now:
>
> package com.android.WWS;
>
> import android.app.Activity;
> import android.content.Context;
> import android.graphics.*;
> import android.os.Bundle;
> import android.view.SurfaceView;
> import android.view.KeyEvent;
> import android.view.View;
> import android.view.View.OnKeyListener;
>
> public class WWS extends Activity {
>
>     @Override
>     protected void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(new GameView(this));
>     }
>
>     private static class GameView extends SurfaceView implements
> OnKeyListener {
>         private Paint mPaint = new Paint();
>         private int x;
>         private int y;
>
>         public GameView(Context context) {
>             super(context);
>             x = 135;
>             y = 303;
>             setWillNotDraw(false);
>             setFocusable(true);
>             requestFocus();
>         }
>
>         @Override
>         public void onDraw(Canvas canvas) {
>             Paint paint = mPaint;
>             canvas.translate(10, 10);
>             canvas.drawColor(Color.rgb(184,134,11));
>             paint.setColor(Color.rgb(107,142,35));
>             paint.setStrokeWidth(1);
>             canvas.drawRect(x, y, x+30, y+7, paint);
>             canvas.drawRect(x+10, y+7, x+20, y+27, paint);
>             canvas.drawRect(x+5, y+27, x+25, y+32, paint);
>         }
>
>         public boolean onKey(View v, int keyCode, KeyEvent event) {
>                 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
>                         y -= 3;
>                         invalidate();
>                 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
>                         x -= 3;
>                         invalidate();
>                 } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
>                         y += 3;
>                         invalidate();
>                 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
>                         x += 3;
>                         invalidate();
>                 }
>                 return true;
>         }
>
>     }
>
> }
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to