Stuck with Android conversion.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 4863 Views - Last Post: 30 April 2012 - 04:08 AM

#1 mrtoothpaste  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 26-April 12

Stuck with Android conversion.

Posted 26 April 2012 - 06:28 PM

Hello there, I have hit a brick wall when I have been attempting to convert my Java code into Android compatible Java. Everytime I try and load it, I get a critical error. There is a lot missing from it, such as controls but I would have thought that it would have compiled regardless.

Game.java
package com.android.skeleton;

import java.util.LinkedList;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceHolder;


public abstract class Game implements Runnable {

	private LinkedList<Sprite> spriteList;

	public LinkedList<Sprite> sprites(){
		return spriteList;
	}
	private LinkedList<Rect>clearList;
	private Thread gameThread;
	private Bitmap backgroundImage=null;
	Canvas canvas;
	SurfaceHolder surfaceHolder = null;
	Canvas backGraphics;
	
	public abstract void gameStart();

	public abstract void gameEnd();

	public abstract void gameUpdate(Canvas c);
	
	public abstract void spriteCollide(Sprite s1, Sprite s2);

	public void init(){
		spriteList = new LinkedList<Sprite>();
		clearList = new LinkedList<Rect>();
	}

	public void setDebugOn(){
		for(Sprite s: spriteList)
			s.showRectangle();
	}
	public void setDebugOff(){
		for(Sprite s: spriteList)
			s.hideRectangle();
	}
	
	public void gameAddSprite(Sprite s){
		spriteList.add(s);
	}
	
	public void gameAddSprites(Sprite []slist){
		for(Sprite sp: slist)
			spriteList.add(sp);
	}
	
	public void gameSetBackground(Bitmap i){
		backgroundImage = i;
		clearList.add(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()));
	}
	
	public void start(){
		gameThread	= new Thread(this);
		gameThread.start();
		gameStart();
	}
	
	public void stop(){
		gameEnd();
		gameThread = null;
	}
	
	public void update(Canvas c){
		for(Sprite s: spriteList){
			if(s.needsCleared()){
				
		}
		gameUpdate(backGraphics);
		// Now that we know what needs patched up, 
		// draw it all...
		paint(c);}
	}
	
	public void paint(Canvas c){
		if(backgroundImage != null) {
			for(Rect r: clearList){
				backGraphics.drawBitmap(backgroundImage, 
									   r.left, r.top, null);
			}
		} else {
			for(@SuppressWarnings("unused") Rect r: clearList){
			}
		}
		clearList.clear();
		int n = spriteList.size();
		for(int i=n-1; i>=0; i--){
			spriteList.get(i).draw(backGraphics);
		}
	}
	
	public void run() {
		while(gameThread == Thread.currentThread()){
			if(surfaceHolder.getSurface().isValid()){
			Canvas canvas = surfaceHolder.lockCanvas();
			gameCheckInputs();
			checkCollisions();
			paint(canvas);
			try{
				Thread.sleep(40);
			}
			catch(InterruptedException ex){
				ex.printStackTrace();
			}
		}
			surfaceHolder.unlockCanvasAndPost(canvas);
		}
	}

	private void checkCollisions(){
		if(spriteList.size() > 1){
			int nsprites = spriteList.size();
			for(int i=0; i<nsprites; i++) if(spriteList.get(i).isActive()){
				for(int j = i+1; j<nsprites; j++) if(spriteList.get(j).isActive()){
					if(spriteList.get(i).getRect().intersect(spriteList.get(j).getRect())){
						spriteCollide(spriteList.get(i), spriteList.get(j));
					}
				}
			}
		}			
	}
}



MyGame.java

package com.android.skeleton;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.provider.MediaStore.Audio;



public class MyGame extends Game{

	private Bitmap background;
	private Bitmap gameover;
	public final int PLAYER = 0;
	public final int ENEMY = 1;
	public final int EXIT = 2;
	public final int BIGENEMY = 3;
	
	private int health;
	
	//Sprites
	Sprite player;
	Enemy enemy1;
	Enemy enemy2;
	BigEnemy enemy3;
	Enemy enemy4;
	Enemy enemy5;
	Enemy enemy6;
	Exit exit;
	
	//Images
	Bitmap cat;
	Bitmap jellyfish;
	Bitmap bigdog;
	Bitmap exitTile;
	
	Audio music;
	Audio damageSound;

	
	Context context;
	InputStream inputStream = null;

	@Override
	public void gameStart() {
		// TODO Auto-generated method stub
		
		//Load images
		
		 try{
				AssetManager assetManager = context.getAssets();
				inputStream = assetManager.open("/resources/blackcat.png");
				cat = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				
				inputStream = assetManager.open("/resources/Jellyfish1.png");
				jellyfish = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				
				inputStream = assetManager.open("/resources/Dog.png");
				bigdog = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				
				inputStream = assetManager.open("/resources/exit.png");
				exitTile = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				
				inputStream = assetManager.open("/resources/kitchen.png");
				background = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				
				inputStream = assetManager.open("/resources/endscreen.png");
				gameover = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				}catch(IOException ex){}
		 
		 gameSetBackground(background);
		
		//Load sounds / music
		 
		//Create sprites
		player = new Sprite(cat, 0);
		enemy1 = new Enemy(jellyfish, 0);
		enemy2 = new Enemy(jellyfish, 0);
		enemy4 = new Enemy(jellyfish, 0);
		enemy5 = new Enemy(jellyfish, 0);
		enemy6 = new Enemy(jellyfish, 0);
		enemy3 = new BigEnemy(bigdog, 1.4);
	    exit = new Exit (exitTile, 0);
	    
	    		
		//Initialise sprite properties
		player.setLocation(320, 480);
		player.setType(0);
		player.setActive();
		health = 10;
		
		enemy1.setLocation(0, 160);
		enemy1.setVelocity(5, 0);
		enemy1.setType(1);
		enemy1.setActive();
		
		enemy2.setLocation(640, 320);
		enemy2.setVelocity(-5, 0);
		enemy2.setType(1);
		enemy2.setActive();
		
		Random random = new Random();
	 	Random random2 = new Random();
		int i = Math.abs(random.nextInt() % 10);
	 	int j = Math.abs(random2.nextInt() % 10);
	 	
	 	while (i < 2){
	 		i = Math.abs(random.nextInt() % 10);
	 	}
	 	while (j < 2){
	 		j = Math.abs(random.nextInt() % 10);
	 	}
		
		enemy4.setLocation(640, 0);
		enemy4.setVelocity(-i, j);
		enemy4.setType(1);
		enemy4.setActive();
		
		Random random3 = new Random();
	 	Random random4 = new Random();
		int k = Math.abs(random3.nextInt() % 10);
	 	int l = Math.abs(random4.nextInt() % 10);
	 	
	 	while (k < 2){
	 		k = Math.abs(random.nextInt() % 10);
	 	}
	 	while (l < 2){
	 		l = Math.abs(random.nextInt() % 10);
	 	}
		
		enemy5.setLocation(0, 480);
		enemy5.setVelocity(k, l);
		enemy5.setType(1);
		enemy5.setActive();
		
		enemy6.setLocation(600, 440);
		enemy6.setVelocity(0, -5);
		enemy6.setType(1);
		enemy6.setActive();
		
		exit.setLocation(600, 0);
		exit.setVelocity(0, 0);
		exit.setType(2);
		exit.setActive();
	
		//Add sprites to game
		gameAddSprite(player);
		gameAddSprite(enemy1);
		gameAddSprite(enemy2);
		gameAddSprite(enemy3);
		gameAddSprite(enemy4);
		gameAddSprite(enemy5);
		gameAddSprite(enemy6);
		gameAddSprite(exit);
		
		
		//Play music track
		
	}

	@Override
	public void gameEnd() {
		// TODO Auto-generated method stub
		player.setInactive();
		enemy1.setInactive();
		enemy2.setInactive();
		enemy3.setInactive();
		enemy4.setInactive();
		enemy5.setInactive();
		enemy6.setInactive();
		exit.setInactive();
		gameSetBackground(gameover);

	}

	@Override
	public void gameUpdate(Canvas c) {
		// TODO Auto-generated method stub
		player.update();
		
		enemy1.rotate(0.1);
		enemy1.update();
		
		enemy2.rotate(0.1);
		enemy2.update();
		
		enemy3.update();
		
		enemy4.rotate(0.1);
		enemy4.update();
		
		enemy5.rotate(0.1);
		enemy5.update();
		
		enemy6.rotate(0.2);
		enemy6.update();
		
		if(enemy3.getPosy() == 430){
			enemy3.setInactive();
		}
		
		if(health == 0){
			gameEnd();
		}
	}
	
	public void draw(Canvas c){
		
	}

	@Override
	public void spriteCollide(Sprite s1, Sprite s2) {
		// TODO Auto-generated method stub
	
		if(s2.getType() == ENEMY && s1.getType() == PLAYER)
		{
			health -= 1;
		}
		if(s2.getType() == BIGENEMY && s1.getType() == PLAYER)
		{
			health -= 1;
		}
		if (s2.getType() == EXIT && s1.getType() == PLAYER)
		{
			gameEnd();
		}
		if(s2.getType() == ENEMY && s1.getType() == ENEMY)
		{
			double i = s2.getVelx();
			double j = s2.getVely();
			double k = s1.getVelx();
			double l = s1.getVely();
			s2.setVelocity(-i, -j);
			s1.setVelocity(-k,-l);
		}
		

	}
	
	public void actionPerformed() {
		// TODO Auto-generated method stub
		
			//Create random spawn positions for bigdogs
			Random random = new Random();
		 	Random random2 = new Random();
			int xPos = Math.abs(random.nextInt() % 640);
		 	int yVel = Math.abs(random2.nextInt() % 20);
		 	
		 	//Make sure y velocity is between 10 and 30. Set this way for now
		 	//to hide problems with individual enemy3 sprites becoming inactive.
		 	while (yVel < 10)
		 		yVel = Math.abs(random.nextInt() % 30);
		 	
		 	//Spawn big dogs with random x position and y velocity
		 	//according to timer settings.
				enemy3.setLocation(xPos, 0);
				enemy3.setActive();
				enemy3.setVelocity(0, -yVel);
				enemy3.setType(3);
				
	}

	@Override
	public void gameCheckInputs() {
		// TODO Auto-generated method stub
		
	}
	
	}





package com.android.skeleton;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;


public class ScreenImage {

	// The sprite image...
	private Bitmap image;
	Context context;
	InputStream inputStream = null;
	
	public ScreenImage(String fileName){
		try{
		AssetManager assetManager = context.getAssets();
		inputStream = assetManager.open(fileName);
		image = BitmapFactory.decodeStream(inputStream);
		inputStream.close();
		}catch(IOException ex){}
	}
	
	public ScreenImage(Bitmap image){
		this.image = image;
	}

	public Bitmap getImage() {
		return image;
	}
	
	public int getWidth(){
		return image.getWidth();
	}
	
	public int getHeight(){
		return image.getHeight();
	}
	
	public void drawOn(Canvas c, int x, int y){
		c.drawBitmap(image, x, y, null);
	}
	
	public void drawOn(Canvas c, int x, int y, double angleInRads){

	}

	public void drawSegmentOn(Canvas c, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2){
	}
}



Sprite.java

package com.android.skeleton;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Sprite {
	private Bitmap bmp;
	private ScreenImage image;		// Bitmap used
	private double posx, posy;		// Where the sprite is drawn
	private int w, h;				// Size of bitmap in pixels
	private double velx, vely;		// Motion vector
	private Rect rect;			// Enclosing rectangle
	private boolean updated;
	private double rotation;		// Angle it is drawn at (radians)
	private double orientation;		// Correction for image angle (radians)
	private boolean lockAttitudeToMotion;
	private boolean showEnclosingRectangle=false;
	private boolean active=false;
	private boolean clearRect=false;	// Clear the area this sprite occupied.
	private int type=0;
	

	public Sprite(){
		posx=posy=0.0;
		velx=vely=0.0;
		rect = new Rect(0, 0, 0, 0);
		lockAttitudeToMotion = true;
	}

	public Sprite(Bitmap img, double orient){
		posx=posy=0;
		velx=vely=0;
		orientation = orient;
		image=new ScreenImage(img);
		w = image.getWidth();
		h = image.getHeight();
		rect = new Rect(0, 0, w, h);
		lockAttitudeToMotion = true;
	}

	public void setType(int type){
		this.type = type;
	}
	public int getType(){
		return type;
	}
	public boolean isUpdated(){
		return updated;
	}

	public void setAttitudeLock(){
		lockAttitudeToMotion = true;
	}
	public void clearAttitudeLock(){
		lockAttitudeToMotion = false;		
	}

	public void showRectangle(){
		showEnclosingRectangle = true;
	}
	public void hideRectangle(){
		showEnclosingRectangle = false;
	}

	public void setActive(){
		active = true;
	}
	public void setInactive(){
		clearRect = true;
		active = false;
	}
	public boolean isActive(){
		return active;
	}

	public void setToClear(){
		clearRect = true;
	}

	public boolean needsCleared(){
		boolean clr = clearRect;
		clearRect = false;
		return clr;
	}
	
	public double getPosx() {
		return posx;
	}
	public void setPosx(double posx) {
		this.posx = posx;
		updated = true;
	}
	public double getPosy() {
		return posy;
	}
	public void setPosy(double posy) {
		this.posy = posy;
		updated = true;
	}

	public void setLocation(double x, double y){
		setPosx(x);
		setPosy(y);
	}

	public void move(double dx, double dy){
		setPosx(getPosx()+dx);
		setPosy(getPosy()+dy);
	}

	public int centreX(){
		return (int)posx+w/2;
	}
	public int centreY(){
		return (int)posy+h/2;
	}
	
	protected ScreenImage getImage(){
		return image;
	}

	private void calcRect(){
		int hw = (int)(w*Math.abs(Math.cos(rotation+orientation))+h*Math.abs(Math.sin(rotation+orientation)))/2;
		int hh = (int)(h*Math.abs(Math.cos(rotation+orientation))+w*Math.abs(Math.sin(rotation+orientation)))/2;		
		rect.left = centreX() - hw;
		rect.bottom = centreY() - hh;
	}
	public Rect getRect(){
		calcRect();
		return rect;
	}
	
	/**
	 * How fast and in what direction is it moving?
	 * @return - vector components for x & y 
	 */
	public double getVelx() {
		return velx;
	}
	public void setVelx(double velx) {
		this.velx = velx;
		if(lockAttitudeToMotion)
			setRotation(getDirection());
	}
	public double getVely() {
		return vely;
	}
	public void setVely(double vely) {
		this.vely = vely;
		if(lockAttitudeToMotion)
			setRotation(getDirection());
	}
	public void setVelocity(double velx, double vely){
		setVelx(velx);
		setVely(vely);
	}

	public double getSpeed(){
		return Math.hypot(velx, vely);
	}
	public void setSpeed(double speed){
		// Current direction...
		double angle = Math.atan2(vely, velx);
		velx = speed * Math.cos(angle);
		vely = speed * Math.sin(angle);
	}

	public double getDirection(){
		return -Math.atan2(vely, velx);
	}
	public void setDirection(double angle){
		double speed = Math.hypot(velx, vely);
		velx = speed * Math.cos(angle);
		vely = -speed * Math.sin(angle);
		if(lockAttitudeToMotion)
			setRotation(angle);
	}
	public void turn(double angleInRads){
		double dir = getDirection();
		dir += angleInRads;
		setDirection(dir);
		if(lockAttitudeToMotion)
			setRotation(dir);
	}

	public double getRotation() {
		return rotation;
	}
	public void setRotation(double rotation) {
		this.rotation = rotation;
		updated = true;
	}
	public void rotate(double angleInRads){
		rotation += angleInRads;
		if(rotation<0) rotation += Math.PI*2;
		if(rotation>=Math.PI*2)rotation -= Math.PI*2;
		updated = true;
	}
	
	public void draw(Canvas canvas){
		if(!active) return;
		canvas.drawBitmap(bmp, (float)posx, (float)posy, null);
		setToClear();
		if(showEnclosingRectangle)
			drawRectangle(canvas);
	}

	private void drawRectangle(Canvas canvas){
		getRect();
		canvas.drawRect(rect.left+3, rect.bottom+3, rect.width()-6, rect.height()-6, null);
	}
	
	public void update(){
		if(!active) return;
		setPosx(getPosx()+velx);
		setPosy(getPosy()+vely);
		getRect();
		if(posx<0){
			setPosx(0);
			setVelx(-velx);
		}
		if(posy<0){
			setPosy(0);
			setVely(-vely);
		}
	}

	public String toString(){
		String s = String.format("Sprite: (%d,%d) @ (%5.2f,%5.2f)  Velocity: (%5.2f, %5.2f)  Rotation: %5.2f  Direction: %5.2f", w, h, posx, posy, velx, vely, Math.toDegrees(rotation+orientation), Math.toDegrees(getDirection()));
		return s;
	}
}



I've probably made quite a lot of mistakes in this as I'm not very familiar with Android. Any help would be massively appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Stuck with Android conversion.

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Stuck with Android conversion.

Posted 26 April 2012 - 06:56 PM

Moving to Android forums.
Was This Post Helpful? 0
  • +
  • -

#3 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 276
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Stuck with Android conversion.

Posted 26 April 2012 - 07:27 PM

Well first of all I dont see any Activities?! You should first look at how to create an android app before trying to convert an existing project! http://developer.and...ello-world.html
Was This Post Helpful? 2
  • +
  • -

#4 mrtoothpaste  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 26-April 12

Re: Stuck with Android conversion.

Posted 29 April 2012 - 12:46 PM

OK, I went away and made some changes to my code, the problem now being that it crashes when its loaded on the emulator and I have no idea how I can fix it.

Android_SkeletonActivity.java

 package com.android.skeleton;

import android.app.Activity;
import android.os.Bundle;

public class Android_SkeletonActivity extends Activity {
	
	GameSurfaceView view = null;

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		view = new GameSurfaceView(this);
		setContentView(view);
	}
	
	@Override
	protected void onpause(){
		super.onpause();
		view.pauseView();
	}
	
	@Override
	protected void onresume(){
		super.onresume();
		view.resumeView();
	}
}



Game.java

package com.android.skeleton;

import java.util.LinkedList;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceHolder;


public abstract class Game implements Runnable {

	private LinkedList<Sprite> spriteList;

	public LinkedList<Sprite> sprites(){
		return spriteList;
	}
	private LinkedList<Rect>clearList;
	private Thread gameThread;
	private Bitmap backgroundImage=null;
	Canvas canvas;
	SurfaceHolder surfaceHolder = null;
	Canvas backGraphics;
	
	public abstract void gameStart();

	public abstract void gameEnd();

	public abstract void gameUpdate(Canvas c);
	
	public abstract void spriteCollide(Sprite s1, Sprite s2);

	public void init(){
		spriteList = new LinkedList<Sprite>();
		clearList = new LinkedList<Rect>();
	}

	public void setDebugOn(){
		for(Sprite s: spriteList)
			s.showRectangle();
	}
	public void setDebugOff(){
		for(Sprite s: spriteList)
			s.hideRectangle();
	}
	
	public void gameAddSprite(Sprite s){
		spriteList.add(s);
	}
	
	public void gameAddSprites(Sprite []slist){
		for(Sprite sp: slist)
			spriteList.add(sp);
	}
	
	public void gameSetBackground(Bitmap i){
		backgroundImage = i;
		clearList.add(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()));
	}
	
	public void start(){
		gameThread	= new Thread(this);
		gameThread.start();
		gameStart();
	}
	
	public void stop(){
		gameEnd();
		gameThread = null;
	}
	
	public void update(Canvas c){
		for(Sprite s: spriteList){
			if(s.needsCleared()){
				
		}
		gameUpdate(backGraphics);
		paint(c);}
	}
	
	public void paint(Canvas c){
		if(backgroundImage != null) {
			for(Rect r: clearList){
				backGraphics.drawBitmap(backgroundImage, 
									   r.left, r.top, null);
			}
		} else {
			for(@SuppressWarnings("unused") Rect r: clearList){
			}
		}
		clearList.clear();
		int n = spriteList.size();
		for(int i=n-1; i>=0; i--){
			spriteList.get(i).draw(backGraphics);
		}
	}
	
	public void run() {
		while(gameThread == Thread.currentThread()){
			if(surfaceHolder.getSurface().isValid()){
			Canvas canvas = surfaceHolder.lockCanvas();
			gameCheckInputs();
			checkCollisions();
			paint(canvas);
			try{
				Thread.sleep(40);
			}
			catch(InterruptedException ex){
				ex.printStackTrace();
			}
		}
			surfaceHolder.unlockCanvasAndPost(canvas);
		}
	}

	private void checkCollisions(){
		if(spriteList.size() > 1){
			int nsprites = spriteList.size();
			for(int i=0; i<nsprites; i++) if(spriteList.get(i).isActive()){
				for(int j = i+1; j<nsprites; j++) if(spriteList.get(j).isActive()){
					if(spriteList.get(i).getRect().intersect(spriteList.get(j).getRect())){
						spriteCollide(spriteList.get(i), spriteList.get(j));
					}
				}
			}
		}			
	}

	public void gameCheckInputs() {
		// TODO Auto-generated method stub
		
	}
}



GameModel.java

package com.android.skeleton;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.media.SoundPool;

public class GameModel extends Game {
	private Bitmap background;
	private Bitmap gameover;
	public final int PLAYER = 0;
	public final int ENEMY = 1;
	public final int EXIT = 2;
	public final int BIGENEMY = 3;
	
	private int health;
	
	Sprite player;
	Enemy enemy1;
	Enemy enemy2;
	BigEnemy enemy3;
	Enemy enemy4;
	Enemy enemy5;
	Enemy enemy6;
	Exit exit;
	
	//Images
	Bitmap cat;
	Bitmap jellyfish;
	Bitmap bigdog;
	Bitmap exitTile;
	
	SoundPool music;
	SoundPool damageSound;
	
	Context context;
	InputStream inputStream = null;
	
	private static GameModel gameModel = null;
	public static GameModel getGameModel(){
		if(gameModel == null){
			gameModel = new GameModel();
		}
		return gameModel;
	}
	private GameModel(){

	}
	
	public void update(){

	}

	public void draw(Canvas canvas){

	}
	
	public boolean processTouchEvent(int x, int y){
		return false;
	}
	@Override
	public void gameStart() {
		// TODO Auto-generated method stub
		
		 try{
				AssetManager assetManager = context.getAssets();
				inputStream = assetManager.open("/resources/blackcat.png");
				cat = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				
				inputStream = assetManager.open("/resources/Jellyfish1.png");
				jellyfish = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				
				inputStream = assetManager.open("/resources/Dog.png");
				bigdog = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				
				inputStream = assetManager.open("/resources/exit.png");
				exitTile = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				
				inputStream = assetManager.open("/resources/kitchen.png");
				background = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				
				inputStream = assetManager.open("/resources/endscreen.png");
				gameover = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
				}catch(IOException ex){}
		 
		 gameSetBackground(background);
		
		player = new Sprite(cat, 0);
		enemy1 = new Enemy(jellyfish, 0);
		enemy2 = new Enemy(jellyfish, 0);
		enemy4 = new Enemy(jellyfish, 0);
		enemy5 = new Enemy(jellyfish, 0);
		enemy6 = new Enemy(jellyfish, 0);
		enemy3 = new BigEnemy(bigdog, 1.4);
	    exit = new Exit (exitTile, 0);
	    
		player.setLocation(320, 480);
		player.setType(0);
		player.setActive();
		health = 10;
		
		enemy1.setLocation(0, 160);
		enemy1.setVelocity(5, 0);
		enemy1.setType(1);
		enemy1.setActive();
		
		enemy2.setLocation(640, 320);
		enemy2.setVelocity(-5, 0);
		enemy2.setType(1);
		enemy2.setActive();
		
		Random random = new Random();
	 	Random random2 = new Random();
		int i = Math.abs(random.nextInt() % 10);
	 	int j = Math.abs(random2.nextInt() % 10);
	 	
	 	while (i < 2){
	 		i = Math.abs(random.nextInt() % 10);
	 	}
	 	while (j < 2){
	 		j = Math.abs(random.nextInt() % 10);
	 	}
		
		enemy4.setLocation(640, 0);
		enemy4.setVelocity(-i, j);
		enemy4.setType(1);
		enemy4.setActive();
		
		Random random3 = new Random();
	 	Random random4 = new Random();
		int k = Math.abs(random3.nextInt() % 10);
	 	int l = Math.abs(random4.nextInt() % 10);
	 	
	 	while (k < 2){
	 		k = Math.abs(random.nextInt() % 10);
	 	}
	 	while (l < 2){
	 		l = Math.abs(random.nextInt() % 10);
	 	}
		
		enemy5.setLocation(0, 480);
		enemy5.setVelocity(k, l);
		enemy5.setType(1);
		enemy5.setActive();
		
		enemy6.setLocation(600, 440);
		enemy6.setVelocity(0, -5);
		enemy6.setType(1);
		enemy6.setActive();
		
		exit.setLocation(600, 0);
		exit.setVelocity(0, 0);
		exit.setType(2);
		exit.setActive();
	
		//Add sprites to game
		gameAddSprite(player);
		gameAddSprite(enemy1);
		gameAddSprite(enemy2);
		gameAddSprite(enemy3);
		gameAddSprite(enemy4);
		gameAddSprite(enemy5);
		gameAddSprite(enemy6);
		gameAddSprite(exit);
		
	}
	@Override
	public void gameEnd() {
		// TODO Auto-generated method stub
		player.setInactive();
		enemy1.setInactive();
		enemy2.setInactive();
		enemy3.setInactive();
		enemy4.setInactive();
		enemy5.setInactive();
		enemy6.setInactive();
		exit.setInactive();
		gameSetBackground(gameover);
		
	}
	@Override
	public void gameUpdate(Canvas canvas) {
		// TODO Auto-generated method stub
		player.update();
		
		enemy1.rotate(0.1);
		enemy1.update();
		
		enemy2.rotate(0.1);
		enemy2.update();
		
		enemy3.update();
		
		enemy4.rotate(0.1);
		enemy4.update();
		
		enemy5.rotate(0.1);
		enemy5.update();
		
		enemy6.rotate(0.2);
		enemy6.update();
		
		if(enemy3.getPosy() == 430){
			enemy3.setInactive();
		}
		
		if(health == 0){
			gameEnd();
		}
	}
	@Override
	public void spriteCollide(Sprite s1, Sprite s2) {
		// TODO Auto-generated method stub
		
		if(s2.getType() == ENEMY && s1.getType() == PLAYER)
		{
			health -= 1;
		}
		if(s2.getType() == BIGENEMY && s1.getType() == PLAYER)
		{
			health -= 1;
		}
		if (s2.getType() == EXIT && s1.getType() == PLAYER)
		{
			gameEnd();
		}
		if(s2.getType() == ENEMY && s1.getType() == ENEMY)
		{
			double i = s2.getVelx();
			double j = s2.getVely();
			double k = s1.getVelx();
			double l = s1.getVely();
			s2.setVelocity(-i, -j);
			s1.setVelocity(-k,-l);
		}
	}
	
	public void actionPerformed() {
		// TODO Auto-generated method stub
		
			//Create random spawn positions for bigdogs
			Random random = new Random();
		 	Random random2 = new Random();
			int xPos = Math.abs(random.nextInt() % 640);
		 	int yVel = Math.abs(random2.nextInt() % 20);
		 	
		 	//Make sure y velocity is between 10 and 30. Set this way for now
		 	//to hide problems with individual enemy3 sprites becoming inactive.
		 	while (yVel < 10)
		 		yVel = Math.abs(random.nextInt() % 30);
		 	
		 	//Spawn big dogs with random x position and y velocity
		 	//according to timer settings.
				enemy3.setLocation(xPos, 0);
				enemy3.setActive();
				enemy3.setVelocity(0, -yVel);
				enemy3.setType(3);
				
	}

	@Override
	public void gameCheckInputs() {
		// TODO Auto-generated method stub
		
	}
	
}



GameSurfaceView.java
package com.android.skeleton;

import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

class GameSurfaceView extends SurfaceView implements Runnable, OnTouchListener{
	Thread thread = null;
	SurfaceHolder surfaceHolder = null;
	volatile boolean running = false;
	
	GameModel model = null;
	
	public GameSurfaceView(Context context) {
		super(context);
		surfaceHolder = getHolder();
		model = GameModel.getGameModel();
		setOnTouchListener(this);
		model.gameStart();
	}
	
	public void pauseView(){
		boolean retry = true;
		running = false;
		while(retry){
			try{
				thread.join();
				retry = false;
			} catch (InterruptedException ex){
				ex.printStackTrace();
			}
		}
	}
	public void resumeView(){
		running = true;
		thread = new Thread(this);
		thread.start();
	}
	
	public void run() {
		while(running){
			if(surfaceHolder.getSurface().isValid()){

				Canvas canvas = surfaceHolder.lockCanvas();

				canvas.drawARGB(255, 255, 255, 255);
				model.gameUpdate(canvas);
				model.draw(canvas);
				
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				surfaceHolder.unlockCanvasAndPost(canvas);
			}
		}
	}
	public boolean onTouch(View v, MotionEvent event) {
		return model.processTouchEvent((int)event.getX(), (int)event.getY());
	}
}



ScreenImage.java


package com.android.skeleton;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;


public class ScreenImage {

	private Bitmap image;
	Context context;
	InputStream inputStream = null;
	
	public ScreenImage(String fileName){
		try{
		AssetManager assetManager = context.getAssets();
		inputStream = assetManager.open(fileName);
		image = BitmapFactory.decodeStream(inputStream);
		inputStream.close();
		}catch(IOException ex){}
	}
	
	public ScreenImage(Bitmap image){
		this.image = image;
	}

	public Bitmap getImage() {
		return image;
	}
	
	public int getWidth(){
		return image.getWidth();
	}
	
	public int getHeight(){
		return image.getHeight();
	}
	
	public void drawOn(Canvas c, int x, int y){
		c.drawBitmap(image, x, y, null);
	}
	
	public void drawOn(Canvas c, int x, int y, double angleInRads){

	}

	public void drawSegmentOn(Canvas c, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2){
	}
}



Sprite.java


package com.android.skeleton;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Sprite {
	private Bitmap bmp;
	private ScreenImage image;		
	private double posx, posy;	
	private int w, h;				
	private double velx, vely;
	private Rect rect;
	private boolean updated;
	private double rotation;	
	private double orientation;	
	private boolean lockAttitudeToMotion;
	private boolean showEnclosingRectangle=false;
	private boolean active=false;
	private boolean clearRect=false;
	private int type=0;
	

	public Sprite(){
		posx=posy=0.0;
		velx=vely=0.0;
		rect = new Rect(0, 0, 0, 0);
		lockAttitudeToMotion = true;
	}

	public Sprite(Bitmap img, double orient){
		posx=posy=0;
		velx=vely=0;
		orientation = orient;
		image=new ScreenImage(img);
		w = image.getWidth();
		h = image.getHeight();
		rect = new Rect(0, 0, w, h);
		lockAttitudeToMotion = true;
	}

	public void setType(int type){
		this.type = type;
	}
	public int getType(){
		return type;
	}
	public boolean isUpdated(){
		return updated;
	}

	public void setAttitudeLock(){
		lockAttitudeToMotion = true;
	}
	public void clearAttitudeLock(){
		lockAttitudeToMotion = false;		
	}

	public void showRectangle(){
		showEnclosingRectangle = true;
	}
	public void hideRectangle(){
		showEnclosingRectangle = false;
	}

	public void setActive(){
		active = true;
	}
	public void setInactive(){
		clearRect = true;
		active = false;
	}
	public boolean isActive(){
		return active;
	}

	public void setToClear(){
		clearRect = true;
	}

	public boolean needsCleared(){
		boolean clr = clearRect;
		clearRect = false;
		return clr;
	}
	
	public double getPosx() {
		return posx;
	}
	public void setPosx(double posx) {
		this.posx = posx;
		updated = true;
	}
	public double getPosy() {
		return posy;
	}
	public void setPosy(double posy) {
		this.posy = posy;
		updated = true;
	}

	public void setLocation(double x, double y){
		setPosx(x);
		setPosy(y);
	}

	public void move(double dx, double dy){
		setPosx(getPosx()+dx);
		setPosy(getPosy()+dy);
	}

	public int centreX(){
		return (int)posx+w/2;
	}
	public int centreY(){
		return (int)posy+h/2;
	}
	
	protected ScreenImage getImage(){
		return image;
	}

	private void calcRect(){
		int hw = (int)(w*Math.abs(Math.cos(rotation+orientation))+h*Math.abs(Math.sin(rotation+orientation)))/2;
		int hh = (int)(h*Math.abs(Math.cos(rotation+orientation))+w*Math.abs(Math.sin(rotation+orientation)))/2;		
		rect.left = centreX() - hw;
		rect.bottom = centreY() - hh;
	}
	public Rect getRect(){
		calcRect();
		return rect;
	}
	

	public double getVelx() {
		return velx;
	}
	public void setVelx(double velx) {
		this.velx = velx;
		if(lockAttitudeToMotion)
			setRotation(getDirection());
	}
	public double getVely() {
		return vely;
	}
	public void setVely(double vely) {
		this.vely = vely;
		if(lockAttitudeToMotion)
			setRotation(getDirection());
	}
	public void setVelocity(double velx, double vely){
		setVelx(velx);
		setVely(vely);
	}

	public double getSpeed(){
		return Math.hypot(velx, vely);
	}
	public void setSpeed(double speed){
		double angle = Math.atan2(vely, velx);
		velx = speed * Math.cos(angle);
		vely = speed * Math.sin(angle);
	}

	public double getDirection(){
		return -Math.atan2(vely, velx);
	}
	public void setDirection(double angle){
		double speed = Math.hypot(velx, vely);
		velx = speed * Math.cos(angle);
		vely = -speed * Math.sin(angle);
		if(lockAttitudeToMotion)
			setRotation(angle);
	}
	public void turn(double angleInRads){
		double dir = getDirection();
		dir += angleInRads;
		setDirection(dir);
		if(lockAttitudeToMotion)
			setRotation(dir);
	}

	public double getRotation() {
		return rotation;
	}
	public void setRotation(double rotation) {
		this.rotation = rotation;
		updated = true;
	}
	public void rotate(double angleInRads){
		rotation += angleInRads;
		if(rotation<0) rotation += Math.PI*2;
		if(rotation>=Math.PI*2)rotation -= Math.PI*2;
		updated = true;
	}
	
	public void draw(Canvas canvas){
		if(!active) return;
		canvas.drawBitmap(bmp, (float)posx, (float)posy, null);
		setToClear();
		if(showEnclosingRectangle)
			drawRectangle(canvas);
	}

	private void drawRectangle(Canvas canvas){
		getRect();
		canvas.drawRect(rect.left+3, rect.bottom+3, rect.width()-6, rect.height()-6, null);
	}
	
	public void update(){
		if(!active) return;
		setPosx(getPosx()+velx);
		setPosy(getPosy()+vely);
		getRect();
		if(posx<0){
			setPosx(0);
			setVelx(-velx);
		}
		if(posy<0){
			setPosy(0);
			setVely(-vely);
		}
	}

	@Override
	public String toString(){
		String s = String.format("Sprite: (%d,%d) @ (%5.2f,%5.2f)  Velocity: (%5.2f, %5.2f)  Rotation: %5.2f  Direction: %5.2f", w, h, posx, posy, velx, vely, Math.toDegrees(rotation+orientation), Math.toDegrees(getDirection()));
		return s;
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Stuck with Android conversion.

Posted 29 April 2012 - 03:01 PM

What does your logcat say?
Was This Post Helpful? 0
  • +
  • -

#6 mrtoothpaste  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 26-April 12

Re: Stuck with Android conversion.

Posted 29 April 2012 - 04:19 PM

Logcat
04-29 23:16:07.369: W/dalvikvm(591): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-29 23:16:07.429: E/AndroidRuntime(591): FATAL EXCEPTION: main
04-29 23:16:07.429: E/AndroidRuntime(591): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.skeleton/com.android.skeleton.Android_SkeletonActivity}: java.lang.NullPointerException
04-29 23:16:07.429: E/AndroidRuntime(591): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at android.os.Looper.loop(Looper.java:137)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at android.app.ActivityThread.main(ActivityThread.java:4424)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at java.lang.reflect.Method.invokeNative(Native Method)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at java.lang.reflect.Method.invoke(Method.java:511)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at dalvik.system.NativeStart.main(Native Method)
04-29 23:16:07.429: E/AndroidRuntime(591): Caused by: java.lang.NullPointerException
04-29 23:16:07.429: E/AndroidRuntime(591): 	at com.android.skeleton.GameModel.gameStart(GameModel.java:72)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at com.android.skeleton.GameSurfaceView.<init>(GameSurfaceView.java:23)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at com.android.skeleton.Android_SkeletonActivity.onCreate(Android_SkeletonActivity.java:18)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at android.app.Activity.performCreate(Activity.java:4465)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-29 23:16:07.429: E/AndroidRuntime(591): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)



Looks like a problem in the GameModel gameStart but I can't see what it is.
Was This Post Helpful? 0
  • +
  • -

#7 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Stuck with Android conversion.

Posted 29 April 2012 - 04:44 PM

After just quickly looking through your code, and in GameModel.java i didn't where you set the context. You are trying to get the Assets from the context that's not set. What's why you are getting this error.

In GameModel.java, try changing gameStart() to gameStart(Context context)
and then in GameSurfaceView.java change model.gameStart(); to model.gameStart(this);
Was This Post Helpful? 1
  • +
  • -

#8 mrtoothpaste  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 26-April 12

Re: Stuck with Android conversion.

Posted 29 April 2012 - 04:57 PM

When I try that, I get the error:
The method gameStart(Context) in the type GameModel is not applicable for the arguments (GameSurfaceView)

I switched model.gameStart(this); with model.gameStart(context); and I now get the error:

04-29 23:53:21.619: W/dalvikvm(712): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-29 23:53:21.729: E/AndroidRuntime(712): FATAL EXCEPTION: main
04-29 23:53:21.729: E/AndroidRuntime(712): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.skeleton/com.android.skeleton.Android_SkeletonActivity}: java.lang.NullPointerException
04-29 23:53:21.729: E/AndroidRuntime(712): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at android.os.Looper.loop(Looper.java:137)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at android.app.ActivityThread.main(ActivityThread.java:4424)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at java.lang.reflect.Method.invokeNative(Native Method)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at java.lang.reflect.Method.invoke(Method.java:511)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at dalvik.system.NativeStart.main(Native Method)
04-29 23:53:21.729: E/AndroidRuntime(712): Caused by: java.lang.NullPointerException
04-29 23:53:21.729: E/AndroidRuntime(712): 	at com.android.skeleton.Game.gameSetBackground(Game.java:59)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at com.android.skeleton.GameModel.gameStart(GameModel.java:98)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at com.android.skeleton.GameSurfaceView.<init>(GameSurfaceView.java:23)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at com.android.skeleton.Android_SkeletonActivity.onCreate(Android_SkeletonActivity.java:18)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at android.app.Activity.performCreate(Activity.java:4465)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-29 23:53:21.729: E/AndroidRuntime(712): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)



So it looks like an error with the way I'm trying to set the background.
Was This Post Helpful? 0
  • +
  • -

#9 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Stuck with Android conversion.

Posted 29 April 2012 - 05:01 PM

Please post your current code for the Game.java
Was This Post Helpful? 0
  • +
  • -

#10 mrtoothpaste  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 26-April 12

Re: Stuck with Android conversion.

Posted 29 April 2012 - 05:04 PM


package com.android.skeleton;

import java.util.LinkedList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceHolder;


public abstract class Game implements Runnable {

	private LinkedList<Sprite> spriteList;

	public LinkedList<Sprite> sprites(){
		return spriteList;
	}
	private LinkedList<Rect>clearList;
	private Thread gameThread;
	private Bitmap backgroundImage=null;
	Canvas canvas;
	SurfaceHolder surfaceHolder = null;
	Canvas backGraphics;
	
	public abstract void gameStart();

	public abstract void gameEnd();

	public abstract void gameUpdate(Canvas c);
	
	public abstract void spriteCollide(Sprite s1, Sprite s2);

	public void init(){
		spriteList = new LinkedList<Sprite>();
		clearList = new LinkedList<Rect>();
	}

	public void setDebugOn(){
		for(Sprite s: spriteList)
			s.showRectangle();
	}
	public void setDebugOff(){
		for(Sprite s: spriteList)
			s.hideRectangle();
	}
	
	public void gameAddSprite(Sprite s){
		spriteList.add(s);
	}
	
	public void gameAddSprites(Sprite []slist){
		for(Sprite sp: slist)
			spriteList.add(sp);
	}
	
	public void gameSetBackground(Bitmap i){
		backgroundImage = i;
		clearList.add(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()));
	}
	
	public void start(){
		gameThread	= new Thread(this);
		gameThread.start();
		gameStart();
	}
	
	public void stop(){
		gameEnd();
		gameThread = null;
	}
	
	public void update(Canvas c){
		for(Sprite s: spriteList){
			if(s.needsCleared()){
				
		}
		gameUpdate(backGraphics);
		paint(c);}
	}
	
	public void paint(Canvas c){
		if(backgroundImage != null) {
			for(Rect r: clearList){
				backGraphics.drawBitmap(backgroundImage, 
									   r.left, r.top, null);
			}
		} else {
			for(@SuppressWarnings("unused") Rect r: clearList){
			}
		}
		clearList.clear();
		int n = spriteList.size();
		for(int i=n-1; i>=0; i--){
			spriteList.get(i).draw(backGraphics);
		}
	}
	
	public void run() {
		while(gameThread == Thread.currentThread()){
			if(surfaceHolder.getSurface().isValid()){
			Canvas canvas = surfaceHolder.lockCanvas();
			gameCheckInputs();
			checkCollisions();
			paint(canvas);
			try{
				Thread.sleep(40);
			}
			catch(InterruptedException ex){
				ex.printStackTrace();
			}
		}
			surfaceHolder.unlockCanvasAndPost(canvas);
		}
	}

	private void checkCollisions(){
		if(spriteList.size() > 1){
			int nsprites = spriteList.size();
			for(int i=0; i<nsprites; i++) if(spriteList.get(i).isActive()){
				for(int j = i+1; j<nsprites; j++) if(spriteList.get(j).isActive()){
					if(spriteList.get(i).getRect().intersect(spriteList.get(j).getRect())){
						spriteCollide(spriteList.get(i), spriteList.get(j));
					}
				}
			}
		}			
	}

	public void gameCheckInputs() {
		// TODO Auto-generated method stub
		
	}

	public void gameStart(Context context) {
		// TODO Auto-generated method stub
		
	}
}



Line 57 is the setbackground line.
Was This Post Helpful? 0
  • +
  • -

#11 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Stuck with Android conversion.

Posted 29 April 2012 - 05:08 PM

In the gameSetBackground void, try adding this before "clearList.add.."
if (clearList == null){
clearList = new LinkedList<Sprite>(); }

Was This Post Helpful? 1
  • +
  • -

#12 mrtoothpaste  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 26-April 12

Re: Stuck with Android conversion.

Posted 29 April 2012 - 05:18 PM

It's a Rect LinkedList but I tried your suggestion with Sprite replaced by Rect but still get the same error.

Thanks for all the help so far!
Was This Post Helpful? 0
  • +
  • -

#13 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Stuck with Android conversion.

Posted 29 April 2012 - 05:22 PM

Okay, sorry. My bad.
But i do not see where you set the value of, or initialize "canvas"
I see the variable and that you use it when you adds a rect to the LinkedList, but that's all i see you doing with it.
Was This Post Helpful? 0
  • +
  • -

#14 mrtoothpaste  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 26-April 12

Re: Stuck with Android conversion.

Posted 29 April 2012 - 05:27 PM

It's initialised on line 22.

Unless there's something else I should be doing with it?
Was This Post Helpful? 0
  • +
  • -

#15 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Stuck with Android conversion.

Posted 29 April 2012 - 05:28 PM

No, you're just declaring the variable on line 22.
You need to set the value of the canvas before you can use it. Right now, the value of the canvas is null

This post has been edited by EndLessMind: 29 April 2012 - 05:29 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3