문제

Is there any way of drawing a Rect in Android in the following pattern, instead of "left, top, right, bottom"? The problem is that I'm using random coordinates:

Rect e = new Rect(random.nextInt(300)+20,20,random.nextInt(300)+45,70);

And it's hard to keep track of the first left random position to include in the right one to the width. In the "x, y, w, h", it's easy to apply my collision test:

if ((((p.left+p.width())>e.left)&&(p.left<(e.left+e.width())))&&
       (((p.top+p.height())>e.top)&&(p.top<(e.top+e.height())))) {
    gameScreen = 2;
}
도움이 되었습니까?

해결책

Just move the random numbers outside of the constructor for your rect.

int left = random.nextInt(300)+20;
int right = left + 20;
int top = random.nextInt(300)+45;
int bottom =  top + 70;

Rect e = new Rect(left, top, right, bottom);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top