I found that too with the LunarLander model - especially when trying to develop a touch and drag style game. Do you know of any other example app/code that demonstrates a better method?
On Jul 22, 3:56 pm, Micah <mi...@ourmailbox.net> wrote: > Check out the LunarLander example that comes with the Android SDK. It > is a very simple game that runs in it's ownthread(separate from the > UIthread). While it's not my favorite design (too much locking) for > a large game, for most cell phone games (ie: simple) it gets the job > done. > > On Jul 22, 5:31 am, Streets Of Boston <flyingdutc...@gmail.com> wrote: > > > A seperatethreadshould not implement/extend SurfaceView or any other > >view. > > > You still create your GameView as you had before, but you add a new > > class called (for example) GameRenderer. > > This GameRenderer will implement aThread. > > > The GameRenderer will execute its 'run()' method at some point. This > > method should contain a 'while' loop, that only stops/pauses when the > > game is halting (e.g. halt when onPause is called on your activity; > > stop when onDestroy is called). > > > In the while loop, get hold of the GameView (actually; get hold of a > > SurfaceHolder from your GameView) lock it and obtain a Canvas from it > > when you're about to draw onto it. > > When done, unlock the canvas. > > > Search a little more on these forums and you'll find more info. > > > On Jul 22, 6:15 am, klirr <haskell...@gmail.com> wrote: > > > > I want to do the drawing in anotherthreadto speed up the game(it is > > > way to slow right now). I was told to do this but don't quite > > > understand why that would speed things up. > > > Is it GameView that should implement Runnable? > > > Should I make thethreadsleep when not drawing? > > > where should I start thethread? > > > > 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; > > > import java.lang.Runnable; > > > import java.lang.Thread; > > > > public class WWS extends Activity { > > > > @Override > > > protected void onCreate(Bundle savedInstanceState) { > > > super.onCreate(savedInstanceState); > > > GameView gv = new GameView(this); > > > setContentView(gv); > > > gv.setOnKeyListener(new OnKeyListener() { > > > public boolean onKey(Viewv, 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; > > > } > > > }); > > > } > > > > private static class GameView extends SurfaceView implements > > > Runnable { > > > 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(); > > > Threadt = newThread(this); > > > t.start(); > > > } > > > > public void run() {} > > > > @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 void decrY(int length) { > > > y -= length; > > > } > > > > public void incrY(int length) { > > > y += length; > > > } > > > > public void decrX(int length) { > > > x -= length; > > > } > > > > public void incrX(int length) { > > > x += length; > > > } > > > > } > > > > }- Hide quoted text - > > > > - Show quoted text - --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---