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