不能让Rect.intersects(REC,REC2)工作 [英] Cant get Rect.intersects(rec, rec2) to work

查看:394
本文介绍了不能让Rect.intersects(REC,REC2)工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我的程序将无法通过获得Rect.intersects(RE,hitcore)。如果我使用的System.out.println()re.flattenToString(),我看到两个矩形相交,但它不会返回true

我也使用 re.intersect(hitcore),但仍没有尝试过。帮助?

 进口android.graphics.Rect;市民阶级敌人{
公共矩形重,hitCore;私人布尔命中= FALSE;
公敌() {
    重=新的Rect(0,0,0,0);
    hitCore =新的Rect(0,0,0,0);}公共无效更新(){    re.set(+的centerX leftX,centerY + TOPY,rightX,BOTY); //这些都是由其他类设置
    hitCore.set(ship.getCenterX()+ 3,ship.getCenterY()+ 10,93,20);            //System.out.println(re.flattenToString()+Rect1的);
    //System.out.println(hitCore.flattenToString()+RECT2);    checkHit(hitCore);}私人无效checkHit(矩形hitCore){
    如果(Rect.intersects(hitCore,RE)){
        命中= TRUE;
    }
}
}


解决方案

我想通了。我用x,y和宽度,高度设定矩形。实际参数是左上角坐标和右下角坐标。

  rect.set(X1,Y1,X2,Y2);

For some reason my program won't get through Rect.intersects(re,hitcore). If I use System.out.println()re.flattenToString(), I see that the two rectangles intersect but it won't return true.

I also tried using re.intersect(hitcore), but still nothing. Help?

import android.graphics.Rect;   

public class Enemy {
public Rect re, hitCore;

private boolean hit = false;


public Enemy() {
    re = new Rect(0, 0, 0, 0);
    hitCore = new Rect(0, 0, 0, 0);

}

public void update() {

    re.set(centerX + leftX, centerY + topY, rightX, botY); // these are set by another class                  
    hitCore.set(ship.getCenterX() + 3, ship.getCenterY() + 10, 93, 20);

            //System.out.println(re.flattenToString() + " rect1");
    //System.out.println(hitCore.flattenToString() + " rect2");

    checkHit(hitCore);

}

private void checkHit(Rect hitCore) {
    if (Rect.intersects(hitCore, re)) {
        hit = true;
    }
}
}

解决方案

I figured it out. I was setting up rectangle using x,y and width,height. The actual parameters are the top left corner coordinate and bottom right corner coordinate.

 rect.set(x1,y1,x2,y2);

这篇关于不能让Rect.intersects(REC,REC2)工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆