如何检测cocos2d中sprite的碰撞 [英] How to detect collision of sprite in cocos2d

查看:361
本文介绍了如何检测cocos2d中sprite的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测一个精灵与另一个精灵的碰撞。但我想检查精灵是否与其他精灵的左部分接触或与精灵的右部分接触。
我使用下面的代码来检测碰撞。但这检测整体碰撞(sprite接触到其他sprite的任何点)。但是如果sprite被触及到其他sprite的前端,则计数为碰撞。

i want to detect collision of a sprite with another sprite. but i want to check whether the sprite is touched with left part of other sprite or touched with right part of sprite. i am using below code for detecting collision. but this detect the overall collision(sprite touched to at any point of other sprite). but want to count is as collision if the sprite is touched to front side of other sprite.

if (CGRectIntersectsRect(sprite1.boundingBox, sprite2.boundingBox)) {
            NSLog(@"sprite1 to delete");
 }

任何人都知道如何做?

推荐答案

你能够比较碰撞时精灵的位置吗?

Would you be able to compare the positions of the sprites at the time of collision?

假设sprite1.anchorpoint,sprite2.anchorpoint = ccp(0,0)。检测到碰撞时:

Assume sprite1.anchorpoint, sprite2.anchorpoint = ccp(0,0). At time when collision is detected:

// left part of sprite 1 touched right part of sprite 2
if (sprite1.position.x == sprite2.position.x + sprite2.contentSize.width.x) {

}
// right part of sprite 1 touched left part of sprite 2
else if (sprite1.position.x + sprite1.contentSize.width.x == sprite2.position.x) {

}

这篇关于如何检测cocos2d中sprite的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆