1.

Solve : Android Game?

Answer»

Hello all,

I've started on a new android application the other day, and i'm designing it with the tablet in mind. It's going to be a multiplayer game and the basic idea of it is to have a "player" object on each side of the SCREEN and both players will be able to move and rotate their character at will and shoot at each other.

I'm getting close to getting the basic functionality working in it, but I've stumbled into a roadblock. I can't figure out how to get both objects to rotate at the same time without affecting the one that isn't MEANT to rotate. In other words, if I rotate one object, the other one either isn't being displayed on the screen, or is being rotated around the screen too, if that makes any sense.

I've included the source below and all help is hugely appreciated! Thank you

Here is my main source file, AndroidDuelActivity.java
Code: [SELECT]package com.android.androidduel;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class AndroidDuelActivity extends Activity {

private Player player1;
private Player player2;

private float touchX;
private float touchY;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Panel(this));
}

class Panel extends SurfaceView implements SurfaceHolder.Callback {
private AndThread _thread;

public Panel(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getHolder().addCallback(this);
_thread = new AndThread(getHolder(),this);

player1 = new Player(50, 250, 250);
player2 = new Player(925, 250, 725);
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int 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) {
// ... again and again..and again!
}
}
}

public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
//canvas.restore();

Matrix matrix = new Matrix();
if (touchX < player1.getRLimit()) {
matrix.setRotate(player1.getRotationAngle(),
player1.getX()+player1.getImage().getWidth()/2,
player1.getY()+player1.getImage().getHeight()/2);

canvas.setMatrix(matrix);
//canvas.drawBitmap(player1.getImage(), player1.getX(), player1.getY(), null);
}

if (touchX > player2.getRLimit()) {
matrix.setRotate(player2.getRotationAngle(),
player2.getX()+player2.getImage().getWidth()/2,
player2.getY()+player2.getImage().getHeight()/2);

canvas.setMatrix(matrix);
//canvas.drawBitmap(player2.getImage(), player2.getX(), player2.getY(), null);
}

canvas.drawBitmap(player1.getImage(), player1.getX(), player1.getY(), null);
canvas.drawBitmap(player2.getImage(), player2.getX(), player2.getY(), null);

canvas.save();
canvas.setMatrix(null);
}

/*
* Get the touch coordinates to detect the angle
*/
public boolean onTouchEvent(MotionEvent event) {
touchX = event.getX();
touchY = event.getY();
if (touchX < player1.getRLimit())
Log.i("AndroidDuel", "Mouse detected at -- "+touchX+" "+touchY);
if (touchX > player2.getRLimit())
Log.i("AndroidDuel", "Mouse detected at -- "+touchX+" "+touchY);
return true;
}
}

/*
*
* Player class
*/
class Player {

private Bitmap image;
private int x;
private int y;
private int rLimit;

Player(int x, int y, int rLimit) {
image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
this.x = x;
this.y = y;
this.rLimit = rLimit;
}

public float getRotationAngle() {
float centerX = x+image.getWidth()/2;
float centerY = y+image.getHeight()/2;
float radians = (float) Math.atan2(centerX-touchX, centerY-touchY);
float degrees = (57.2957795f*radians) * -1;

return degrees;
}

public Bitmap getImage() { return this.image; }
public int getX() { return this.x; }
public int getY() { return this.y; }
public int getRLimit() { return this.rLimit; }
}
}
I'm also using a custom thread class, AndThread.java, but I didn't include this because I didn't think that it would help with my issue, but if anyone thinks that i should include it, just let me know so I can throw it up on here.You should move the drawing code into the player itself:

Code: [Select]public void draw(Graphics g){
Matrix cached = g.getMatrix();
canvas.setMatrix(matrix);
if (touchX < this.getRLimit())
matrix.setRotate(player1.getRotationAngle(),
getX()+getImage().getWidth()/2,
getY()+getImage().getHeight()/2);


canvas.drawBitmap(getImage(), getX(), getY(), null);
canvas.setMatrix(cached);
}


Basic idea is to save the initial rotation, rotate if NECESSARY, draw, and then reset it to what it was.Thank you for your post BC_Programmer. I don't know why I didn't think of that, actually. But I will surely give this a shot.



Discussion

No Comment Found