我正在做一个应用程序,我正在尝试检测用户的触摸位置。
在实现“检测触摸位置”功能的过程中,我从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& amp; 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;
}
答案 0 :(得分:3)
这是cocos2d中的一个很好的功能,它允许您在只想处理单个触摸事件的情况下吞下触摸。
尝试将此功能添加到您的课程中:
- (void) registerWithTouchDispatcher {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES];
}
答案 1 :(得分:-2)
而不是
-(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 *)...
您的问题可能只是无效的数据类型或类似的废话,我的建议只是尝试切换触摸类型。
我会提供更多信息但你没有提供很多信息,所以这是我能做的最好的。