【问题标题】:Weird behavior while subclassing CCSprite子类化 CCSprite 时的奇怪行为
【发布时间】:2012-05-21 17:07:01
【问题描述】:

我有这个:

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

问题是:为什么?!

【问题讨论】:

  • 您如何收到arrayOfNodes,您能否确认其中确实只包含Node 对象而没有CCSprite
  • 我在 Game 的 init 方法中创建了一个随机的节点数组,并且我已经检查过了,其他自定义方法可以工作..只有这个不行。我正在尝试重写我的代码来解决这个问题

标签: cocos2d-iphone subclass


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    -(id)init{
        wasTouched=NO;
        }
    

    这很痛。 :(

    您总是必须调用 init 方法的超级实现。您还必须在这里返回自我。编译器没有抱怨缺少返回值吗?忠告:不要忽略编译器警告。

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

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

    【讨论】:

    • 嗯..这显然不是完整的代码......无论如何..我做了一个解决方法,现在似乎没问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多