我目前正在做一个应用程序,我试图检测用户的触摸位置。
在实现“检测触摸位置”功能的过程中,我从 ccTouchBegan 更改为 ccTouchesBegan。
但我无法让它工作。我从 ccTouchBegan 更改为 ccTouchesBegan:
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
而不是使用:
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
当我这样做时,当我单击屏幕时,整个事情都会崩溃。生成 SIGABRT 错误突出显示:
#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;
}