How to use the Rect.intersect method.

橙三吉。 提交于 2019-11-30 05:39:25

问题


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....... 

回答1:


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.



来源:https://stackoverflow.com/questions/9492166/how-to-use-the-rect-intersect-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!