cocos2d sprite collision detection boundingbox(cocos2d sprite 碰撞检测boundingbox)
问题描述
我有 2 个精灵.我使用边界框来检查与 CGRectIntersectsRect 的碰撞.但它不起作用.HBBall 和 HBpaddle 都有一个名为 image 的 CCSprite.
I got 2 sprites. I use the boundingbox to check for collision with CGRectIntersectsRect. But it is not working. HBBall and HBpaddle has a CCSprite called image.
初始化:
ball = [[HBBall alloc] init];
ball.position = ccp(150, 50);
[self addChild:ball];
[update addObject:ball];
paddle1 = [[HBPaddle alloc] init];
paddle1.position = ccp(50, 160);
[self addChild:paddle1];
更新:
if (CGRectIntersectsRect([paddle1.image boundingBox], [ball.image boundingBox]))
CCLOG(@"ball hit paddle");
CGRectIntersectsRect 总是返回 true.有人有想法吗?
CGRectIntersectsRect retuns always true. Does anyone have an idea?
推荐答案
你不能直接传递边界框,因为它是相对于精灵的.您必须像这样传递绝对 CGRect 边界框:
you cant pass directly the bounding box, because it's relative to the sprite. You MUST pass the absolute CGRect boundingbox like this:
s = CCsprite
s.anchorPoint = ccp(0, 0);
CGRect absoluteBox = CGRectMake(s.position.x, s.position.y, [s boundingBox].size.width, [s boundingBox].size.height);
进行必要的调整!
希望能帮上忙!
这篇关于cocos2d sprite 碰撞检测boundingbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:cocos2d sprite 碰撞检测boundingbox


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