如何在java中进行碰撞检测
我正在尝试在java中为cocos2d for android进行共谋检测,这是我的代码
float oldx = player.getPosition().x;
float oldy = player.getPosition().y;
if((player.getPosition().y + player.getContentSize().height > building1.getPosition().y) &&
(player.getPosition().y < building1.getPosition().y + building1.getContentSize().height) &&
(player.getPosition().x + player.getContentSize().width > building1.getPosition().x) &&
(player.getPosition().x < building1.getPosition().x + building1.getContentSize().width))
{
player.setPosition(CGPoint.ccp(oldx, oldy));
}
,但由于某种原因它不起作用......为什么?
I'm trying to go collusion detection in java for cocos2d for android and heres the code I have
float oldx = player.getPosition().x;
float oldy = player.getPosition().y;
if((player.getPosition().y + player.getContentSize().height > building1.getPosition().y) &&
(player.getPosition().y < building1.getPosition().y + building1.getContentSize().height) &&
(player.getPosition().x + player.getContentSize().width > building1.getPosition().x) &&
(player.getPosition().x < building1.getPosition().x + building1.getContentSize().width))
{
player.setPosition(CGPoint.ccp(oldx, oldy));
}
but for some reason it doesn't work... why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将
oldx
和oldy
设置为player.getPosition().x
和player.getPosition().y
, 分别。您在碰撞检测 if 语句中使用完全相同的检查,因此您基本上将位置设置为原来的位置。在检查碰撞发生之前,您需要知道玩家想要移动到的位置,然后不允许玩家移动到那里(即不更新他们的 X 和 Y)。
You are setting
oldx
andoldy
toplayer.getPosition().x
andplayer.getPosition().y
, respectively. You are using exactly the same checks in your collision detection if-statement so you are essentially setting the position to the same spot it was.You need to know the position the player wants to move to prior to doing the check that the collision occurs, and then just not allow the player to move there (i.e. don't update their X and Y).