我有这个:
//Node.h
@interface Node: CCSprite{
BOOL wasTouched;
}
-(BOOL)getTouched;
// some other methods
//Node.m
-(BOOL)getTouched{
return wasTouched;
}
-(id)init{
wasTouched=NO;
}
//wasTouched changes in the other methods..when they are called (no problem here)
//Game.m
//i make an array of nodes and do some stuff
-(void)someMethod{
for (Node *node in arrayOfNodes){
if ([node getTouched]) { //here it crashes
//code
}}}
它与消息-[CCSprite getTouched]: unrecognized selector sent to instance 0x236dd0
问题是:为什么?!
答案 0 :(得分:0)
您可能在Nodes数组中插入了一个普通的CCSprite。
答案 1 :(得分:0)
-(id)init{
wasTouched=NO;
}
这很疼。 :(
您始终必须调用init方法的超级实现。你也必须在这里归来。编译器没有抱怨缺少返回值吗?建议:不要忽略编译器警告。
这两种情况都可能导致非常奇怪的行为,包括崩溃我猜。这是修复:
-(id) init
{
self = [super init];
if (self)
{
wasTouched = NO;
}
return self;
}