Cocos2d-xna: horizontal strategic game development experiment 3 Scene and ctor scenario Switching

Source: Internet
Author: User

In the previous article, we used CCSprite and CCMenu to build the initial scenario and explained the problem about optimizing program content. A good game may have many combinations of scenarios, each scenario manages its own scripts, that is, rules. How does one jump to the scenario throughout the game? First look:

This figure is from the ccctor ctor class parsing diagram of Elvis. You don't have to look at it too comprehensively. You just need to look at the top of the page. As mentioned above, there is a director (CCDirector) in the game to manage the entire scenario, to make the switching scenario better, You need to complete other scenarios according to the previous scenario creation method, such as the level selection interface, game interface, and game end interface.

Complete all scenarios

Image resources need to be generated before production

The above resources contain the elements of the basic scenario, and the elements used to demonstrate the game interface, open the TexturePackerGUI, and combine them, because there are several relatively large images, you can also split them into multiple plist files to check whether they are wasted. For example, I will pack the images I will use to GameUI01.plist this time, and the preview image will be packaged into GameUI02.plist, add them to the Content:

Now we have two resources, plist. GameUI01 has been loaded in the SceneStart class. To ensure the uniformity of resource loading, we have created a GameRoot class and obtained it using static methods, do not forget to delete addSpriteFramesWithFile in SceneStart:

public class GameRoot{    public static void InitializeResource()    {        CCSpriteFrameCache.sharedSpriteFrameCache().addSpriteFramesWithFile("GameUI01", "images/GameUI01");        CCSpriteFrameCache.sharedSpriteFrameCache().addSpriteFramesWithFile("GameUI02", "images/GameUI02");    }}

Note that you need to set GameRoot. initializeResource (); added to AppDelegate. in cs's applicationDidFinishLaunching function, be sure to add it to the first line of the scenario jump. In this way, image resources are first loaded when the scenario is built.

There may be better solutions, such as placing it in a Loading interface. Here, GameRoot has other functions, just as its name is treated as the game root, we can manage all the scenarios here and use a single example to implement global access control. We can do this:

// Use a global root to manage all scenario instances of the game. public class GameRoot {public static void InitializeResource () {CCSpriteFrameCache. sharedSpriteFrameCache (). addSpriteFramesWithFile ("GameUI01", "images/GameUI01"); CCSpriteFrameCache. sharedSpriteFrameCache (). addSpriteFramesWithFile ("GameUI02", "images/GameUI02");} private static SceneStart _ SceneStart; public static SceneStart pSceneStart {get {if (_ SceneStart = null) _ SceneStart = new SceneStart (); return _ SceneStart;} private static SceneSelect _ SceneSelect; public static SceneSelect pSceneSelect {get {if (_ SceneSelect = null) _ SceneSelect = new SceneSelect (); return _ SceneSelect;} private static SceneGame _ SceneGame; public static SceneGame pSceneGame {get {if (_ SceneGame = null) _ SceneGame = new SceneGame (); return _ SceneGame ;}} private static SceneOver _ SceneOver; public static SceneOver pSceneOver {get {if (_ SceneOver = null) _ SceneOver = new SceneOver (); return _ SceneOver ;}}}

Because of its long history, it is folded. Here, we can ensure that the only single instance can be managed in a unified manner. In any part of the game, we can call GameRoot to obtain the scenario instance.

The following is the SceneSelect class code. If you want to make the selection interface complex, you have to do so. Of course, this is also more interesting. This class of code only implements the basic functions, in the future, when the CCAction class is used, it will be more perfect:

Public class SceneSelect: CCScene {public SceneSelect () {base. init (); // background image CCSprite background = CCSprite. spriteWithSpriteFrameName ("bg_select.png"); background. anchorPoint = new CCPoint (0, 0); this. addChild (background); CCMenuItemSprite btn_back = CCMenuItemSprite. itemFromNormalSprite (CCSprite. spriteWithSpriteFrameName ("btn_back1.png"), CCSprite. spriteWithSpriteFrameName ("btn_back2.png"), this, click_back); CCMenu menu = CCMenu. menuWithItems (btn_back); menu. position = new CCPoint (666,32); this. addChild (menu); CCSprite tab1 = CCSprite. spriteWithSpriteFrameName ("tab_shu1.png"); CCSprite tab2 = CCSprite. spriteWithSpriteFrameName ("tab_wu2.png"); CCSprite tab3 = CCSprite. spriteWithSpriteFrameName ("tab_wei2.png"); tab1.position = new CCPoint (115,430); tab2.position = new CCPoint (335,430); tab3.position = new CCPoint (575,430); this. addChild (tab1); this. addChild (tab2); this. addChild (tab3); CCPoint offset = new CCPoint (150,150); for (int I = 0; I <4; I ++) {for (int j = 0; j <3; j ++) {CCMenuItemSprite level = CCMenuItemSprite. itemFromNormalSprite (CCSprite. spriteWithSpriteFrameName ("btn_level1.png"), CCSprite. spriteWithSpriteFrameName ("btn_level2.png"), this, click_level); menu = CCMenu. menuWithItems (level); menu. position = new CCPoint (offset. x + 160 * I, offset. y + 85 * j); this. addChild (menu) ;}} private void click_back (CCObject s) {} private void click_level (CCObject sender ){}}

Implementation of scenario selection:

In the scenario where a level is selected, all entries are redirected to SceneGame. The corresponding level will be designed later.

The following is the SceneGame Code for the game scenario. Here the Code is only pasted with a picture, and a "sign" button is added. This button will be used as a trigger for the end of the game in this example:

Public class SceneGame: CCScene {public SceneGame () {base. init (); // background image CCSprite background = CCSprite. spriteWithSpriteFrameName ("bg_game.png"); background. anchorPoint = new CCPoint (0, 0); this. addChild (background); // return button CCMenuItemSprite btn_attack = CCMenuItemSprite. itemFromNormalSprite (CCSprite. spriteWithSpriteFrameName ("btn_soldierattack1.png"), CCSprite. spriteWithSpriteFrameName ("btn_soldierattack2.png"), this, click_attack); CCMenu menu = CCMenu. menuWithItems (btn_attack); menu. position = new CCPoint (732, 36); this. addChild (menu);} private void click_attack (CCObject sender ){}}

The code works as follows:

The last is the end scenario:

Public class SceneOver: CCScene {public SceneOver () {base. init (); // background image CCSprite background = CCSprite. spriteWithSpriteFrameName ("bg_over.png"); background. anchorPoint = new CCPoint (0, 0); this. addChild (background); // text CCSprite title = CCSprite. spriteWithSpriteFrameName ("text_over.png"); title. position = new CCPoint (CCDirector. shareddire (). getWinSize (). width/2, CCDirector. shareddire (). getWinSize (). height/2 + 150); this. addChild (title); // return button CCMenuItemSprite btn_back = CCMenuItemSprite. itemFromNormalSprite (CCSprite. spriteWithSpriteFrameName ("btn_back1.png"), CCSprite. spriteWithSpriteFrameName ("btn_back2.png"), this, click_back); CCMenu menu = CCMenu. menuWithItems (btn_back); menu. position = new CCPoint (666, 32); this. addChild (menu);} private void click_back (CCObject s ){}}

The effect is as follows:

 

Scenario switching Transition

Now that we have completed the basic scenario and want to switch the scenario, we will use the three CCDirector class methods below:

PopScene (): returns to the previous scenario.

PushScene (CCScene pScene): switches to a specified scenario.

ReplaceScene (CCScene pScene): replace the current scenario

In CCDirector, scenario management is a queue. It is difficult to write instructions. The following figure shows the queue:

The scenario queue has been managed for you, so you don't have to worry too much about it.

How can this be reflected in the code? You only need to process the corresponding delegate event. For example, you can click Start on the Start interface to send pushScene to the SceneSelect scenario, and click return to execute popScene. replaceScene is simple, it is used to replace SceneGame with SceneOver at the end of the game.

The added code is as follows:

SceneStart:

private void click_start(CCObject sender){    CCDirector.sharedDirector().pushScene(GameRoot.pSceneSelect);}

SceneSelect:

private void click_back(CCObject s){    CCDirector.sharedDirector().popScene();}private void click_level(CCObject sender){    CCDirector.sharedDirector().pushScene(GameRoot.pSceneGame);}

SceneGame:

private void click_attack(CCObject sender){    CCDirector.sharedDirector().replaceScene(GameRoot.pSceneOver);}

SceneOver:

private void click_back(CCObject s){    CCDirector.sharedDirector().popScene();}

Click the button to view the effect.

Transfer Effect

Finally, is it very monotonous to change the effect of the transfer and click directly? More beautiful transfer effect has been provided in the engine, you can see the transition Part Of The cocos2d-xna code, dozens of switching effects let choose:

The specific usage is very simple. For example, when we click the "Start" button on the Start interface, we can perform a CCTransitionFade transfer effect. This effect can make the screen black and then turn it into a new scenario, in SceneStart. add the following code to the click_start method in cs:

private void click_start(CCObject sender){    var s = CCTransitionFade.transitionWithDuration(0.5f, GameRoot.pSceneSelect);    CCDirector.sharedDirector().pushScene(s);}

In this case, it usually takes a long time to switch. In the engine, there is only one line of code, and different Transition has different parameters, interested friends can refer to the test project sample in the cocos2d-xna of OpenXLive transplantation, which shows a lot of cut field effect, here is no longer too much demonstration.

This example project: https://github.com/Nowpaper/SanguoCommander_cocos2dxna_Sample
In this example, the project name is SanguoCommander3.

This article mainly describes the deep production and Transition usage of the scenario, mainly for the scenario Scene content, the next article will use CCLayer, and will complete complex interface interaction compiling with CCAction.

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.