我目前正在做一個應用程序,我試圖檢測用戶的觸摸位置。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;
}
不要忘了你必須刪除自己作爲觸摸委託,你必須在 - (void)清理;方法,因爲觸摸分派器可能會保留自我實例。 – LearnCocos2D