Cocos2d场景编辑器CocosBuilder使用教程

来源:互联网 发布:海洋电影源码 编辑:程序博客网 时间:2024/06/08 17:28
 Cocos2d场景编辑器CocosBuilder使用教程
2012-06-14 20:00:41
标签:场景编辑器 游戏开发 cocos2d
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://1127341.blog.51cto.com/1117341/898754

    在使用Cocos2d-iPhone框架开发iOS游戏的时候,对于每一个场景(CCScene)的编辑是比较麻烦的,好在有外国的牛人提供了非常棒的场景编辑器----CocosBuilder。下面我将详细介绍CocosBuilder结合Cocos2d-iPhone框架的使用。

   框架:Cocos2d-iPhone v2.0-rc2

   工具:CocosBuilder 2.0 beta,Xcode4.3

   一、打开Xcode,新建一个cocos2d iOS工程

   二、在包含.xcodeproj文件的地方新建一个文件夹,叫CCBuilder吧,然后再在这个新建好的文件夹下建两个文件夹,分别叫Projects和Resources,用于放置CocosBuilder工程和资源文件

   三、打开安装好的CocosBuilder,打开File菜单下的New,选择New Project,新建一个CocosBuilder工程,并将这个新建的工程保存到/CCBuilder/Projects文件夹下面。然后打开File菜单下的Project Setting菜单,如下图所示

点击"+"号,分别把/Projects和/Resources文件夹加进来

然后在Publish directory这里,选择之前新建的Xcode工程的资源文件夹

将"Flatten paths when publishing"和"Publish to zip file"勾上,减少生成的文件的体积

全部完成后点击"Done"按钮

   四、打开File菜单下的New,选择New File,新建一个CocosBuilder文件,并将这个新建的文件保存到/CCBuilder/Projects文件夹下面,比如这里新建的ccb文件叫StartGame.ccb,现在就可以在Object菜单下选择添加Cocos2d元素了

   五、在完成ccb文件的编辑后,点击File菜单下的Publish,这样将会在Xcode工程的资源文件夹下生成一个ccb.zip文件,将这个文件添加到Xcode的引用中,就可以使用了

   六、为了解析生成的ccb.zip文件,你需要将附件中的文件添加到Xcode工程中,也就是将 CCBReader(用于读取ccb文件)和SSZipArchive(用于解压缩zip文件)添加进去。并且需要在Xcode工程的build settings下更改Preprocessor Macros,添加一个参数"CCB_ENABLE_UNZIP"

   七、在调用cocos2d的中CCFileUtils的[CCFileUtils sharedFileUtils]之前,需要调用一下[CCBFileUtils sharedFileUtils];

  1. director_ = (CCDirectorIOS*) [CCDirector sharedDirector]; 
  2.  
  3. director_.wantsFullScreenLayout = YES; 
  4.     
  5. [CCBFileUtils sharedFileUtils]; 
  6.  
  7. // Display FSP and SPF 
  8. [director_ setDisplayStats:YES]; 
  9.  
  10. // set FPS at 60 
  11. [director_ setAnimationInterval:1.0/60]; 
  12.  
  13. // attach the openglView to the director 
  14. [director_ setView:glView]; 
  15.  
  16. // for rotation and other messages 
  17. [director_ setDelegate:self]; 
  18.  
  19. // 2D projection 
  20. [director_ setProjection:kCCDirectorProjection2D]; 
  21. /   [director setProjection:kCCDirectorProjection3D]; 
  22.  
  23. // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices 
  24. if( ! [director_ enableRetinaDisplay:YES] ) 
  25.     CCLOG(@"Retina Display Not supported"); 
  26.  
  27. // Create a Navigation Controller with the Director 
  28. navController_ = [[UINavigationController alloc] initWithRootViewController:director_]; 
  29. navController_.navigationBarHidden = YES; 
  30.  
  31. // set the Navigation Controller as the root view controller 
  32. /   [window_ setRootViewController:rootViewController_]; 
  33. [window_ addSubview:navController_.view]; 
  34.  
  35. // make main window visible 
  36. [window_ makeKeyAndVisible]; 
  37.  
  38. // Default texture format for PNG/BMP/TIFF/JPEG/GIF images 
  39. // It can be RGBA8888, RGBA4444, RGB5_A1, RGB565 
  40. // You can change anytime. 
  41. [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888]; 
  42.  
  43. // If the 1st suffix is not found and if fallback is enabled then fallback suffixes are going to searched. If none is found, it will try with the name without suffix. 
  44. // On iPad HD  : "-ipadhd", "-ipad",  "-hd" 
  45. // On iPad     : "-ipad", "-hd" 
  46. // On iPhone HD: "-hd" 
  47. CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils]; 

   八、使用ccb文件

  1. [CCBReader unzipResources:@"ccb.zip"]; 
  2. // Use the CCBReader to load the HelloCocosBuilder scene 
  3. // from the ccbi-file. 
  4. CCScene* scene = [CCBReader sceneWithNodeGraphFromFile:@"StartGame.ccbi" owner:self]; 
  5.      
  6. // Add the scene to the stack. The director will run it when it automatically when the view is displayed. 
  7. [director_ pushScene: scene]; 

    这样就可以正常的使用了