[Cpp]
Cocos2d-x Study Notes (5) -- switching between CCScene scenarios
Step 1: Create an cocos2d-win32 application and name it simpleGame;
Step: to keep the code clean and tidy, combine the mountains and the sun in the previous section into a background-LayerDay (daytime), and combine the mountains and the moon into a night view (evening)
The results are as follows:
Daytime:
Evening:
Add three classes in HelloWorldScene. h: LayerDay, LayerNight, and MyScene:
[Cpp]
/*************************************** *********************************/
/* Daytime background */
/*************************************** *********************************/
Class LayerDay: public CCLayerColor
{
Public:
LayerDay ();
~ LayerDay ();
Public:
Virtual void onEnter ();
};
/*************************************** *********************************/
/* Evening background */
/*************************************** *********************************/
Class LayerNight: public CCLayerColor
{
Public:
LayerNight ();
~ LayerNight ();
Public:
Virtual void onEnter ();
};
/*************************************** *********************************/
/* Custom scenario */
/*************************************** *********************************/
Class MyScene: public CCScene
{
Public:
MyScene ();
Public:
Virtual void onEnter ();
Virtual void runThisTest ();
Void daySceneCallback (CCObject * pSender );
Void nightSceneCallback (CCObject * pSender );
};
Modify HelloWorld: scene () in HelloWorldScene. cpp as follows:
[Cpp]
CCScene * HelloWorld: scene ()
{
CCScene * scene = NULL;
Do
{
// 'Scene 'is an autorelease object
Scene = new MyScene ();
CC_BREAK_IF (! Scene );
// 'Player' is an autorelisted object
// HelloWorld * layer = HelloWorld: node ();
LayerDay * pLayer = new LayerDay ();
// Add layer as a child to scene
// The second parameter in addChild is the background order. LayerHill is on the top of LayerCloud, that is, the surface.
Scene-> addChild (pLayer, 0 );
} While (0 );
// Return the scene
Return scene;
}
Then add the member functions of the three classes:
[Cpp]
/*************************************** *********************************/
/* Class LayerNight */
/*************************************** *********************************/
LayerNight: LayerNight ()
{
CCSize size = CCDirector: shareddire()-> getWinSize ();
This-> initWithColor (ccc4 (0, 0, 0,255 ));
CCSprite * pSpriteNight = CCSprite: spriteWithFile ("night.png ");
CCSprite * pSpriteHill = CCSprite: spriteWithFile ("hill.png ");
PSpriteNight-> setPosition (ccp (size. width/2, size. height/2 ));
PSpriteHill-> setPosition (ccp (size. width/2, size. height/2 ));
AddChild (pSpriteNight );
AddChild (pSpriteHill );
}
LayerNight ::~ LayerNight ()
{
}
Void LayerNight: onEnter ()
{
CCLayer: onEnter ();
}
/*************************************** *********************************/
/* Class LayerDay */
/*************************************** *********************************/
LayerDay: LayerDay ()
{
This-> initWithColor (ccc4 (255,255,255,255 ));
CCSize size = CCDirector: shareddire()-> getWinSize ();
CCSprite * pSpriteHill = CCSprite: spriteWithFile ("hill.png ");
CCSprite * pSpriteCloud = CCSprite: spriteWithFile ("cloud.png ");
Pspritecposition-> setPosition (ccp (size. width/2, size. height/2 ));
PSpriteHill-> setPosition (ccp (size. width/2, size. height/2 ));
AddChild (pSpriteCloud );
AddChild (pSpriteHill );
}
LayerDay ::~ LayerDay ()
{
}
Void LayerDay: onEnter ()
{
CCLayer: onEnter ();
}
/*************************************** *********************************/
/* Custom scenario */
/*************************************** *********************************/
MyScene: MyScene ()
{
CCScene: init ();
CCLabelTTF * labelDay = CCLabelTTF: labelWithString ("Day", "Arial", 20 );
CCLabelTTF * labelNight = CCLabelTTF: labelWithString ("Night", "Arial", 20 );
CCMenuItemLabel * pMenuDayItem = CCMenuItemLabel: itemWithLabel (
LabelDay, this, menu_selector (MyScene: daySceneCallback ));
CCMenuItemLabel * pMenuNightItem = CCMenuItemLabel: itemWithLabel (
LabelNight, this, menu_selector (MyScene: nightSceneCallback ));
CCMenu * pMenuDay = CCMenu: menuWithItems (pMenuDayItem, NULL );
CCMenu * pMenuNight = CCMenu: menuWithItems (pMenuNightItem, NULL );
CCSize size = CCDirector: shareddire()-> getWinSize ();
PMenuDayItem-> setPosition (ccp (size. width/2-40, 30 ));
PMenuNightItem-> setPosition (ccp (size. width/2 + 40, 30 ));
PMenuDay-> setPosition (CCPointZero );
PMenuNight-> setPosition (CCPointZero );
AddChild (pMenuDay, 2 );
AddChild (pMenuNight, 2 );
}
Void MyScene: daySceneCallback (CCObject * pSender)
{
CCScene * scene = new MyScene ();
CCLayer * pLayer = new LayerDay ();
Scene-> addChild (pLayer, 0 );
CCDirector: sharedDirector ()-> pushScene (scene );
Scene-> release ();
PLayer-> release ();
}
Void MyScene: Maid (CCObject * pSender)
{
CCScene * scene = new MyScene ();
CCLayer * pLayer = new LayerNight ();
Scene-> addChild (pLayer, 0 );
CCDirector: sharedDirector ()-> pushScene (scene );
Scene-> release ();
PLayer-> release ();
}
Void MyScene: onEnter ()
{
CCScene: onEnter ();
}
Void MyScene: runThisTest ()
{
CCLayer * pLayer = new LayerNight ();
AddChild (pLayer );
PLayer-> release ();
CCDirector: sharedDirector ()-> replaceScene (this );
}
Step 3: Compile and run the program. The following screen is displayed. You can switch the scenario by clicking "Day" and "Night" on the screen.
However, the scenario is tough to switch between. To make the scenario more natural and elegant, we have to make some modifications to the Code in HelloWorldScene. cpp:
Add
CCTransitionScene * createTransition (cocos2d: ccTime t, CCScene * s)
{
Return CCTransitionFadeUp: transitionWithDuration (t, s); // The effect of scenario Switching
// Return CCTransitionFadeDown: transitionWithDuration (t, s );
// Return CCTransitionTurnOffTiles: transitionWithDuration (t, s );
// Return CCTransitionSplitRows: transitionWithDuration (t, s );
// Return CCTransitionSplitCols: transitionWithDuration (t, s );
//......
}
Run the program again. Is the effect much better this time!