Eu modifiquei o GameTemplate do androDev e inseri nele o modo de entrada acelerometro.... não achei a versão que estava melhorada onde eu movia a imagem por acelerometro(lembro que a imagem meio que ficava engasgando....)
De qualquer forma planejo usar acelerômetro através do framework Rokon. Posto um exemplo aqui assim que der..(aproveitando hora do almoço). PS. Alguém sabe como colocar código fonte nestes blogs???? =p
Arquivo GameView.java
package br.com.sandclan.lordelfo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.hardware.SensorListener;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
@SuppressWarnings("deprecation")
class GameView extends SurfaceView implements SurfaceHolder.Callback {
class GameThread extends Thread implements SensorListener{
public static final int STATE_LOSE = 1;
public static final int STATE_PAUSE = 2;
public static final int STATE_READY = 3;
public static final int STATE_RUNNING = 4;
public static final int STATE_WIN = 5;
private float x;
private float y;
private static final int SPEED = 100;
private boolean dRight;
private boolean dLeft;
private boolean dUp;
private boolean dDown;
private int mCanvasWidth;
private int mCanvasHeight;
private long mLastTime;
private Bitmap mSandclan;
float sensorx;
float sensory;
float sensorz;
public void onSensorChanged(int sensor, float[] values) {
sensorx = values[0];
sensory = values[1];
sensorz = values[2];
}
public void onAccuracyChanged(int sensor, int accuracy) {
}
private Handler mHandler;
private int mMode;
private boolean mRun = false;
private SurfaceHolder mSurfaceHolder;
public GameThread(SurfaceHolder surfaceHolder, Context context,
Handler handler) {
mSurfaceHolder = surfaceHolder;
mHandler = handler;
mContext = context;
x = 10;
y = 10;
mSandclan = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.sandclan);
}
public void doStart() {
synchronized (mSurfaceHolder) {
x = 10;
y = 10;
mLastTime = System.currentTimeMillis() + 100;
setState(STATE_RUNNING);
}
}
public void pause() {
synchronized (mSurfaceHolder) {
if (mMode == STATE_RUNNING)
setState(STATE_PAUSE);
}
}
@Override
public void run() {
while (mRun) {
Canvas c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
if (mMode == STATE_RUNNING)
updateGame();
doDraw(c);
}
} finally {
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
public void setRunning(boolean b) {
mRun = b;
}
public void setState(int mode) {
synchronized (mSurfaceHolder) {
setState(mode, null);
}
}
public void setState(int mode, CharSequence message) {
synchronized (mSurfaceHolder) {
mMode = mode;
}
}
public void setSurfaceSize(int width, int height) {
synchronized (mSurfaceHolder) {
mCanvasWidth = width;
mCanvasHeight = height;
}
}
public void unpause() {
synchronized (mSurfaceHolder) {
mLastTime = System.currentTimeMillis() + 100;
}
setState(STATE_RUNNING);
}
boolean doKeyDown(int keyCode, KeyEvent msg) {
boolean handled = false;
synchronized (mSurfaceHolder) {
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
dRight = true;
handled = true;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
dLeft = true;
handled = true;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_UP){
dUp = true;
handled = true;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
dDown = true;
handled = true;
}
return handled;
}
}
boolean doKeyUp(int keyCode, KeyEvent msg) {
boolean handled = false;
synchronized (mSurfaceHolder) {
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
dRight = false;
handled = true;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
dLeft = false;
handled = true;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_UP){
dUp = false;
handled = true;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
dDown = false;
handled = true;
}
return handled;
}
}
private void doDraw(Canvas canvas) {
canvas.drawARGB(255, 128, 128, 128);
canvas.drawBitmap(mSandclan, x, y, new Paint());
canvas.drawText(Float.toString(sensorx), 20, 100, new Paint());
canvas.drawText(Float.toString(sensory), 20, 150, new Paint());
canvas.drawText(Float.toString(sensorz), 20, 200, new Paint());
if (sensorx < 0.00)
canvas.drawText("x<0", 20, 250, new Paint());
if (sensorx > 0.00)
canvas.drawText("x>0", 20, 250, new Paint());
if (sensory < 0.00)
canvas.drawText("y<0", 20, 300, new Paint());
if (sensory > 0.00)
canvas.drawText("y>0", 20, 300, new Paint());
if (sensorz < 0.00)
canvas.drawText("z<0", 20, 350, new Paint());
if (sensorz > 0.00)
canvas.drawText("z>0", 20, 350, new Paint());
}
private void updateGame() {
long now = System.currentTimeMillis();
if (mLastTime > now)
return;
double elapsed = (now - mLastTime) / 1000.0;
mLastTime = now;
if (dUp)
y -= elapsed * SPEED;
if (dDown)
y += elapsed * SPEED;
if (y < 0)
y = 0;
else if (y >= mCanvasHeight - mSandclan.getHeight())
y = mCanvasHeight - mSandclan.getHeight();
if (dLeft)
x -= elapsed * SPEED;
if (dRight)
x += elapsed * SPEED;
if (x < 0)
x = 0;
else if (x >= mCanvasWidth - mSandclan.getWidth())
x = mCanvasWidth - mSandclan.getWidth();
}
}
private Context mContext;
private GameThread thread;
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
thread = new GameThread(holder, context, new Handler() {
@Override
public void handleMessage(Message m) {
}
});
setFocusable(true);
}
public GameThread getThread() {
return thread;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
return thread.doKeyDown(keyCode, msg);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent msg) {
return thread.doKeyUp(keyCode, msg);
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
if (!hasWindowFocus)
thread.pause();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
thread.setSurfaceSize(width, height);
}
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
}
Arquivo GameTemplate.java
package br.com.sandclan.lordelfo;
import br.com.sandclan.lordelfo.GameView.GameThread;
import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
public class GameTemplate extends Activity {
private static final int MENU_PAUSE = Menu.FIRST;
private static final int MENU_RESUME = Menu.FIRST + 1;
private static final int MENU_START = Menu.FIRST + 2;
private static final int MENU_STOP = Menu.FIRST + 3;
private SensorManager mSensorManager;
private GameThread mGameThread;
private GameView mGameView;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_START, 0, R.string.menu_start);
menu.add(0, MENU_STOP, 0, R.string.menu_stop);
menu.add(0, MENU_PAUSE, 0, R.string.menu_pause);
menu.add(0, MENU_RESUME, 0, R.string.menu_resume);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_START:
mGameThread.doStart();
return true;
case MENU_STOP:
mGameThread.setState(GameThread.STATE_LOSE);
return true;
case MENU_PAUSE:
mGameThread.pause();
return true;
case MENU_RESUME:
mGameThread.unpause();
return true;
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mGameView = (GameView) findViewById(R.id.game);
mGameThread = mGameView.getThread();
mGameThread.setState(GameThread.STATE_READY);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(mGameThread, SensorManager.SENSOR_ACCELEROMETER);
}
@Override
protected void onPause() {
super.onPause();
mGameView.getThread().pause();
}
}
Nenhum comentário:
Postar um comentário