Cocos2d-x scene switch I change! ---- Game development: Zhao Yun to fight (12), cocos2d horizontal combat

Source: Internet
Author: User

Cocos2d-x scene switch I change! ---- Game development: Zhao Yun to fight (12), cocos2d horizontal combat

Here isEvankakaWelcome to the discussion and exchange ~~~~~~

 Reprinted please indicate the source http://blog.csdn.net/evankaka/article/details/42883881

This article focuses on the login interface created with Cocos Studio to design a layer class and jump to different scenarios based on the selection of the login interface. Among them, the interface used in this paper can refer to the previous Cocos2d-x using Cocos Studio production interface and application --- game development "zhao Yun to fight" (11)

Cocos2d-x version: 2.2.5

Project Environment: Windows 7 + VS2010

Open Method: place the project in the project folder under the cocos2d-x installation directory open with

Let's take a look at the effect:



I. Scenario switching knowledge 1 some common knowledge

Entry: AppDelegate

Scenario: Scene (various scene implemented by developers)

Scenario components: Layer (mainly provides the Group Management Mechanism) and various nodes (basic elements)

Scenario switching: Director obviously, a simple game basically only needs to implement several Scene by itself, add some layers and nodes, and find a suitable place to switch with Director.


2. Description

RunWithScene (CCScene * scene): starts the game and runs the scene scenario. This method is called when the main program starts for the first time. (It is applied in AppDelegate) replaceScene (CCScene * scene): replaces the current scene with the input scene, and the current scene is released. This is the most common method for switching scenarios. PushScene (CCScene * scene): runs a new scenario without releasing the memory of the old scenario. Pushing a new scenario is equivalent to placing another piece of paper on the visible paper, the previous paper remains unchanged. Applicability: a. Promote a frequently used scenario, such as the Setting scenario in the game. B. When you want to retain the initial scenario status, you do not want to retain or load the status. For example, when you view the rankings during the game process and promote the ranking scenario, the game scenario will not continue to run and will be suspended. PopScene: releases the current scenario, then pops up the stack top scenario from the generation execution scenario stack, and sets it to the current running scenario. If the stack is empty, the application is directly ended. It can be used in pairs with pushScene, as shown in the following figure: Enter the settings page on the main interface, and then return to the main interface. It is difficult to test this because we need to ensure that there is always enough additional memory for the scenario to be promoted whenever we push the scenario. It is recommended that all the scenarios to be promoted be lightweight and only occupy a small amount of memory, and only pop up itself, instead of promoting other scenarios.
Ii. custom logon Interface

Now I can directly read the code, replace the JSON file, and change the button name to set the game logon interface.

GameLogin. h

# Ifndef _ GameLogin_SCENE_H __# define _ GameLogin_SCENE_H __# include "cocos2d. h "# include" cocos-ext.h "USING_NS_CC; USING_NS_CC_EXT; using namespace gui; // remember not to see error C2065:" SEL_TouchEvent ": Undeclared identifier class GameLogin: public cocos2d: CCLayer {public: virtual bool init (); static cocos2d: CCScene * scene (); CREATE_FUNC (GameLogin ); // The callback event void TouchBeginButton (cocos2d: CCObject * obj, gui: TouchEventType) of the Start game button; // set the callback event void TouchSetnButton (cocos2d :: CCObject * obj, gui: TouchEventType); // void TouchExitButton (cocos2d: CCObject * obj, gui: TouchEventType type );}; # endif/_ GameLogin_SCENE_H __

GameLogin. cpp

# Include "GameLogin. h "# include" HelloWorldScene. h "// This is the header file CCScene * GameLogin: scene () {CCScene * scene = CCScene: create (); GameLogin * layer = GameLogin: create (); scene-> addChild (layer); return scene;} bool GameLogin: init () {if (! CCLayer: init () {return false;} CCSize visibleSize = CCDirector: sharedDirector ()-> getVisibleSize (); CCPoint origin = CCDirector: shareddire () -> getVisibleOrigin (); // load the interface gui created by cocos studio: TouchGroup * ul = gui: TouchGroup: create (); gui :: layout * equipe_root = dynamic_cast <gui: Layout *> (GUIReader: shareReader ()-> widgetFromJsonFile ("MyLogin_1.ExportJson"); ul-> addWidget (equipe_root ); this-> addChild (ul, 2); // Add event listening gui to the start Button: Button * Btn_Begin = dynamic_cast <gui :: button *> (equipe_root-> getChildByName ("Begin_Game"); // Begin_Game is the name of the Button defined in Cocos Studio. Btn_Begin-> addTouchEventListener (this, toucheventselector (GameLogin :: touchBeginButton); // Add event listening gui to the setting Button: Button * Btn_Set = dynamic_cast <gui: Button *> (equipe_root-> getChildByName ("Set_Game ")); // Set_Game is the name of the button defined in Cocos Studio Btn_Set-> addTouchEventListener (this, toucheventselector (GameLogin: TouchSetnButton); // Add event listening gui to the start button :: button * Btn_Exit = dynamic_cast <gui: Button *> (equipe_root-> getChildByName ("Exit_Game ")); // Exit_Game is the name of the button Btn_Exit-> addTouchEventListener (this, toucheventselector (GameLogin: TouchExitButton); return true;} void GameLogin :: touchBeginButton (cocos2d: CCObject * obj, gui: TouchEventType type) {if (type = gui: TouchEventType: TOUCH_EVENT_BEGAN) CCDirector: shareddire () -> replaceScene (HelloWorld: scene (); // enter the main game page} // set the callback event void GameLogin: TouchSetnButton (cocos2d: CCObject * obj, gui: TouchEventType type) {}// void GameLogin: TouchExitButton (cocos2d: CCObject * obj, gui: TouchEventType type) {if (type = gui: TouchEventType: TOUCH_EVENT_BEGAN) CCDirector: sharedDirector ()-> end ();}

A very simple class that can be extended in the future ~

3. Change scenario and initialization interface 1. Change the initial interface

Open AppDelegate. cpp or AppDelegate. h and add the header file # include "GameLogin. h"

Set the original CCScene * pScene = HelloWorld: scene ();
Change to CCScene * pScene = GameLogin: scene ();

Remember in the Cocos2d-x game pause, continue the game, re-start interface implementation --- game development "Zhao cloud to fight" (10) inside we are not defined a Gamepause class, there is a button to return to the main interface. Remember to write the event here.

Add header file # include "GameLogin. h"

Modify a function

// Return to the main interface void Gamepause: menuLogin (CCObject * pSender) {CCDirector: sharedDirector ()-> replaceScene (GameLogin: scene ());}
Run the following command to see if


2. Switching with effects

GameLogin. cpp

Void GameLogin: TouchBeginButton (cocos2d: CCObject * obj, gui: TouchEventType) {if (type = gui: TouchEventType: TOUCH_EVENT_BEGAN) {// CCDirector :: sharedDirector ()-> replaceScene (HelloWorld: scene (); // enter the game Main Page // CCDirector: sharedDirector ()-> replaceScene (CCTransitionPageTurn: create (2, helloWorld: scene (), false); // flip the page to go to the game's main interface CCDirector: sharedDirector ()-> replaceScene (CCTransitionTurnOffTiles: create (2, HelloWorld :: scene (); // the random square disappears into the game's main interface // CCTransitionPageTurn // function: Create a page flip transition animation // parameter 1: duration of the transition action // parameter 2: Object of the switched target scenario // parameter 3: whether to flip the page reversely // CCTransitionTurnOffTiles // role: create a transition animation with a random square disappear // parameter 1: duration of the Transition action // parameter 2: object in the switched target scenario }}
More scenarios:

[1]: CCTransitionCrossFade: create (time, target scenario );

// Fade down to another scenario

[2]: CCTransitionFade: create (time, target scenario );

// This scenario fades out and then appears in another scenario.

[3]: CCTransitionFadeBL: create (time, target scenario );

// This scenario disappears from the upper right corner to the lower left corner to another scenario.

[4]: CCTransitionFadeDown: create (time, target scenario );

// This scenario disappears from top to bottom to another scenario

[5]: CCTransitionFadeTR: create (time, target scenario );

// This scenario disappears from the lower left corner to the upper right corner to another scenario

[6]: CCTransitionFadeUp: create (time, target scenario );

// This scenario disappears from bottom to top to another scenario

[7]: CCTransitionFlipAngular: create (time, target scenario, style );

// In this scenario, the flip disappears to another scenario (in the top oblique corner)

// Style (can be left empty ):

// KCCTransitionOrientationLeftOver (Flip left to right)

// KCCTransitionOrientationRightOver (FLIP right to left)

[8]: CCTransitionFlipX: create (time, target scenario, style );

// In this scenario, the flip disappears to another scenario (X axis)

// Style (can be left empty ):

// KCCTransitionOrientationLeftOver (Flip left to right)

// KCCTransitionOrientationRightOver (FLIP right to left)

[9]: CCTransitionFlipY: create (time, target scenario );

// In this scenario, the flip disappears to another scenario (Y axis)

// Style (can be left empty ):

// KCCTransitionOrientationUpOver (flip up)

// KCCTransitionOrientationDownOver (flip up and down)

[10]: CCTransitionJumpZoom: create (time, target scenario );

// This scenario disappears and then another scenario appears

[11]: CCTransitionMoveInB: create (time, target scenario );

// Another scenario appears from the bottom.

[12]: CCTransitionMoveInL: create (time, target scenario );

// Another scenario occurs from the left

[13]: CCTransitionMoveInT: create (time, target scenario );

// Another scenario occurs from the whole

[14]: CCTransitionMoveInR: create (time, target scenario );

// In another scenario, the entire image appears from the right.

[15]: CCTransitionPageTurn: create (time, target scenario, bool );

// Page flip switch. If bool is true, the forward flip is performed.

[16]: CCTransitionProgressHorizontal: create (time, target scenario );

// This scenario disappears from left to right and appears in another scenario

[17]: CCTransitionProgressInOut: create (time, target scenario );

// This scenario disappears from the center to the four sides.

[18]: CCTransitionProgressOutIn: create (time, target scenario );

// This scenario disappears from four to the middle and appears in another scenario

[19]: CCTransitionProgressRadialCCW: create (time, target scenario );

// This scenario disappears to another scenario counterclockwise.

[20]: CCTransitionProgressRadialCW: create (time, target scenario );

// This scenario disappears clockwise to another scenario

[21]: CCTransitionProgressVertical: create (time, target scenario );

// This scenario disappears from top to bottom and appears in another scenario

[22]: CCTransitionRotoZoom: create (time, target scenario );

// After the rotation disappears in this scenario, the rotation occurs in another scenario.

[23]: CCTransitionShrinkGrow: create (time, target scenario );

// This scenario is reduced and switched to another scenario.

[24]: CCTransitionSlideInB: create (time, target scenario );

// This scenario slides upwards to another scenario

[25]: CCTransitionSlideInL: create (time, target scenario );

// This scenario slides to the Right to another scenario

[26]: CCTransitionSlideInR: create (time, target scenario );

// This scenario slides left to another scenario

[27]: CCTransitionSlideInT: create (time, target scenario );

// This scenario slides down to another scenario

[28]: CCTransitionSplitCols: create (time, target scenario );

// After the three rectangles disappear up and down in this scenario, the three rectangles appear up and down in another scenario.

[29]: CCTransitionSplitRows: create (time, target scenario );

// After the three rectangles disappear from the left and right in this scenario, the three rectangles appear on the left and right in another scenario.

[30]: CCTransitionTurnOffTiles: create (time, target scenario );

// In this scenario, the small square disappears to another scenario.

[31]: CCTransitionZoomFlipAngular: create (time, target scenario, style );

// In this scenario, the flip disappears to another scenario (in the top oblique corner)

// Style (can be left empty ):

// KCCTransitionOrientationLeftOver (Flip left to right)

// KCCTransitionOrientationRightOver (FLIP right to left)

[32]: CCTransitionZoomFlipX: create (time, target scenario, style );

// In this scenario, the flip disappears to another scenario (X axis)

// Style (can be left empty ):

// KCCTransitionOrientationLeftOver (Flip left to right)

// KCCTransitionOrientationRightOver (FLIP right to left)

[33]: CCTransitionZoomFlipY: create (time, target scenario, style );

// In this scenario, the flip disappears to another scenario (Y axis)

// Style (can be left empty ):

// KCCTransitionOrientationUpOver (flip up)

// KCCTransitionOrientationDownOver (flip up and down)





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.