android基本碰撞检测问题

时间:2015-01-08 05:46:28

标签: android collision-detection

我有一个非常基本的碰撞检测问题。我对android开发很新,刚刚开始游戏开发。无论如何,这里是其所有活动的来源。当球(球)接触中心球(敌球)时,想法是让比赛关闭。 但是当我开始游戏时,它说游戏意外停止了。下面是代码(忽略未使用的变量和屏幕大小,屏幕大小是我测试的设备的大小,以后会改变)

MainActivity.java

public class MainActivity extends Activity implements OnTouchListener {

OurView v;
Bitmap ball;
Bitmap enemy;
float x;
float y;
float enemyx;
float enemyy;
float screenH;
float screenW;
float centerball;
float centerenemy;
boolean colide=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    v=new OurView(this);
    v.setOnTouchListener(this);
    ball=BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
    enemy=BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
    x=y=0;
    enemyx=200;
    enemyy=300;
    screenW=768;    //just for the testing device
    screenH=1280;   //just for the testing device
    setContentView(v);
}

public class OurView extends SurfaceView implements Runnable{

    Thread t=null;
    SurfaceHolder holder;
    boolean isItOK=false;

    public OurView(Context context) {
        super(context);
        holder=getHolder();
    }

    @Override
    public void run(){
        while(isItOK=true){
            if(!holder.getSurface().isValid()){
                continue;
            }
            Canvas c=holder.lockCanvas();
            c.drawARGB(255, 255, 255, 255);
            centerenemy=enemyy+(enemy.getWidth()/2)+(enemy.getHeight()/2);
            if((int)(Math.sqrt(((x-enemyx)*(x-enemyx))+((y-enemyy)*(y-enemyy))))
<=ball.getHeight()){
                colide=true;
            }
            if(colide==true){
                Log.d("MainActivity", "colision");
            }else{
                continue;
            }
            c.drawBitmap(enemy,enemyx,enemyy,null);
            if(enemyx<=screenW){
                enemyx+=3;
            }else{
                enemyx=0;
            }
            c.drawBitmap(ball,x-(ball.getWidth()/2),y-(ball.getHeight())/2,null);

            holder.unlockCanvasAndPost(c);
        }
    }
    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();
    }
}

@Override
protected void onResume() {
    super.onResume();
    v.resume();
}
@Override
protected void onPause() {
    super.onPause();
    v.pause();
}
@Override
public boolean onTouch(View v, MotionEvent me) {
    try {
        Thread.sleep(17);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    //above statement not needed because the game is not taking
    //a lot of memory, when the game takes up more memory, add 
    //this to the code
    switch(me.getAction()){
    case MotionEvent.ACTION_DOWN:
        break;
    case MotionEvent.ACTION_UP:
        break;
    case MotionEvent.ACTION_MOVE:
        x=me.getX();
        y=me.getY();
        centerball=y+(ball.getWidth()/2)+(ball.getHeight()/2);
        break;
    }
    return true;
}

}

logcat的

01-08 16:11:43.499: D/dalvikvm(29434): Late-enabling CheckJNI
01-08 16:11:43.699: D/ActivityThread(29434): setTargetHeapUtilization:0.25
01-08 16:11:43.699: D/ActivityThread(29434): setTargetHeapIdealFree:8388608
01-08 16:11:43.699: D/ActivityThread(29434): setTargetHeapConcurrentStart:2097152
01-08 16:11:44.389: D/libEGL(29434): loaded /system/lib/egl/libEGL_adreno200.so
01-08 16:11:44.409: D/libEGL(29434): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
01-08 16:11:44.419: D/libEGL(29434): loaded /system/lib/egl/libGLESv2_adreno200.so
01-08 16:11:44.419: I/Adreno200-EGL(29434): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 
QUALCOMM build :AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107_msm8625_JB_REL_ 
2.0.3_CL3357771_release_AU (CL3357771)
01-08 16:11:44.419: I/Adreno200-EGL(29434): Build Date: 02/25/13 Mon
01-08 16:11:44.419: I/Adreno200-EGL(29434): Local Branch: 
01-08 16:11:44.419: I/Adreno200-EGL(29434): Remote Branch: quic/jb_rel_2.0.3
01-08 16:11:44.419: I/Adreno200-EGL(29434): Local Patches: NONE
01-08 16:11:44.419: I/Adreno200-EGL(29434): Reconstruct Branch:     
AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107 +  NOTHING
01-08 16:11:44.479: D/OpenGLRenderer(29434): Enabling debug mode 0
01-08 16:11:44.509: E/SurfaceTextureClient(29434): Surface::lock failed, already locked
01-08 16:11:44.519: E/SurfaceHolder(29434): Exception locking surface
01-08 16:11:44.519: E/SurfaceHolder(29434): java.lang.IllegalArgumentException
01-08 16:11:44.519: E/SurfaceHolder(29434):     at         
android.view.Surface.lockCanvasNative(Native Method)
01-08 16:11:44.519: E/SurfaceHolder(29434):     at     
android.view.Surface.lockCanvas(Surface.java:88)
01-08 16:11:44.519: E/SurfaceHolder(29434):     at 
android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:842)
01-08 16:11:44.519: E/SurfaceHolder(29434):     at     
android.view.SurfaceView$4.lockCanvas(SurfaceView.java:818)
01-08 16:11:44.519: E/SurfaceHolder(29434):     at  
com.example.newgame1.MainActivity$OurView.run(MainActivity.java:63)
01-08 16:11:44.519: E/SurfaceHolder(29434):     at 
java.lang.Thread.run(Thread.java:856)
01-08 16:11:44.609: W/dalvikvm(29434): threadid=11: thread exiting with uncaught 
exception (group=0x41bdf438)
01-08 16:11:44.609: E/AndroidRuntime(29434): FATAL EXCEPTION: Thread-8162
01-08 16:11:44.609: E/AndroidRuntime(29434): java.lang.NullPointerException
01-08 16:11:44.609: E/AndroidRuntime(29434):    at     
com.example.newgame1.MainActivity$OurView.run(MainActivity.java:64)
01-08 16:11:44.609: E/AndroidRuntime(29434):    at 
java.lang.Thread.run(Thread.java:856)

thnx提前帮助

0 个答案:

没有答案