当前位置:软件学习 > Director >>

cocos2d开发学习二:关于 Director,Scenes, Layers, Sprites详解

[cpp] 
<pre name="code" class="cpp">以游戏开场进入菜单选项来简单介绍下一中提到的四个概念的使用</pre> 
<pre></pre> 
<pre name="code" class="cpp">Director:除了在应用启动后对全局的的配置设置,后期它更多的作用在于场景的转换(scene)</pre>初期用到主要的几个方法: <br> 
replaceScene<br> 
popScene<br> 
pushScene<br> 
runwithScene<br> 
<br> 
Scenes:场景,其实这个抽闲的概念在代码量基本很少,一般就在就是初始化一个场景时会看到他实例化,然后被添加进1-N个layer层。然后返回该对象供Director来操作。<br> 
一般会看到它的地方<br> 
+(CCScene *)scene<br> 
{<br> 
    CCScene *scene = [CCScene node];//其实就是执行了init,使用自动释放<br> 
    CCLayer *layer = [MenuLayer node];<br> 
    [scene addChild:layer];<br> 
    return scene;<br> 
}<br> 
一个scene中可以包含多个layer。<br> 
Layers:书上也说了,基本99%的工作量都是对这个东西的编写操作。<br> 
你可以在layer上面创造各种子layer和Sprites,说白了,其实都是节点。<br> 
  CCMenu *menu = [CCMenu menuWithItems:item1,item2, nil];<br> 
        menu.tag = 100;<br> 
        CGSize winSize = [[CCDirector sharedDirector] winSize];<br> 
       <br> 
        menu.position = CGPointMake(winSize.width/2, winSize.height);<br> 
        [self addChild:menu];<br> 
上面是简单的在layer上放上了一个菜单layer。<br> 
<br> 
Sprites:精灵的话可以理解最小单元的node(当然只是一种自我概念)<br> 
<p>游戏的主角啊什么,花花草草都可以看成是一个个Sprites</p> 
<p><br> 
</p> 
<p>cocoaChina 上有个更官方的解释这4个概念:<a href="http://www.cocoachina.com/gamedev/gameengine/2010/0419/1116.html">http://www.cocoachina.com/gamedev/gameengine/2010/0419/1116.html</a></p> 
<p>我觉得写的很好,理解起来也很明了</p> 
<p><br> 
</p> 
<p>好了,理解了以上4个最重要的元素后,我们开始着手编码也运用他们了</p> 
<p>我们的第一个helloword就问候完本人顺便问候家里人了</p> 
<p></p> 
<pre name="code" class="cpp">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    // Create the main window 
    window_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds] 
                                   pixelFormat:kEAGLColorFormatRGB565   //kEAGLColorFormatRGBA8 
                                   depthFormat:0    //GL_DEPTH_COMPONENT24_OES 
                            preserveBackbuffer:NO 
                                    sharegroup:nil 
                                 multiSampling:NO 
                               numberOfSamples:0]; 
 
    director_ = (CCDirectorIOS*) [CCDirector sharedDirector]; 
    director_.wantsFullScreenLayout = YES; 
    [director_ setDisplayStats:YES]; 
    [director_ setAnimationInterval:1.0/60]; 
    [director_ setView:glView]; 
    [director_ setDelegate:self]; 
    [director_ setProjection:kCCDirectorProjection2D]; 
//  [director setProjection:kCCDirectorProjection3D]; 
 
    if( ! [director_ enableRetinaDisplay:YES] ) 
        CCLOG(@"Retina Display Not supported"); 
 
    navController_ = [[UINavigationController alloc] initWithRootViewController:director_]; 
    navController_.navigationBarHidden = YES; 
 
    [window_ addSubview:navController_.view]; 
    [window_ makeKeyAndVisible]; 
    [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888]; 
    CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils]; 
    [sharedFileUtils setEnableFallbackSuffixes:NO];             // Default: NO. No fallback suffixes are going to be used 
    [sharedFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"];      // Default on iPhone RetinaDisplay is "-hd" 
    [sharedFileUtils setiPadSuffix:@"-ipad"];                   // Default on iPad is "ipad" 
    [sharedFileUtils setiPadRetinaDisplaySuffix:@"-ipadhd"];    // Default on iPad RetinaDisplay is "-ipadhd" 
    [CCTexture2D PVRImagesHavePremultipliedAlpha:YES]; 
    //以上都是模板自动给你生成的,就是配置一些全局参数 
     
     
    //启动一个场景 
    [director_ runWithScene: [MenuLayer scene]];  
     
    return YES; 
}</pre><br> 
<p></p> 
<p>以上代码基本就模板自己生成,就是一些相关的设置。这里为了简洁,我把注释都删除了,大家新建后能看到注释。</p> 
<p>这也是对Director这个概念最入门的印象,导演么,布置整场戏的道具,演员配置,灯光啊,音响啊什么的。</p> 
<p>然后布置完后,第一场景开始  runWithScene。</p> 
<p>我这边第一场景假设是菜单选择场景。</p> 
<p></p> 
<pre name="code" class="cpp">@implementation MenuLayer 
 
+(CCScene *)scene 

    CCScene *scene = [CCScene node];//其实就是执行了init,使用自动释
补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,