两个Imageview的Android碰撞

时间:2015-01-30 16:22:27

标签: android collision

我是Android新手,想创建一个游戏,其中我有两个,在屏幕底部移动玩家,在屏幕顶部不停地产生对象,向下移动。(就像App'2Cars '来自Ketchapp。

问题在于:我想检查坠落物体和可移动物体之间的碰撞。我怎样才能做到这一点。 (我正在寻找将近3天但没有找到答案)

public class GameAcvitiy extends Activity {
protected ImageView mMoverr;
protected ImageView mMoverl;
private int count = 0;
private int countr = 0;
private int leftm = 0;
private int rightm = 0;
public int y;
public int x;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_acvitiy);
    MediaPlayer mediaPlayer = MediaPlayer.create(GameAcvitiy.this,R.raw.drums);
    mediaPlayer.start();
    mMoverl = (ImageView) findViewById(R.id.charal);
    mMoverr = (ImageView) findViewById(R.id.charar);

    // Hin und her bewegung
    Button l = (Button) this.findViewById(R.id.changelanebuttonl);
    l.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            count++;
            if (count == 1) {
                mMoverl.animate().x(275).setDuration(200);
            }
            if (count == 2) {
                mMoverl.animate().x(0).setDuration(200);
                count--;
                count--;
            }
        }
    });

    Button r = (Button) this.findViewById(R.id.changelanebuttonr);
    r.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            countr++;
            if (countr == 1) {
                mMoverr.animate().x(605).setDuration(200);
            }
            if (countr == 2) {
                mMoverr.animate().x(875).setDuration(200);
                countr--;
                countr--;
            }
        }
    });

    (new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            while (!Thread.interrupted())
                try
                {
                    Thread.sleep(1500);
                    runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            spawn();

                        }
                    });
                }
                catch (InterruptedException e)
                {
                    //error
                }
        }
    })).start();
}


public void spawn() {
    Random r = new Random();
    int spawnc = r.nextInt(3 - 1) + 1;

    if (spawnc == 1) {
        leftm = 0;
    }
    if (spawnc == 2) {
        leftm = 275;
    }

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.layoutBase);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    ImageView iv = new ImageView(this);
    iv.setImageResource(R.drawable.chara);
    lp.topMargin = 0;
    lp.leftMargin = leftm;
    rl.addView(iv, lp);
    iv.animate().y(1900).setDuration(4000);

    Random vrt = new Random();
    int spawnvt = vrt.nextInt(4 - 1) + 1;
    if (spawnvt == 1) {
        iv.setImageResource(R.drawable.charab);
    }
    if (spawnvt == 2) {
        iv.setImageResource(R.drawable.charag);
    }
    if (spawnvt == 3) {
        iv.setImageResource(R.drawable.charan);
    }


    //Spawn2
    Random rr = new Random();
    int spawnct = rr.nextInt(3 - 1) + 1;

    if (spawnct == 1) {
        rightm = 605;
    }
    if (spawnct == 2) {
        rightm = 875;
    }

    RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    ImageView ivt = new ImageView(this);
    ivt.setImageResource(R.drawable.chara);
    lp2.topMargin = 0;
    lp2.leftMargin = rightm;
    rl.addView(ivt, lp2);
    ivt.animate().y(1900).setDuration(4000);

    Random vr = new Random();
    int spawnv = vr.nextInt(4 - 1) + 1;

    if (spawnv == 1) {
        ivt.setImageResource(R.drawable.charab);
    }
    if (spawnv == 2) {
        ivt.setImageResource(R.drawable.charag);
    }
    if (spawnv == 3) {
        ivt.setImageResource(R.drawable.charan);
    }
        collide(mMoverr, ivt);
        collide(mMoverl, iv);
}


public void collide(ImageView firstIV, ImageView secondIV){
    Rect rc1 = new Rect();
    firstIV.getHitRect(rc1);
    Rect rc2 = new Rect();
    secondIV.getHitRect(rc2);

    if (Rect.intersects(rc1,rc2)) {
        Toast.makeText(GameAcvitiy.this, "BUM", Toast.LENGTH_LONG).show();
    } else {
    }
}

到目前为止,谢谢你们。我希望你能帮助我

1 个答案:

答案 0 :(得分:0)

getHitRect方法是正确使用的方法,您可以通过在开发人员选项中启用指针位置来检查像素完美碰撞,并在ImageViews移动时查看确切的坐标。 下次,只提供相关代码而不是所有代码...