2013-07-19 44 views
0

有人可以告诉我为什么这不起作用吗? 我不断收到此错误:“图层#ccTouchBegan覆盖我”错误

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Layer#ccTouchBegan override me'

当我拿出

[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:0 swallowsTouches:YES];

它不会崩溃,但它也没有工作,这绝对不是一个答案。

// Import the interfaces 
#import "HelloWorldLayer.h" 
#import "CCTouchDispatcher.h" 

CCSprite *man; 
CCSprite *death; 

// Needed to obtain the Navigation Controller 
#import "AppDelegate.h" 

#pragma mark - HelloWorldLayer 

// HelloWorldLayer implementation 
@implementation HelloWorldLayer 


// Helper class method that creates a Scene with the HelloWorldLayer as the only child. 
+(CCScene *) scene 
{ 
// 'scene' is an autorelease object. 
CCScene *scene = [CCScene node]; 

// 'layer' is an autorelease object. 
HelloWorldLayer *layer = [HelloWorldLayer node]; 

// add layer as a child to scene 
[scene addChild: layer]; 

// return the scene 
return scene; 
} 

// on "init" you need to initialize your instance 
-(id) init 
{ 
// always call "super" init 
// Apple recommends to re-assign "self" with the "super's" return value 
if((self=[super init])) { 

    man = [CCSprite spriteWithFile:@"man.png"]; 
    man.position = ccp(150,150); 
    [self addChild: man]; 
    death = [CCSprite spriteWithFile:@"death.png"]; 
    death.position = ccp(100,100); 
    [self addChild: death]; 

    [self schedule:@selector(callEveryFrame:)]; 

    [[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:0 swallowsTouches:YES]; 

} 
return self; 
} 

// on "dealloc" you need to release all your retained objects 
- (void) dealloc 
{ 
// in case you have something to dealloc, do it in this method 
// in this particular example nothing needs to be released. 
// cocos2d will automatically release all the children (Label) 

// don't forget to call "super dealloc" 
[super dealloc]; 
} 

-(void) callEveryFrame:(ccTime)dt{ 

man.position = ccp(man.position.x +250*dt, man.position.y); 
if (man.position.x > 480 + 64) { 
    man.position =ccp(-64,man.position.y); 
} 
death.position = ccp(death.position.x, death.position.y +100*dt); 
if (death.position.y > 300+64){ 
    death.position =ccp(death.position.x,-64); 
    } 

} 

-(BOOL)CCTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ 
return YES; 
} 


-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{ 
    CGPoint location = [touch locationInView: [touch view]]; 
    CGPoint convertedLocation = [[CCDirector sharedDirector]convertToGL:location]; 

    [man stopAllActions]; 
    [man runAction:[CCMoveTo actionWithDuration:1 position:convertedLocation]]; 
} 


@end 

回答

0

您没有在课堂上实施ccTouchBegan:withEvent:方法。

Objective C中的变量,类,方法等名称区分大小写。现在仔细看 你的代码:

-(BOOL)CCTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ 
return YES; 
}