子类化CCSprite时的奇怪行为

时间:2012-05-19 21:30:15

标签: cocos2d-iphone subclass

我有这个:

//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

崩溃

问题是:为什么?!

2 个答案:

答案 0 :(得分:0)

您可能在Nodes数组中插入了一个普通的CCSprite。

答案 1 :(得分:0)

-(id)init{
    wasTouched=NO;
    }

这很疼。 :(

您始终必须调用init方法的超级实现。你也必须在这里归来。编译器没有抱怨缺少返回值吗?建议:不要忽略编译器警告。

这两种情况都可能导致非常奇怪的行为,包括崩溃我猜。这是修复:

-(id) init
{
    self = [super init];
    if (self)
    {
        wasTouched = NO;
    }
    return self;
}