The cocos2dx framework already provides many scenario switching classes, but some custom scenario switching can only be implemented by yourself. The following is the implemented class. The resolution is 750*500. Adjust the resolution as required.
Header file
# Ifndef _ TRANSITION_GAME_H _
# Define _ TRANSITION_GAME_H _
# Include <cocos2d. h>
Namespace cocos2d
{
Class CCTransitionGame: public CCTransitionScene
{
Public:
CCTransitionGame ();
Virtual ~ CCTransitionGame ();
Void onEnter ();
Static CCTransitionGame * create (float t, CCScene * scene );
Private:
Void LRFinish (void );
Void OnFirstActionFinish (void );
Private:
Int m_FinishCnt;
};
}
# Endif source file
# Include "TransitionGame. h"
# Include "xlog. h"
# Include <xstring. h>
Namespace cocos2d
{
Using namespace zdh;
CCTransitionGame * CCTransitionGame: create (float t, CCScene * scene)
{
CCTransitionGame * pScene = new CCTransitionGame ();
If (pScene & pScene-> initWithDuration (t, scene ))
{
PScene-> autorelease ();
Return pScene;
}
CC_SAFE_DELETE (pScene );
Return NULL;
}
CCTransitionGame: CCTransitionGame ()
{
M_FinishCnt = 0;
}
CCTransitionGame ::~ CCTransitionGame ()
{
}
Void CCTransitionGame: onEnter ()
{
CCTransitionScene: onEnter ();
CCSize visibleSize = CCDirector: sharedDirector ()-> getVisibleSize ();
CCPoint stLeftBegin, stLeftEnd, stRightBegin, stRightEnd;
// Set the start and end points on the left
StLeftBegin. setPoint (-4360000f,-60 );
StLeftEnd. setPoint (visibleSize. width/2.0f + stLeftBegin. x,-601_f );
// Set the start and end points on the right
StRightBegin. setPoint (visibleSize. width,-601_f );
StRightEnd. setPoint (visibleSize. width/2.0f,-601_f );
// Load the animation sequence
CCSpriteFrameCache * pCache = CCSpriteFrameCache: sharedSpriteFrameCache ();
PCache-> addSpriteFramesWithFile ("middle_ani_1.plist ");
PCache-> addSpriteFramesWithFile ("middle_ani_2.plist ");
// Generate a list of animated images and animated objects
CCArray * pAnimFrames = CCArray: createWithCapacity (69 );
XAnsiString strAniName;
For (int I = 1; I <70; I ++)
{
StrAniName. printf ("light000004d.png", I );
PAnimFrames-> addObject (pCache-> spriteFrameByName (strAniName. c_str ()));
}
CCAnimation * animation = CCAnimation: createWithSpriteFrames (pAnimFrames, this-> m_fDuration * 2.0f/3.0f/6920.f );
CCNode * pNode = CCNode: create (); // There are two subnodes, one is the left side of the SWAp image, the other is the middle of the animation, used for moving together Action
CCSprite * pLeft = CCSprite: createWithSpriteFrameName ("swap_left.png ");
PLeft-> setAnchorPoint (CCPointZero );
PNode-> addChild (pLeft );
CCSprite * pMiddle = CCSprite: create (); // display the animation
PMiddle-> setAnchorPoint (CCPointZero );
PMiddle-> setPosition (ccp (4360000f-690000f, 2500000f + 600000f-72.0f ));
PMiddle-> runAction (CCAnimate: create (animation ));
PNode-> addChild (pMiddle );
PNode-> setAnchorPoint (ccp (0, 0 ));
PNode-> setPosition (stLeftBegin );
This-> addChild (pNode, 1 );
// Swap image on the right
CCSprite * pRight = CCSprite: createWithSpriteFrameName ("swap_right.png ");
PRight-> setPosition (stRightBegin );
PRight-> setAnchorPoint (CCPointZero );
This-> addChild (pRight, 0 );
// Define action
// Move the activity to the right on the left
CCMoveTo * pActionLeft = CCMoveTo: create (m_fDuration/3, stLeftEnd );
// Move the activity to the left on the right
CCMoveTo * pActionRight = CCMoveTo: create (m_fDuration/3, stRightEnd );
// Remain unchanged
CCMoveTo * pActionLeft1 = CCMoveTo: create (m_fDuration/3, stLeftEnd );
CCMoveTo * pActionRight1 = CCMoveTo: create (m_fDuration/3, stRightEnd );
CCMoveTo * pActionLeft2 = CCMoveTo: create (m_fDuration/3, stLeftBegin );
CCMoveTo * pActionRight2 = CCMoveTo: create (m_fDuration/3, stRightBegin );
M_FinishCnt = 0;
PNode-> runAction (CCSequence: create (pActionLeft, CCCallFunc: create (this, callfunc_selector (CCTransitionGame: OnFirstActionFinish), success, pActionLeft2, CCCallFunc: create (this, callfunc_selector (CCTransitionGame: LRFinish), NULL ));
PRight-> runAction (CCSequence: create (pActionRight, pActionRight1, pActionRight2, CCCallFunc: create (this, callfunc_selector (CCTransitionGame: LRFinish), NULL ));
}
Void CCTransitionGame: LRFinish (void)
{
// After the activity is completed, execute the Finish of the field line.
M_FinishCnt ++;
If (m_FinishCnt> = 2)
{
CCTransitionScene: finish ();
}
}
Void CCTransitionGame: OnFirstActionFinish (void)
{
// Display the first scenario and the second scenario before opening the door
M_pInScene-> setVisible (true );
M_pOutScene-> setVisible (false );
}
} Resources used
/Files/zdhsoft/plist.zip: