如何使用Rect.intersect方法。如何使用、方法、Rect、intersect

2023-09-06 13:57:26 作者:我们都在埋怨

香港专业教育学院创建了一个游戏,你将一个矩形和道奇从天空坠落的其他矩形。虽然每次的矩形相交没有任何反应。

如果(mSquare.intersect(jSquare)){              canvas.drawColor(Color.BLACK);

 碰撞= mSquare.intersect(jSquare);
     如果(碰撞==真){canvas.drawColor(Color.RED);
  }不管这始终返回false,其中矩形是.......
 

解决方案

有很多方法可以做到这一点,最简单的办法是让边界矩形的每个位图并在每个时间步要检查使用 Rect.intersect()法的冲突。

事情是这样的:

 布尔碰撞= player.getRect()相交(fallingObject.getRect())。
 
46.矩操作函数SetRect FillRect FrameRect PtInRect InvertRect Offsetrect SetRectEmpty IsRectEmpty Intersect

此外还有很多其他的(更好)的方式来做到这一点,尤其是与不是矩形,当你有很多屏幕上的对象的对象打交道。看看这个帖子进行了很好的讨论

还有书开始的Andr​​oid游戏大约有碰撞检测一个伟大的篇章,这本书非常值得一读,如果你正在考虑写一个游戏。

Ive created a game where you move a rectangle and dodge other falling rectangles from the sky. Though everytime the rectangles intersect nothing happens.

if(mSquare.intersect(jSquare)){ canvas.drawColor(Color.BLACK); or

collision = mSquare.intersect(jSquare);
     if(collision==true){  canvas.drawColor(Color.RED);
  }  this always returns false no matter where the rectangles are....... 

解决方案

There are a lot of ways to do this, the simplest would be to get the bounding Rect for each Bitmap and on each time step to check for a collision using Rect.intersect() method.

Something like this:

boolean collision = player.getRect().intersect(fallingObject.getRect());

Additionally there are many other (better) ways to do this, especially when dealing with objects that aren't rectangles and when you have lots of objects on the screen. Check out this post for a good discussion

Also the book "Beginning Android Games" has a great chapter about collision detection and the book is well worth a read if you are considering writing a game.