collision detection between multiple sprite and the touchlistner object which is controlled by user
<p>SurfaceViewexample Class implements OnTouchListner and where the rectangle image will move based on user touch.And  in OurView class multiple sprite ll be drawed and i need to find collison between the rectangular image and the multiple sprite.and i tried some thing which s not working.Sprite1 class used to animate the given bitmap by dividing it with 4rows and 3 cols.and update the position.</p>

<p>//code of SrfaceView example</p>

<pre><code>  import android.app.Activity;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.os.Bundle;
  import android.view.MotionEvent;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.view.View;
  import android.view.View.OnTouchListener;

  public class SurfaceViewExample extends Activity implements OnTouchListener{

 OurView v;
 Bitmap ball,maddy;
 float x,y;
 Sprite1 sprite,sprite1,sprite2;
 boolean spriteLoaded =false;
private Bitmap maddy2;
private Bitmap maddy3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    v=new OurView(this);
    v.setOnTouchListener(this);
    ball=BitmapFactory.decodeResource(getResources(),R.drawable.oee1);
    maddy=BitmapFactory.decodeResource(getResources(),R.drawable.mad7);
    maddy2=BitmapFactory.decodeResource(getResources(),R.drawable.mad7);
    maddy3=BitmapFactory.decodeResource(getResources(),R.drawable.mad7);
    x = y =0;
    setContentView(v);
}

     @Override
 protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    v.pause();
 }

    @Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    v.resume();
}
   public class OurView extends SurfaceView implements Runnable{
     Thread t=null;
     SurfaceHolder holder;
     boolean isItOk=false;
     Sprite1 sprite,sprite1,sprite2;
 boolean spriteLoaded=false;
public OurView(Context context) {
        super(context);
    // TODO Auto-generated constructor stub
    holder=getHolder();

}


    // TODO Auto-generated method stub

//   private Sprite createSprite(int resouce) {
       //Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
      // return new Sprite(this, bmp);
 //}
public void run() {

    // TODO Auto-generated method stub

    while( isItOk ==true)
    {
     //drawing   
     if(!holder.getSurface().isValid()) {

        continue;

     }

     //ss.add(createSprite(R.drawable.mad7));
     //ss.add(createSprite(R.drawable.mad7));
     if(!spriteLoaded)
     {
         sprite=new Sprite1(OurView.this,maddy);
         sprite1=new Sprite1(OurView.this,maddy2);
         sprite2=new Sprite1(OurView.this,maddy3);
         spriteLoaded=true;
     }
     Canvas c=holder.lockCanvas();
     onDraw(c);
     holder.unlockCanvasAndPost(c);

    }
 }
   protected void onDraw(Canvas canvas)
{   
    canvas.drawARGB(250,150,150,10);    
    canvas.drawBitmap(ball, x+3, y+3, null);
    sprite.onDraw(canvas);
    sprite1.onDraw(canvas);
    sprite2.onDraw(canvas);


}
public void pause()
{
    isItOk=false;
    while(true) {
        try {
            t.join();
        }catch(InterruptedException e) {

            e.printStackTrace();

        }
        break;

    }
   t=null;
}

 public void resume()
  {
   isItOk=true;  
   t=new Thread(this);
   t.start();
 }

  }
   public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

x=me.getX();
y=me.getY();
switch(me.getAction()){
 case MotionEvent.ACTION_DOWN:
        x=me.getX();
        y=me.getY();
    break;
 case  MotionEvent.ACTION_UP:
     x=me.getX();
     y=me.getY();
     break;

 case MotionEvent.ACTION_MOVE:
     x=me.getX();
     y=me.getY();
     break;

}  
</code></pre>

<p>//collision detection condition                                                              if(sprite.isCollition(me.getX(),me.getY())||sprite2.isCollition(me.getX(),me.getY()))<br>
      { 
     try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
         //TODO Auto-generated catch block
        e.printStackTrace();
    }
    }</p>

<pre><code> return true;
    }


  } 
</code></pre>

<p>//Sprite1 class code:where collisondetection function exist</p>

<pre><code>  import java.util.Random;
  import madhu.aloor.aloor.SurfaceViewExample.OurView;
  import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.graphics.Rect;

  public class Sprite1 {
   // 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 OurView ourView;
   private Bitmap maddy;
   private int x = 0;
   private int y = 0;
   private int xSpeed;
   private int ySpeed;
   private int currentFrame = 0;
   private int width;
   private int height;

   public Sprite1(OurView ourView, Bitmap maddy) {
         this.width = maddy.getWidth() / BMP_COLUMNS;
         this.height = maddy.getHeight() / BMP_ROWS;
         this.ourView = ourView;
         this.maddy =maddy;

         Random rnd = new Random();
         x = rnd.nextInt(ourView.getWidth() - width);
         y = rnd.nextInt(ourView.getHeight() - height);
         xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
         ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
   }

   private void update() {
         if (x &gt;= ourView.getWidth() - width - xSpeed || x + xSpeed &lt;= 0) {
                xSpeed = -xSpeed;
         }
         x = x + xSpeed;
         if (y &gt;= ourView.getHeight() - height - ySpeed || y + ySpeed &lt;= 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(maddy, 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 isCollition(float x2, float y2) {
             return( (x2 &gt; x &amp;&amp; x2 &lt; x + width &amp;&amp; y2 &gt; y &amp;&amp; y2 &lt; y + height));
   }
 }
</code></pre>
