2011-06-10 28 views
0

我一直在xCode上使用cocos2D進行遊戲,並且遇到了一個問題需要解決,這似乎超出了我的理解範圍。Cocos2D中Retina和ccTouchBegan的問題

本質上,我使用SneakyButton是一個自定義類,它捕獲觸摸事件,因此您可以使用PNG文件或Circle Radius來執行某些操作。在我的情況下,火子彈。

當我在iOS設備上以SD模式(1倍縮放)在設備上運行項目(對於iPhone 4以前的視網膜)時,顯示所有運行正常。

但是,當我在設備上以Retina顯示模式(2x刻度)運行項目時...我在應用程序的代表中將刻度設置爲2X。可以看到PNG。所有的作品都很好,除了觸摸沒有出現。

我已經縮小到這部分的代碼。

' 
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    if (active) return NO; 

    CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]; 
    location = [self convertToNodeSpace:location]; 
     //Do a fast rect check before doing a circle hit check: 
    if(location.x < -radius || location.x > radius || location.y < -radius || location.y > radius){ 
     return NO; 
    }else{ 
     float dSq = location.x*location.x + location.y*location.y; 
     if(radiusSq > dSq){ 
      active = YES; 
      if (!isHoldable && !isToggleable){ 
       value = 1; 
       [self schedule: @selector(limiter:) interval:rateLimit]; 
      } 
      if (isHoldable) value = 1; 
      if (isToggleable) value = !value; 
      return YES; 
     } 
    } 
return NO; 
} 
' 
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    if (!active) return; 

    CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]; 
    location = [self convertToNodeSpace:location]; 
     //Do a fast rect check before doing a circle hit check: 
    if(location.x < -radius || location.x > radius || location.y < -radius || location.y > radius){ 
     return; 
    }else{ 
     float dSq = location.x*location.x + location.y*location.y; 
     if(radiusSq > dSq){ 
      if (isHoldable) value = 1; 
     } 
     else { 
      if (isHoldable) value = 0; active = NO; 
     } 
    } 
} 
' 
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    if (!active) return; 
    if (isHoldable) value = 0; 
    if (isHoldable||isToggleable) active = NO; 
} 

- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    [self ccTouchEnded:touch withEvent:event]; 
} 

@end 
' 

我想我的問題是在代碼的半徑部...有沒有人跑進這之前或你有這樣的代碼,我需要去國防部,使其工作的哪一部分的任何想法?這是我在完成這個6個月項目之前的最後一塊絆腳石!幫幫我!在此先感謝!

回答

0

你在哪裏設置半徑?或者你如何得到它?如果你從png文件本身獲得半徑,請確保你使用的是邊界框的半徑,而不是contentize,這會在應用任何縮放之前給出sprite的半徑。