开发者

Importance of Cocos2D CCScenes?

I recently followed Ray Wenderlich's Cocos2D tutorial for putting Cocos2D in a UIKit app. I am currently only using Cocos2D in only one of my UIViews. In the tutorial he uses a CCScene which is a specific .h and .m class in the project. In the scene is some code which I do not know the important of either. I must say that I am new to Cocos2D and I am wondering what is the point of a CCScene? Do I need to use it in my situation? And if so, how?

Thanks!

Edit1: Is this correct? I am also adding a simple game loop so should I do it in the way开发者_如何转开发 I am doing it below or should I use CADisplayLink?

//Cocos2D methods
-(id) init {
    if((self=[super init]))
    {
        CCScene *scene = [CCScene node];
        CCLayer *layer = [CCLayer node];
        [scene addChild:layer];
        [[CCDirector sharedDirector] runWithScene:scene];
        [self performSelector:@selector(cocosgameLoop:) withObject:nil afterDelay:1/60.0f];
    }
    return self;
}
- (void)cocosgameLoop:(ccTime)dT {
    //Check for collisions here and add gravity, etc....
}
- (void)viewDidUnload {
    [super viewDidUnload];
    [[CCDirector sharedDirector] end];
}
//


CCScene is a root node in a scene graph. A node that have no parents. CCDirector operates with CCScenes only. Read this for more information: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:lesson_3._menus_and_scenes

ANSWER TO YOUR COMMENT

Actually you don't have to understand how CCScene is implemented. But if you want something to be rendered with cocos2d you have to create a CCScene and pass it to CCDirector. The common way is:

CCScene *scene = [CCScene node];
CCLayer *layer = [CCLayer node];
[scene addChild:layer];
[[CCDirector sharedDirector] runWithScene:scene];

Usually you have to subclass a CCLayer and reimplement init method to add your staff (sprites for example). Take a look at programming guide for more more detailed answer: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:index

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜