android遊戲開發二——圓的碰撞

來源:互聯網
上載者:User
碰撞是個很重要的內容,我這先簡單介紹一下。  CircleActivity代碼; package yy.circle;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.Window;import android.view.WindowManager;public class CircleActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        this.requestWindowFeature(Window.FEATURE_NO_TITLE);        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);        setContentView(new Circle(this));    }}  Circle代碼; package yy.circle;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.PaintFlagsDrawFilter;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.SurfaceHolder.Callback;public class Circle extends SurfaceView implements Callback,Runnable {        private SurfaceHolder sfh;        private Paint paint;        private Thread thread;        private boolean flag;        private Canvas canvas;        private int screenW,screenH;        //private Bitmap bmp;        private int x1=20,y1=20,r1=20;        private int x2=50,y2=50,r2=20;        private boolean isCollsion;        private PaintFlagsDrawFilter pfd=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);    public Circle(Context context){         super(context);         sfh=this.getHolder();         sfh.addCallback(this);         paint=new Paint();         paint.setColor(Color.WHITE);         paint.setAntiAlias(true);         //bmp=BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);         setFocusable(true);        }    public void surfaceCreated(SurfaceHolder holder){     screenW=this.getWidth();     screenH=this.getHeight();     flag=true;     thread=new Thread(this);     thread.start();    }    public void myDraw(){     try{      canvas=sfh.lockCanvas();      if(canvas!=null){      canvas.setDrawFilter(pfd);       canvas.drawColor(Color.BLACK);      if(isCollsion){       paint.setColor(Color.RED);       paint.setTextSize(20);       canvas.drawText("Collision!",0,30,paint);      }      else{paint.setColor(Color.WHITE);}            canvas.drawCircle(x1, y1 , r1, paint);      canvas.drawCircle(x2, y2, r2, paint);      //canvas.drawBitmap(bmp, 100, 100, paint);      }     }catch(Exception e){}     finally{      if(canvas!=null){       sfh.unlockCanvasAndPost(canvas);      }     }    }    public boolean onTouchEvent(MotionEvent event){     x1=(int)event.getX()-r1;     y1=(int)event.getY()-r1;     if(isCollsionWithCircle(x1,y1,r1,x2,y2,r2)){      isCollsion=true;     }else{      isCollsion=false;     }     return true;    }    private boolean isCollsionWithCircle(int x1, int y1, int r1, int x2,   int y2, int r2) {  /*if(x1>=x2&&x1>=x2+r2+r1){   return false;  }  else if(x1<=x2&&x1+r1+r2<=x2){   return false;  }  else if(y1>=y2&&y1>=y2+r2+r1){   return false;  }  else if(y1<=y2&&y1+r1+r2<=y2){   return false;  }*/     if((r1+r2)*(r1+r2)<=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)){      return false;     }       return true; } public boolean onKeyDown(int keyCode,KeyEvent event){     return super.onKeyDown(keyCode, event);    }    public void logic(){}    public void run(){     while(flag){      long start=System.currentTimeMillis();      myDraw();      logic();      long end=System.currentTimeMillis();      try{       if(end-start<50){        Thread.sleep(50-(end-start));       }      }catch (InterruptedException e) {    e.printStackTrace();   }     }    }    public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){}    public void surfaceDestroyed(SurfaceHolder holder){     flag=false;    }}  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.