Skip to main content
Improved layout and indenting. If I messed up some of it, do correct it!
Source Link

Collision Code snippet:
 

//Handles Collision
        private void CheckCollisions(){     
            synchronized(mSurfaceHolder){
                for (int i = sprites.size() - 1; i >= 0; i--){
                        Sprite sprite = sprites.get(i);
                        if(sprite.isCollision(bullet)){
                            sprites.remove(sprite);
                            mScore++;
                            if(sprites.size() == 0){
                                mLevel = mLevel +1;
                                currentLevel++;
                                initLevel();                            
                            }
                            break;
                        }           
                }
            }
        }


Sprite Class Code Snippet
//bounding box left<right and top>bottom
           int left ;
           int right ;
           int top ;
           int bottom ;
        public boolean isCollision(Beam other) {
            // TODO Auto-generated method stub  
            if(this.left>other.right || other.left<other.right)return false;
            if(this.bottom>other.top || other.bottom<other.top)return false;

            return true;
        }

EDIT1:
SpriteSprite Class Code Snippet:
public class Sprite {

       // direction = 0 up, 1 left, 2 down, 3 right,
       // animation = 3 back, 1 left, 0 front, 2 right
       int[] DIRECTION_TO_ANIMATION_MAP = { 3, 1, 0, 2 };
       private static final int BMP_ROWS = 4;
       private static final int BMP_COLUMNS = 3;
       private static final int MAX_SPEED = 5;
       private HitmanView gameView;
       private Bitmap bmp;
       private int x;
       private int y;
       private int xSpeed;
       private int ySpeed;
       private int currentFrame = 0;
       private int width;
       private int height;
       
       //bounding box left<right and top>bottom
       int left ;
       int right ;
       int top ;
       int bottom ;

       public Sprite(HitmanView gameView, Bitmap bmp) {
             this.width = bmp.getWidth() / BMP_COLUMNS;
             this.height = bmp.getHeight() / BMP_ROWS;
             this.gameView = gameView;
             this.bmp = bmp;
             Random rnd = new Random();
             x = rnd.nextInt(gameView.getWidth() - width);
             y = rnd.nextInt(gameView.getHeight() - height);
             xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
             ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
       }
       private void update() {
             if (x >= gameView.getWidth() - width - xSpeed || x + xSpeed <= 0) {
                    xSpeed = -xSpeed;
             }
             x = x + xSpeed;
             if (y >= gameView.getHeight() - height - ySpeed || y + ySpeed <= 0) {
                    ySpeed = -ySpeed;
             }
             y = y + ySpeed;
             currentFrame = ++currentFrame % BMP_COLUMNS;
       }
       public void onDraw(Canvas canvas) {
             update();
             int srcX = currentFrame * width;
             int srcY = getAnimationRow() * height;
             Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
             Rect dst = new Rect(x, y, x + width, y + height);
             canvas.drawBitmap(bmp, src, dst, null);
       }
       private int getAnimationRow() {
             double dirDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
             int direction = (int) Math.round(dirDouble) % BMP_ROWS;
             return DIRECTION_TO_ANIMATION_MAP[direction];
       }
       public boolean isCollision(float x2, float y2){
           return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
       }


    public boolean isCollision(Beam other) {
        // TODO Auto-generated method stub  
        if(this.left>other.right || other.left<other.right)return false;
        if(this.bottom>other.top || other.bottom<other.top)return false;

        return true;
    }   

}

EDIT 1:

BulletSprite Class:
public class Bullet {

public class Sprite {

   // direction = 0 up, 1 left, 2 down, 3 right,
   // animation = 3 back, 1 left, 0 front, 2 right
   int[] DIRECTION_TO_ANIMATION_MAP = { 3, 1, 0, 2 };
   private static final int BMP_ROWS = 4;
   private static final int BMP_COLUMNS = 3;
   private static final int MAX_SPEED = 5;
   private HitmanView gameView;
   private Bitmap bmp;
   private int x;
   private int y;
   private int xSpeed;
   private int ySpeed;
   private int currentFrame = 0;
   private int width;
   private int height;
   
   //bounding box left<right and top>bottom
   int left ;
   int right ;
   int top ;
   int bottom ;

   public Sprite(HitmanView gameView, Bitmap bmp) {
         this.width = bmp.getWidth() / BMP_COLUMNS;
         this.height = bmp.getHeight() / BMP_ROWS;
         this.gameView = gameView;
         this.bmp = bmp;
         Random rnd = new Random();
         x = rnd.nextInt(gameView.getWidth() - width);
         y = rnd.nextInt(gameView.getHeight() - height);
         xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
         ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
   }
   private void update() {
         if (x >= gameView.getWidth() - width - xSpeed || x + xSpeed <= 0) {
                xSpeed = -xSpeed;
         }
         x = x + xSpeed;
         if (y >= gameView.getHeight() - height - ySpeed || y + ySpeed <= 0) {
                ySpeed = -ySpeed;
         }
         y = y + ySpeed;
         currentFrame = ++currentFrame % BMP_COLUMNS;
   }
   public void onDraw(Canvas canvas) {
         update();
         int srcX = currentFrame * width;
         int srcY = getAnimationRow() * height;
         Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
         Rect dst = new Rect(x, y, x + width, y + height);
         canvas.drawBitmap(bmp, src, dst, null);
   }
   private int getAnimationRow() {
         double dirDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
         int direction = (int) Math.round(dirDouble) % BMP_ROWS;
         return DIRECTION_TO_ANIMATION_MAP[direction];
   }
   public boolean isCollision(float x2, float y2){
       return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
   }


    public boolean isCollision(Beam other) {
        // TODO Auto-generated method stub  
        if(this.left>other.right || other.left<other.right)return false;
        if(this.bottom>other.top || other.bottom<other.top)return false;

        return true;
    }   
}

Bullet Class:

public class Bullet {

    int mX;
    int mY;
    private Bitmap mBitmap;
    
    //bounding box left<right and top>bottom
    int left ;
    int right ;
    int top ;
    int bottom ;
    
    public Bullet (Bitmap mBitmap){
        this.mBitmap = mBitmap;
        
    }

    public void draw(Canvas canvas, int mX, int mY) {
        this.mX = mX;
        this.mY = mY;
        canvas.drawBitmap(mBitmap, mX, mY, null);
    }
}

Collision Code snippet:
 

//Handles Collision
        private void CheckCollisions(){     
            synchronized(mSurfaceHolder){
                for (int i = sprites.size() - 1; i >= 0; i--){
                        Sprite sprite = sprites.get(i);
                        if(sprite.isCollision(bullet)){
                            sprites.remove(sprite);
                            mScore++;
                            if(sprites.size() == 0){
                                mLevel = mLevel +1;
                                currentLevel++;
                                initLevel();                            
                            }
                            break;
                        }           
                }
            }
        }


Sprite Class Code Snippet
//bounding box left<right and top>bottom
           int left ;
           int right ;
           int top ;
           int bottom ;
        public boolean isCollision(Beam other) {
            // TODO Auto-generated method stub  
            if(this.left>other.right || other.left<other.right)return false;
            if(this.bottom>other.top || other.bottom<other.top)return false;

            return true;
        }

EDIT1:
Sprite Class:
public class Sprite {

       // direction = 0 up, 1 left, 2 down, 3 right,
       // animation = 3 back, 1 left, 0 front, 2 right
       int[] DIRECTION_TO_ANIMATION_MAP = { 3, 1, 0, 2 };
       private static final int BMP_ROWS = 4;
       private static final int BMP_COLUMNS = 3;
       private static final int MAX_SPEED = 5;
       private HitmanView gameView;
       private Bitmap bmp;
       private int x;
       private int y;
       private int xSpeed;
       private int ySpeed;
       private int currentFrame = 0;
       private int width;
       private int height;
       
       //bounding box left<right and top>bottom
       int left ;
       int right ;
       int top ;
       int bottom ;

       public Sprite(HitmanView gameView, Bitmap bmp) {
             this.width = bmp.getWidth() / BMP_COLUMNS;
             this.height = bmp.getHeight() / BMP_ROWS;
             this.gameView = gameView;
             this.bmp = bmp;
             Random rnd = new Random();
             x = rnd.nextInt(gameView.getWidth() - width);
             y = rnd.nextInt(gameView.getHeight() - height);
             xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
             ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
       }
       private void update() {
             if (x >= gameView.getWidth() - width - xSpeed || x + xSpeed <= 0) {
                    xSpeed = -xSpeed;
             }
             x = x + xSpeed;
             if (y >= gameView.getHeight() - height - ySpeed || y + ySpeed <= 0) {
                    ySpeed = -ySpeed;
             }
             y = y + ySpeed;
             currentFrame = ++currentFrame % BMP_COLUMNS;
       }
       public void onDraw(Canvas canvas) {
             update();
             int srcX = currentFrame * width;
             int srcY = getAnimationRow() * height;
             Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
             Rect dst = new Rect(x, y, x + width, y + height);
             canvas.drawBitmap(bmp, src, dst, null);
       }
       private int getAnimationRow() {
             double dirDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
             int direction = (int) Math.round(dirDouble) % BMP_ROWS;
             return DIRECTION_TO_ANIMATION_MAP[direction];
       }
       public boolean isCollision(float x2, float y2){
           return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
       }


    public boolean isCollision(Beam other) {
        // TODO Auto-generated method stub  
        if(this.left>other.right || other.left<other.right)return false;
        if(this.bottom>other.top || other.bottom<other.top)return false;

        return true;
    }   

}

Bullet Class:
public class Bullet {

int mX;
int mY;
private Bitmap mBitmap;

//bounding box left<right and top>bottom
int left ;
int right ;
int top ;
int bottom ;

public Bullet (Bitmap mBitmap){
    this.mBitmap = mBitmap;
    
}

public void draw(Canvas canvas, int mX, int mY) {
    this.mX = mX;
    this.mY = mY;
    canvas.drawBitmap(mBitmap, mX, mY, null);
}

Collision Code snippet:

//Handles Collision
private void CheckCollisions(){     
    synchronized(mSurfaceHolder){
        for (int i = sprites.size() - 1; i >= 0; i--){
            Sprite sprite = sprites.get(i);
            if(sprite.isCollision(bullet)){
                sprites.remove(sprite);
                mScore++;
                if(sprites.size() == 0){
                    mLevel = mLevel +1;
                    currentLevel++;
                    initLevel();                            
                }
                break;
            }           
        }
    }
}

Sprite Class Code Snippet:

//bounding box left<right and top>bottom
int left ;
int right ;
int top ;
int bottom ;
public boolean isCollision(Beam other) {
    // TODO Auto-generated method stub  
    if(this.left>other.right || other.left<other.right)return false;
    if(this.bottom>other.top || other.bottom<other.top)return false;

    return true;
}

EDIT 1:

Sprite Class:

public class Sprite {

   // direction = 0 up, 1 left, 2 down, 3 right,
   // animation = 3 back, 1 left, 0 front, 2 right
   int[] DIRECTION_TO_ANIMATION_MAP = { 3, 1, 0, 2 };
   private static final int BMP_ROWS = 4;
   private static final int BMP_COLUMNS = 3;
   private static final int MAX_SPEED = 5;
   private HitmanView gameView;
   private Bitmap bmp;
   private int x;
   private int y;
   private int xSpeed;
   private int ySpeed;
   private int currentFrame = 0;
   private int width;
   private int height;
   
   //bounding box left<right and top>bottom
   int left ;
   int right ;
   int top ;
   int bottom ;

   public Sprite(HitmanView gameView, Bitmap bmp) {
         this.width = bmp.getWidth() / BMP_COLUMNS;
         this.height = bmp.getHeight() / BMP_ROWS;
         this.gameView = gameView;
         this.bmp = bmp;
         Random rnd = new Random();
         x = rnd.nextInt(gameView.getWidth() - width);
         y = rnd.nextInt(gameView.getHeight() - height);
         xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
         ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
   }
   private void update() {
         if (x >= gameView.getWidth() - width - xSpeed || x + xSpeed <= 0) {
                xSpeed = -xSpeed;
         }
         x = x + xSpeed;
         if (y >= gameView.getHeight() - height - ySpeed || y + ySpeed <= 0) {
                ySpeed = -ySpeed;
         }
         y = y + ySpeed;
         currentFrame = ++currentFrame % BMP_COLUMNS;
   }
   public void onDraw(Canvas canvas) {
         update();
         int srcX = currentFrame * width;
         int srcY = getAnimationRow() * height;
         Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
         Rect dst = new Rect(x, y, x + width, y + height);
         canvas.drawBitmap(bmp, src, dst, null);
   }
   private int getAnimationRow() {
         double dirDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
         int direction = (int) Math.round(dirDouble) % BMP_ROWS;
         return DIRECTION_TO_ANIMATION_MAP[direction];
   }
   public boolean isCollision(float x2, float y2){
       return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
   }


    public boolean isCollision(Beam other) {
        // TODO Auto-generated method stub  
        if(this.left>other.right || other.left<other.right)return false;
        if(this.bottom>other.top || other.bottom<other.top)return false;

        return true;
    }   
}

Bullet Class:

public class Bullet {

    int mX;
    int mY;
    private Bitmap mBitmap;
    
    //bounding box left<right and top>bottom
    int left ;
    int right ;
    int top ;
    int bottom ;
    
    public Bullet (Bitmap mBitmap){
        this.mBitmap = mBitmap;
        
    }

    public void draw(Canvas canvas, int mX, int mY) {
        this.mX = mX;
        this.mY = mY;
        canvas.drawBitmap(mBitmap, mX, mY, null);
    }
}
added 3259 characters in body
Source Link
Manji
  • 79
  • 8

EDIT1:
Sprite Class:
public class Sprite {

       // direction = 0 up, 1 left, 2 down, 3 right,
       // animation = 3 back, 1 left, 0 front, 2 right
       int[] DIRECTION_TO_ANIMATION_MAP = { 3, 1, 0, 2 };
       private static final int BMP_ROWS = 4;
       private static final int BMP_COLUMNS = 3;
       private static final int MAX_SPEED = 5;
       private HitmanView gameView;
       private Bitmap bmp;
       private int x;
       private int y;
       private int xSpeed;
       private int ySpeed;
       private int currentFrame = 0;
       private int width;
       private int height;
       
       //bounding box left<right and top>bottom
       int left ;
       int right ;
       int top ;
       int bottom ;

       public Sprite(HitmanView gameView, Bitmap bmp) {
             this.width = bmp.getWidth() / BMP_COLUMNS;
             this.height = bmp.getHeight() / BMP_ROWS;
             this.gameView = gameView;
             this.bmp = bmp;
             Random rnd = new Random();
             x = rnd.nextInt(gameView.getWidth() - width);
             y = rnd.nextInt(gameView.getHeight() - height);
             xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
             ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
       }
       private void update() {
             if (x >= gameView.getWidth() - width - xSpeed || x + xSpeed <= 0) {
                    xSpeed = -xSpeed;
             }
             x = x + xSpeed;
             if (y >= gameView.getHeight() - height - ySpeed || y + ySpeed <= 0) {
                    ySpeed = -ySpeed;
             }
             y = y + ySpeed;
             currentFrame = ++currentFrame % BMP_COLUMNS;
       }
       public void onDraw(Canvas canvas) {
             update();
             int srcX = currentFrame * width;
             int srcY = getAnimationRow() * height;
             Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
             Rect dst = new Rect(x, y, x + width, y + height);
             canvas.drawBitmap(bmp, src, dst, null);
       }
       private int getAnimationRow() {
             double dirDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
             int direction = (int) Math.round(dirDouble) % BMP_ROWS;
             return DIRECTION_TO_ANIMATION_MAP[direction];
       }
       public boolean isCollision(float x2, float y2){
           return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
       }


    public boolean isCollision(Beam other) {
        // TODO Auto-generated method stub  
        if(this.left>other.right || other.left<other.right)return false;
        if(this.bottom>other.top || other.bottom<other.top)return false;

        return true;
    }   

}

Bullet Class:
public class Bullet {

int mX;
int mY;
private Bitmap mBitmap;

//bounding box left<right and top>bottom
int left ;
int right ;
int top ;
int bottom ;

public Bullet (Bitmap mBitmap){
    this.mBitmap = mBitmap;
    
}

public void draw(Canvas canvas, int mX, int mY) {
    this.mX = mX;
    this.mY = mY;
    canvas.drawBitmap(mBitmap, mX, mY, null);
}

EDIT1:
Sprite Class:
public class Sprite {

       // direction = 0 up, 1 left, 2 down, 3 right,
       // animation = 3 back, 1 left, 0 front, 2 right
       int[] DIRECTION_TO_ANIMATION_MAP = { 3, 1, 0, 2 };
       private static final int BMP_ROWS = 4;
       private static final int BMP_COLUMNS = 3;
       private static final int MAX_SPEED = 5;
       private HitmanView gameView;
       private Bitmap bmp;
       private int x;
       private int y;
       private int xSpeed;
       private int ySpeed;
       private int currentFrame = 0;
       private int width;
       private int height;
       
       //bounding box left<right and top>bottom
       int left ;
       int right ;
       int top ;
       int bottom ;

       public Sprite(HitmanView gameView, Bitmap bmp) {
             this.width = bmp.getWidth() / BMP_COLUMNS;
             this.height = bmp.getHeight() / BMP_ROWS;
             this.gameView = gameView;
             this.bmp = bmp;
             Random rnd = new Random();
             x = rnd.nextInt(gameView.getWidth() - width);
             y = rnd.nextInt(gameView.getHeight() - height);
             xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
             ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
       }
       private void update() {
             if (x >= gameView.getWidth() - width - xSpeed || x + xSpeed <= 0) {
                    xSpeed = -xSpeed;
             }
             x = x + xSpeed;
             if (y >= gameView.getHeight() - height - ySpeed || y + ySpeed <= 0) {
                    ySpeed = -ySpeed;
             }
             y = y + ySpeed;
             currentFrame = ++currentFrame % BMP_COLUMNS;
       }
       public void onDraw(Canvas canvas) {
             update();
             int srcX = currentFrame * width;
             int srcY = getAnimationRow() * height;
             Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
             Rect dst = new Rect(x, y, x + width, y + height);
             canvas.drawBitmap(bmp, src, dst, null);
       }
       private int getAnimationRow() {
             double dirDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
             int direction = (int) Math.round(dirDouble) % BMP_ROWS;
             return DIRECTION_TO_ANIMATION_MAP[direction];
       }
       public boolean isCollision(float x2, float y2){
           return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
       }


    public boolean isCollision(Beam other) {
        // TODO Auto-generated method stub  
        if(this.left>other.right || other.left<other.right)return false;
        if(this.bottom>other.top || other.bottom<other.top)return false;

        return true;
    }   

}

Bullet Class:
public class Bullet {

int mX;
int mY;
private Bitmap mBitmap;

//bounding box left<right and top>bottom
int left ;
int right ;
int top ;
int bottom ;

public Bullet (Bitmap mBitmap){
    this.mBitmap = mBitmap;
    
}

public void draw(Canvas canvas, int mX, int mY) {
    this.mX = mX;
    this.mY = mY;
    canvas.drawBitmap(mBitmap, mX, mY, null);
}
Tweeted twitter.com/#!/StackGameDev/status/86636666999148544
Fixed code formatting. Removed random links that were attached to nothing, fixed the rest of the formating, removed gratitudes and greeting
Source Link
AttackingHobo
  • 7.1k
  • 4
  • 38
  • 48

Hi I am making a game for android. I am having trouble with collision detection part of the game. 

I am using touch events to fire the gun as you will see in the video video. Note, the android icon is a temporary graphic for the bullets

When ever the user touches (represented by clicks in the video)the bullet appears and kills random sprites. 

As you can see it never touches the sprites it kills or kill the sprites it does touch.
My

My Question is howHow do I fix it, so that the sprite dies when the bullet hits it.
Any help is greatly appreciated and Thanks in advance.?

//Handles Collision
        private void CheckCollisions(){     
            synchronized(mSurfaceHolder){
                for (int i = sprites.size() - 1; i >= 0; i--){
                        Sprite sprite = sprites.get(i);
                        if(sprite.isCollision(bullet)){
                            sprites.remove(sprite);
                            mScore++;
                            if(sprites.size() == 0){
                                mLevel = mLevel +1;
                                currentLevel++;
                                initLevel();                            
                            }
                            break;
                        }           
                }
            }
        }


Sprite Class Code Snippet <br>
//bounding box left<right and top>bottom
           int left ;
           int right ;
           int top ;
           int bottom ;
        public boolean isCollision(Beam other) {
            // TODO Auto-generated method stub  
            if(this.left>other.right || other.left<other.right)return false;
            if(this.bottom>other.top || other.bottom<other.top)return false;

            return true;
        }

Side Note:The Icon image represents the bullet and yes I know the graphics are crappy but I am now concerned with them at all right now. I am more concerned about getting the codding right first.
[Game Play][3]

Hi I am making a game for android. I am having trouble with collision detection part of the game. I am using touch events to fire the gun as you will see in the video . When ever the user touches (represented by clicks in the video)the bullet appears and kills random sprites. As you can see it never touches the sprites it kills or kill the sprites it does touch.
My Question is how do I fix it, so that the sprite dies when the bullet hits it.
Any help is greatly appreciated and Thanks in advance.

//Handles Collision
        private void CheckCollisions(){     
            synchronized(mSurfaceHolder){
                for (int i = sprites.size() - 1; i >= 0; i--){
                        Sprite sprite = sprites.get(i);
                        if(sprite.isCollision(bullet)){
                            sprites.remove(sprite);
                            mScore++;
                            if(sprites.size() == 0){
                                mLevel = mLevel +1;
                                currentLevel++;
                                initLevel();                            
                            }
                            break;
                        }           
                }
            }
        }


Sprite Class Code Snippet <br>
//bounding box left<right and top>bottom
           int left ;
           int right ;
           int top ;
           int bottom ;
        public boolean isCollision(Beam other) {
            // TODO Auto-generated method stub  
            if(this.left>other.right || other.left<other.right)return false;
            if(this.bottom>other.top || other.bottom<other.top)return false;

            return true;
        }

Side Note:The Icon image represents the bullet and yes I know the graphics are crappy but I am now concerned with them at all right now. I am more concerned about getting the codding right first.
[Game Play][3]

I am having trouble with collision detection part of the game. 

I am using touch events to fire the gun as you will see in the video. Note, the android icon is a temporary graphic for the bullets

When ever the user touches (represented by clicks in the video)the bullet appears and kills random sprites. 

As you can see it never touches the sprites it kills or kill the sprites it does touch.

My Question is How do I fix it, so that the sprite dies when the bullet hits it?

//Handles Collision
        private void CheckCollisions(){     
            synchronized(mSurfaceHolder){
                for (int i = sprites.size() - 1; i >= 0; i--){
                        Sprite sprite = sprites.get(i);
                        if(sprite.isCollision(bullet)){
                            sprites.remove(sprite);
                            mScore++;
                            if(sprites.size() == 0){
                                mLevel = mLevel +1;
                                currentLevel++;
                                initLevel();                            
                            }
                            break;
                        }           
                }
            }
        }


Sprite Class Code Snippet
//bounding box left<right and top>bottom
           int left ;
           int right ;
           int top ;
           int bottom ;
        public boolean isCollision(Beam other) {
            // TODO Auto-generated method stub  
            if(this.left>other.right || other.left<other.right)return false;
            if(this.bottom>other.top || other.bottom<other.top)return false;

            return true;
        }
added 146 characters in body
Source Link
Manji
  • 79
  • 8
Loading
Source Link
Manji
  • 79
  • 8
Loading