我有一个NSDictionary
(allSprites)有很多精灵(spriteX),在我的Touch方法中,我想检查精灵是否被触及。
我的问题是它不会对boundingBox做出反应。我没有看到我的错误!这是NSDictionary
的问题吗?我没有任何错误或任何错误...但它没有工作。
还有另一种检查NSDictionary
中的boundingBox的方法吗?有人可以帮帮我吗?
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in [event allTouches]) {
for (NSValue* value in allSprites) {
CGPoint location = [touch locationInView:touch.view];
location = [[CCDirector sharedDirector] convertToGL:location];
if(CGRectContainsPoint([spriteX boundingBox], location)){
NSLog(@"sprite was touched");
}
}
}
}
答案 0 :(得分:3)
你似乎没有在你的循环中引用spriteX而不是在边界框的测试中,它可能没有被初始化。也许你打算这样做:
for (CCSprite* spriteX in [allSprites allValues]) {
CGPoint location = [touch locationInView:touch.view];
location = [[CCDirector sharedDirector] convertToGL:location];
if(CGRectContainsPoint([spriteX boundingBox], location)){
NSLog(@"sprite was touched");
}
}
如果你需要精灵的密钥:
for (NSString*key in [allSprites allKeys]) {
spriteX = [allSprites objectForKey:key];
CGPoint location = [touch locationInView:touch.view];
location = [[CCDirector sharedDirector] convertToGL:location];
if(CGRectContainsPoint([spriteX boundingBox], location)){
NSLog(@"sprite with key %@ was touched",key);
}
}