Ios 未创建CCTouch边界框
我正在用Cocos2d在iOS上制作一个游戏 这是我用来检查立方体是否被触碰的方法Ios 未创建CCTouch边界框,ios,cocos2d-iphone,Ios,Cocos2d Iphone,我正在用Cocos2d在iOS上制作一个游戏 这是我用来检查立方体是否被触碰的方法 - (void)selectSpriteForTouch:(CGPoint)touchLocation { CubeSprite * newSprite = nil; for (CubeSprite *sprite in movableSprites) { NSLog(@"tested against sprite %i", sprite.boundingBox.origin.x)
- (void)selectSpriteForTouch:(CGPoint)touchLocation {
CubeSprite * newSprite = nil;
for (CubeSprite *sprite in movableSprites) {
NSLog(@"tested against sprite %i", sprite.boundingBox.origin.x);
if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {
singleCube = true;
newSprite = sprite;
activeTag = sprite.tag;
break;
}
}
if (newSprite != selSprite) {
selSprite = newSprite;
}
}
但由于某些原因,sprite.boundingBox设置不正确
“针对精灵测试”日志只是打印出“针对精灵测试0”,这似乎不可能,因为我可以在屏幕上看到精灵
下面是我用来将立方体添加到场景中的方法
-(void)addCube:(CubeSprite *)cube {
int totalCubes = [cubes count];
[cube setPosition:ccp(700 - (totalCubes * 50), 120)];
[cubes addObject:cube];
[movableSprites addObject:cube];
[self addChild:cube];
}
可能出了什么问题
提前谢谢
编辑,这是我的多维数据集初始化方法
-(id)initWithNumber:(int)number {
if( (self=[super init])) {
[self setTag:number];
CCSprite* sprite = [CCSprite spriteWithFile:string];
[self addChild:sprite];
NSLog(@"Cube created with value of %i and with file %@", number, string);
}
return self;
}
试着用这个
for (CubeSprite *sprite in movableSprites) {
CGRect projectileRect = [sprite boundingBox];
if (CGRectContainsPoint(projectileRect, touchLocation)) {
singleCube = true;
newSprite = sprite;
activeTag = sprite.tag;
break;
}
}
可能CGRectContainsPoint返回false positive。如果禁用“break;”语句,它应该遍历所有多维数据集 有些事情可能是错误的: -立方体的边界框太大,位置不是您认为的位置 -所有立方体都有相同的位置
-touchLocation是(0,0)Xcode没有就格式字符串错误向您发出警告吗
NSLog(@"tested against sprite %i", sprite.boundingBox.origin.x);
%i
表示整数值CGPoint
的坐标是浮点值。改用%f