return a class as a layer

做~自己de王妃 提交于 2019-12-13 07:58:13

问题


This question is using cocos2d with xcode, but i think general objective c guys can handle it.

I have a class which is a CCLayer and i want to return it as a layer to my main class and show it in there(and all actions happens in that layer class) .

so the "other" class is :

CCSprite *r;


    -(id)set
    {
        r=[CCSprite spriteWithFile:@"c5.png"];
        r.position=ccp(50,50);
        [self addChild:r z:3];
        return self;


    }

and from main class i call it with :

recieverCircleLayer *r=[recieverCircleLayer alloc] ;
        [self addChild:[r  set ]];

so nothing is happen and i dont see that sprite from the other class on screen.

what am i missing ? thanks .


回答1:


Your second code snippet should be:

recieverCircleLayer *r = [[recieverCircleLayer alloc] init];
[self addChild:[r  set]];

You forgot the init in alloc-init. Regardless of whether or not that completely solves your problem, it's definitely a step in the right direction. ;)

Also, class names should start with an uppercase letter and be camel cased all the way through. For example, RecieverCircleLayer.



来源:https://stackoverflow.com/questions/14588172/return-a-class-as-a-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!