I am a novice in android. It's my first game, so I am trying to make a frogger clone. I am getting a problem in collision detection. In logcat, even though the frog is not moving and not colliding anywhere, I still get a "collision detected" message. Also, if the frog collides, it is not responding.
Please help me find the problem.
Here is an excerpt of my code:
Game class
private Frog frog;
private FrogControl fg;
private FrogControlY fg1;
private MainThread thread;
ImageView imgview = (ImageView) findViewById(R.id.imageview);
ImageView imageArr[] = new ImageView[20];
int id[] = { R.drawable.audi_r8, R.drawable.bike, R.drawable.bus,
R.drawable.car, R.drawable.chrysler, R.drawable.cruise_bike };
ArrayList<Vehicle> vh = new ArrayList<Vehicle>();
ArrayList<VehicleReverse> vhs = new ArrayList<VehicleReverse>();
public Game(Context context) {
super(context);
getHolder().addCallback(this);
frog = new Frog(BitmapFactory.decodeResource(getResources(),
R.drawable.frog), 100, 400);
fg = new FrogControl(BitmapFactory.decodeResource(getResources(),
R.drawable.arrow), 250, 400);
fg1 = new FrogControlY(BitmapFactory.decodeResource(getResources(),
R.drawable.arrow), 0, 400);
int noOfCar = (int) Math.random() * 5;
int totalCar = 3 + noOfCar;
for (int j = 0; j < totalCar; j++) {
int y = (int) (Math.random() * 3);
int[] LaneSelection = { 50, 150, 250 };
int yValue = LaneSelection[y];
int x = 0;
int frogX = frog.getX();
int frogY = frog.getY();
int frogW = fg.getX();
int frogH = fg.getY();
vh.add(new Vehicle(BitmapFactory.decodeResource(getResources(),
id[y]), x, yValue, frogX, frogY, frogW, frogH));
}
for (int k = totalCar - 1; k > 0; k--) {
int y = (int) (Math.random() * 3);
int[] LaneSelection = { 100, 200, 300 };
int yValue = LaneSelection[y];
int x = 300;
vhs.add(new VehicleReverse(BitmapFactory.decodeResource(
getResources(), id[y]), x, yValue));
}
thread = new MainThread(getHolder(), this);
setFocusable(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Nothing
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (Exception e) {
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// vh.carmove();
fg.handleAction((int) event.getX(), (int) event.getY());
fg1.handleActionY((int) event.getX(), (int) event.getY());
if (fg.isTouched()) {
// fg1.setTouchedY(false);
if ((int) event.getX() < (250 + (fg.getX() / 2))
&& (frog.getX() > 0)) {
frog.frogShiftLeft();
// fg1.setTouchedY(true);
}
if ((int) event.getX() >= (250 + (fg.getX() / 2))
&& (frog.getX() < 300)) {
frog.frogShiftRight();
}
}
if (fg1.isTouched()) {
// fg.setTouched(false);
if ((int) event.getY() < (380 + (fg1.getY() / 2))
&& (frog.getY() > 0) && ((int) event.getX() < 100)) {
frog.frogShiftUp();
// fg.setTouched(true);
}
if ((int) event.getY() >= (380 + (fg1.getY() / 2))
&& (frog.getY() < 400) && ((int) event.getX() < 100)) {
frog.frogShiftDown();
}
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
}
if (event.getAction() == MotionEvent.ACTION_UP) {
if (frog.isTouched()) {
frog.setTouched(false);
}
}
return true;
}
public void render(Canvas canvas) {
canvas.drawColor(Color.BLACK);
fg.draw(canvas);
fg1.draw(canvas);
frog.draw(canvas);
for (int j = 0; j < vh.size(); j++) {
Vehicle o = vh.get(j);
o.draw(canvas);
o.collision();
/*
* if(o.collision()){ //Frog f = new
* Frog(Bitmap.createBitmap(null)); //frog.remove(); /*Intent intent
* = new Intent(); startActivity(intent);
*/
// }
}
for (int k = vhs.size() - 1; k > 0; k--) {
VehicleReverse v = vhs.get(k);
v.draw(canvas);
}
}
}
vehicle class:
package com.froggergame;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;
public class Vehicle extends Thread implements Runnable{
private static final String TAG = null;
public Bitmap bmp;
public int x;
public int y;
Frog frog;
FrogControl froggy;
Vehicle vehicle;
int frogX,frogY;
int frogW, frogH;
public Vehicle(Bitmap bitmap,int x,int y,int frogX,int frogY, int frogW, int frogH){
this.bmp=bitmap;
this.x=x;
this.y=y;
this.frogX=frogX;
this.frogY=frogY;
this.frogH = frogH;
this.frogW = frogW;
}
public Bitmap getBmp() {
return bmp;
}
public void setBmp(Bitmap bmp) {
this.bmp = bmp;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public int getW(){
return bmp.getWidth();
}
public int getH() {
return bmp.getHeight();
}
public void setY(int y) {
this.y = y;
}
public void carmove(){
try {
System.out.println("carmove");
x = x + 5;
if(x>=300){
x=0;
}
} catch (Exception e) {
e.printStackTrace();
}
setX(x);
}
public boolean collision(){
System.out.println(getW());
System.out.println(getX());
System.out.println(getH());
System.out.println(getY());
System.out.println("Froggy x pos"+frogX);
System.out.println("Froggy width"+frogW);
System.out.println("Froggy y pos"+frogY);
System.out.println("Froggy height"+frogH);
if((getX()+getW()>=frogX)&&(getX()<frogX+frogW) && (getY()+getH()<=frogY)&&(getY()<frogY+frogH))
{
//frog = new Frog(Bitmap.createBitmap(null));
Log.v(TAG, "Collision Detected");
return true;
}else {
//Log.v(TAG, "Not collided");
return false;
}
}
public void draw(Canvas canvas){
canvas.drawBitmap(bmp, x, y,null);
carmove();
//collision();
}
}