返回一个类作为一个层 [英] return a class as a layer

查看:276
本文介绍了返回一个类作为一个层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是使用 cocos2d with xcode ,但我认为一般目标c家伙可以处理它。



我有一个类,它是一个 CCLayer ,我想返回它作为一个图层到我的主类,并显示在那里(所有的动作发生在该图层类)。



所以其他类是:

  CCSprite * r ; 


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


}

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

所以没有发生任何事情,我不看屏幕上的其他类的精灵。

我缺少什么?
感谢。

解决方案

您的第二个代码段应为:

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

你忘记了 init $ c> alloc-init 。不管是否完全解决了你的问题,这绝对是朝正确方向迈出的一步。 ;)



此外,类名应以大写字母开头,并以驼峰式方式插入。例如, RecieverCircleLayer


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 .

解决方案

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.

这篇关于返回一个类作为一个层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆