2011-10-12 35 views
2

我目前正在做一個應用程序,我試圖檢測用戶的觸摸位置。ccTouchesBegan vs ccTouchBegan - 觸摸檢測和SIGABRT崩潰

在實現「檢測觸摸位置」功能的過程中,我從ccTouchBegan更改爲ccTouchesBegan。

但我無法讓它工作。我改變從ccTouchBegan到ccTouchesBegan:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

而不是使用:

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ 

當我這樣做整個事情崩潰,當我點擊屏幕。產生SIGABRT錯誤higlighting:

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED 
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
NSAssert(NO, @"Layer#ccTouchBegan override me"); 
return YES; 
} 
#endif 
@end 

所以我的問題是:

爲什麼你認爲它崩潰?

ccTouchBegan & ccTouchesBegan有什麼區別?多點觸摸能力?

如需進一步的幫助,這是我的代碼:

-(id) init 
    { 
    if((self=[super init])) { 

     [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888]; 
     self.isTouchEnabled = YES; 

     // Set up background 
     background = [CCSprite spriteWithFile:@"Image.png"]; 
     background.scaleX = 1; 
     background.scaleY = 1; 
     background.position = ccp(0,0); 
     [self addChild:background]; 


     [[CCTouchDispatcher sharedDispatcher]addTargetedDelegate:self 
                 priority:0 
               swallowsTouches:YES]; 

     // Preload sound effect 
     soundFile = [SimpleAudioEngine sharedEngine]; 
     if (soundFile != nil) { 
      [soundFile preloadBackgroundMusic:@"sound.wav"]; 
     } 


    } 
    return self; 
    } 


    -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

    UITouch *touch = [touches anyObject]; 

    NSLog(@"ccTouchesBegan"); 
    // Sets the sound variable to YES 
    ifOne = YES; 

    prevPos = [touch locationInView:[touch self]]; 
    prevPos = [[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.self]]; 

    [self schedule:@selector(timerUpdate:) interval:0.1]; 
    //return YES; 
    } 

回答

3

這是在cocos2d一個不錯的功能,它可以讓你在要處理的只是一個單一的觸摸事件的情況下吞下接觸。

嘗試增加此功能類:

- (void) registerWithTouchDispatcher { 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES]; 
} 
+2

不要忘了你必須刪除自己作爲觸摸委託,你必須在 - (void)清理;方法,因爲觸摸分派器可能會保留自我實例。 – LearnCocos2D

-2
  • ccTouchesBegan發生第二你點擊屏幕
  • ccTouchesEnded發生了,你讓點擊屏幕後去第二

而不是

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
instead of using: 
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ 

嘗試使用

-(void)ccTouchesBegan:(UITouch *)... 

-(void)ccTouchesBegan:(NSSet *)... 

-(BOOL)ccTouchesBegan:(NSSet *)... 

您的問題可能只是無效的數據類型或一些垃圾一樣的是,我的建議只是嘗試切換類型觸及四周。

我會給更多的信息,但你沒有提供大量的信息來處理,所以這是我能做的最好的。