ios - Cocos2D CCScenes 的重要性?

标签 ios cocos2d-iphone layer

我最近按照 Ray Wenderlich 的 Cocos2D 教程将 Cocos2D 放入 UIKit 应用程序。我目前只在我的一个 UIView 中使用 Cocos2D。在教程中,他使用了 CCScene,它是项目中特定的 .h 和 .m 类。场景中有一些代码,我也不知道它们的重要性。我必须说我是 Cocos2D 的新手,我想知道 CCScene 有什么意义?我需要在我的情况下使用它吗?如果是这样,怎么做到的?

谢谢!

Edit1:这是正确的吗?我还添加了一个简单的游戏循环,所以我应该按照下面的方式来做还是应该使用 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 是场景图中的根节点。没有父节点的节点。 CCDirector 仅与 CCScenes 一起运行。阅读此内容以获取更多信息:http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:lesson_3._menus_and_scenes

对您的评论的答复

其实CCScene是如何实现的,你不必了解。但是如果你想用 cocos2d 渲染一些东西,你必须创建一个 CCScene 并将它传递给 CCDirector。常见的方式是:

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

通常你必须继承一个 CCLayer 并重新实现 init 方法来添加你的员工(例如 Sprite )。查看编程指南以获得更详细的答案: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:index

关于ios - Cocos2D CCScenes 的重要性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7536455/

相关文章:

iphone - 使用 @2x 升级 cocos2d iPhone 游戏以在 iPhone4 分辨率下运行

javascript - 从图层集合中搜索/检索原始容器传单图层的最佳方法

iOS 图层蒙版在 UIStackView 上不起作用

ios - 从 2.1 更新到 2.2 后 WCSession sendmessage 慢了一倍

ios - 如何使导航 Controller 中的嵌入与scrollView一起使用

ios - 我如何知道 GHosted Authenticate ViewController 何时消失?

iphone - Cocos2d - 将 GLImageProcessing 效果应用于 CCSprite

validation - DAO 是否应该验证输入

ios - UISplitViewController 在纵向显示细节上方的主视图

ios - 不使用 Swift 显示小数