How to detect collision of sprite in cocos2d(如何检测 cocos2d 中精灵的碰撞)
问题描述
我想检测一个精灵与另一个精灵的碰撞.但我想检查精灵是否与其他精灵的左侧部分接触或与精灵的右侧部分接触.我正在使用下面的代码来检测碰撞.但这会检测整体碰撞(精灵在其他精灵的任何点接触).但是如果精灵被触摸到其他精灵的正面,则要算作碰撞.
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 中精灵的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检测 cocos2d 中精灵的碰撞


基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01