I'm somewhat working on a tile engine for a game I'm working on but I'm having issues with my bitmaps not drawing/canvas not being drawn on. Therefore I came here for help because I'm not really sure what to do as I am mostly new to android.
My code is shown below. Also any and all help will be appreciated.
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Update extends SurfaceView implements SurfaceHolder.Callback{
private MainThread thread;
private MapSetup mapSetup;
Bitmap red, green, blue;
public Update(Context context){
super(context);
getHolder().addCallback(this);
setFocusable(true);
red = BitmapFactory.decodeResource(getResources(), R.drawable.red);
blue = BitmapFactory.decodeResource(getResources(), R.drawable.blue);
green = BitmapFactory.decodeResource(getResources(), R.drawable.green);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
}
public void surfaceCreated(SurfaceHolder holder){
}
public void surfaceDestroyed(SurfaceHolder holder){
boolean retry = true;
while(retry){
try{
thread.join();
retry = false;
}catch(InterruptedException e){
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event){
return true;
}
protected void onDraw(Canvas canvas){
canvas.drawBitmap(red, 100, 100, null);
for(int x = 0; x <= mapSetup.getXTiles(); x++){
for(int y = 0; y <= mapSetup.getYTiles(); y++){
if(mapSetup.tileIds[x][y] == 0){
canvas.drawBitmap(red, 0, 0, null);
}
if(mapSetup.tileIds[x][y] == 1){
canvas.drawBitmap(green, 25, 25, null);
}
if(mapSetup.tileIds[x][y] == 2){
canvas.drawBitmap(blue, 50, 50, null);
}
}
}
}
public void update(){
}
}
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class MainThread extends Thread{
private SurfaceHolder surfaceHolder;
private Update update;
private boolean running;
public void setRunning(Boolean running){
this.running = running;
}
public MainThread(SurfaceHolder surfaceHolder, Update update){
super();
this.surfaceHolder = surfaceHolder;
this.update = update;
}
@Override
public void run(){
Canvas canvas;
while(running){
canvas = null;
try{
canvas = this.surfaceHolder.lockCanvas();
synchronized(surfaceHolder){
this.update.update();
this.update.onDraw(canvas);
}
}finally{
if(canvas != null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.app.Activity;
public class MainTileActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new Update(this));
}
public int getWidth(){
return getWindowManager().getDefaultDisplay().getWidth();
}
public int getHeight(){
return getWindowManager().getDefaultDisplay().getHeight();
}
}
import java.util.Random;
public class MapSetup {
private MainTileActivity mta;
Random rand = new Random();
int tileTypes = 2;
String[] tileNames;
int[][] tileIds;
public void setTileNames(){
for(int x = 0; x < tileTypes; x++){
tileNames[x] = x+".png";
}
}
public int getScreenWidth(){
return mta.getWidth();
}
public int getScreenHeight(){
return mta.getHeight();
}
public int getXTiles(){
return getScreenWidth()/25;
}
public int getYTiles(){
return getScreenHeight()/25;
}
public void setTiles(){
for(int x = 0; x <= getXTiles(); x++){
for(int y = 0; y <= getYTiles(); y++){
int id = rand.nextInt(3);
tileIds[x][y] = id;
}
}
}
}

New Topic/Question
Reply


MultiQuote




|