newlist

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

//创建类的对象,并返回相应类的指针
/**
 * define a create function for a specific type, such as CCLayer
 * @__TYPE__ class type to add create(), such as CCLayer
 */
#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
    __TYPE__ *pRet = new __TYPE__(); \
    if (pRet && pRet->init()) \
    { \
        pRet->autorelease(); \
        return pRet; \
    } \
    else \
    { \
        delete pRet; \
        pRet = NULL; \
        return NULL; \
    } \
}
#ifndef _ROTATE_WORLD_TEST_H_
#define _ROTATE_WORLD_TEST_H_

#include "../testBasic.h"

class RotateWorldTestScene : public TestScene
{
public:
    virtual void runThisTest();
};

class SpriteLayer : public CCLayer
{
public:
    virtual void onEnter();
    CREATE_FUNC(SpriteLayer)
};

class TestLayer : public CCLayer
{
public:
    virtual void onEnter();

    CREATE_FUNC(TestLayer)
};

class RotateWorldMainLayer : public CCLayer
{
public:
    virtual void onEnter();

    CREATE_FUNC(RotateWorldMainLayer)
};

#endif

init()和onEnter()这俩个方法都是CCNode的方法。其区别如下:

 

1.其被调用的顺序是先init(),后onEnter()。

 

2.init()在类的初始化时只会调用一次。

 

3.onEnter在该类被载入场景的时候被调用,可能会发生多次。

 

4.CCLayer* cclayer = new MyLayer();

这种情况下,只会触发onEnter。

 

5.CCLayer* cclayer = MyLayer::create();

这样情况下,既会触发init()方法,也会触发onEnter()方法。

#include "RotateWorldTest.h"
#include "../testResource.h"

//------------------------------------------------------------------
//
// TestLayer
//
//------------------------------------------------------------------
void TestLayer::onEnter()
{
    CCLayer::onEnter();

    float x,y;
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    x = size.width;
    y = size.height;

    //CCMutableArray *array = [UIFont familyNames];
    //for( CCString *s in array )
    //    NSLog( s );
    CCLabelTTF* label = CCLabelTTF::create("cocos2d", "Tahoma", 64);

    label->setPosition( ccp(x/2,y/2) );
    
    addChild(label);
}

//------------------------------------------------------------------
//
// SpriteLayer
//
//------------------------------------------------------------------
void SpriteLayer::onEnter()
{
    CCLayer::onEnter();

    float x,y;
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    x = size.width;
    y = size.height;
    
    CCSprite* sprite = CCSprite::create(s_pPathGrossini);
    CCSprite* spriteSister1 = CCSprite::create(s_pPathSister1);
    CCSprite* spriteSister2 = CCSprite::create(s_pPathSister2);
    
    sprite->setScale(1.5f);
    spriteSister1->setScale(1.5f);
    spriteSister2->setScale(1.5f);
    
    sprite->setPosition(ccp(x/2,y/2));
    spriteSister1->setPosition(ccp(40,y/2));
    spriteSister2->setPosition(ccp(x-40,y/2));

    CCAction *rot = CCRotateBy::create(16, -3600);
    
    addChild(sprite);
    addChild(spriteSister1);
    addChild(spriteSister2);
    
    sprite->runAction(rot);

    CCActionInterval *jump1 = CCJumpBy::create(4, ccp(-400,0), 100, 4);
    CCActionInterval *jump2 = jump1->reverse();
    
    CCActionInterval *rot1 = CCRotateBy::create(4, 360*2);
    CCActionInterval *rot2 = rot1->reverse();
    
    spriteSister1->runAction(CCRepeat::create( CCSequence::create(jump2, jump1, NULL), 5 ));
    spriteSister2->runAction(CCRepeat::create( CCSequence::create((CCFiniteTimeAction *)(jump1->copy()->autorelease()), (CCFiniteTimeAction *)(jump2->copy()->autorelease()), NULL), 5 ));
    
    spriteSister1->runAction(CCRepeat::create( CCSequence::create(rot1, rot2, NULL), 5 ));
    spriteSister2->runAction(CCRepeat::create( CCSequence::create((CCFiniteTimeAction *)(rot2->copy()->autorelease()), (CCFiniteTimeAction *)(rot1->copy()->autorelease()), NULL), 5 ));
}

//------------------------------------------------------------------
//
// RotateWorldMainLayer
//
//------------------------------------------------------------------

void RotateWorldMainLayer::onEnter()
{
    CCLayer::onEnter();

    float x,y;
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    x = size.width;
    y = size.height;
    //创建4个颜色不一样的层
    CCNode* blue =  CCLayerColor::create(ccc4(0,0,255,255));
    CCNode* red =   CCLayerColor::create(ccc4(255,0,0,255));
    CCNode* green = CCLayerColor::create(ccc4(0,255,0,255));
    CCNode* white = CCLayerColor::create(ccc4(255,255,255,255));
    //将另一个层加到该层中
    blue->setScale(0.5f);
    blue->setPosition(ccp(-x/4,-y/4));
    blue->addChild( SpriteLayer::create() );
    
    red->setScale(0.5f);
    red->setPosition(ccp(x/4,-y/4));

    green->setScale(0.5f);
    green->setPosition(ccp(-x/4,y/4));
    green->addChild(TestLayer::create());

    white->setScale(0.5f);
    white->setPosition(ccp(x/4,y/4));
    white->ignoreAnchorPointForPosition(false);
    white->setPosition(ccp(x/4*3,y/4*3));

    addChild(blue, -1);
    addChild(white);
    addChild(green);
    addChild(red);

    CCAction* rot = CCRotateBy::create(8, 720);
    //对4个层做动作
    blue->runAction(rot);
    red->runAction((CCAction *)(rot->copy()->autorelease()));
    green->runAction((CCAction *)(rot->copy()->autorelease()) );
    white->runAction((CCAction *)(rot->copy()->autorelease()) );
}

void RotateWorldTestScene::runThisTest()
{
    CCLayer* pLayer = RotateWorldMainLayer::create();

    addChild(pLayer);
    runAction( CCRotateBy::create(4, -360) );

    CCDirector::sharedDirector()->replaceScene(this);

}

 

posted on 2013-07-25 07:18  一枚程序  阅读(325)  评论(0编辑  收藏  举报