2013-06-02 19 views
0

我有一個帶有很多Sprite(spriteX)的NSDictionary(allSprites),並且在我的Touch Method中我想檢查是否觸及了Sprite。NSDictionary中的CCSprite不會對BoundingBox產生反應

我的問題是它不會對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"); 
     } 
    } 
    } 
} 

回答

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); 
    } 
} 
+0

非常感謝!!!!這工作很棒!!!!!!!!!!! – user2444915

+1

我會將'location = ...'行移出循環。 – Kreiri