有人可以告诉我为什么这不起作用吗?我不断收到此错误:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“Layer#ccTouchBegan override me”
当我拿出
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self 优先级:0swallowsTouches: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