Make a simple windows phone 7 game with cocos2d-x: tombstone mechanism and final work)

Source: Internet
Author: User

After the tutorials in the previous three sections, we already have a good game. However, we have not found any problems. For example, if the background music is back to the menu, the background music is still playing. This experience is unfriendly and should be regarded as a BUG.

Let's modify it. Open the GamePlayScene class. Reload the two methods of the parent class, OnEnter and OnExit. Move the background music that was originally played in the Init of GamePlayLayer to the OnEnter to play the background music and set it to replay. Stop playing background music in OnExit. The Code is as follows:

        public override void onExit()        {            base.onExit();            SimpleAudioEngine.sharedEngine().stopBackgroundMusic();        }        public override void onEnter()        {            base.onEnter();            SimpleAudioEngine.sharedEngine().playBackgroundMusic(@"resource/backgroundmusic",true);        }

In this way, once we are not in a game scenario, background music will naturally stop. Back to the game scene, the background music can also start playing.

Now there is another problem with the game. After you press the Start menu and return to the desktop, you cannot return to the game by pressing the return key. Careful friends will find that there is an exception in the Debug window and the thread exits. When the thread exits, we naturally cannot go back to the game.

So what is the reason, because we put the reference of a scenario --- GamePlayScene in PhoneApplicationService. This pornographic and violent behavior is regarded as insecure by Microsoft. Therefore, there is a security exception. This exception causes the thread to exit directly. So how can we solve this problem? I can't bear to use this reference .. Although it is considered unsafe. In this case, we will save the country.

Open Game1.cs and add a declaration to the class Game1.

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;


Then add the Deactived and Actived registration of the program in the Game1 constructor.

            PhoneApplicationService.Current.Deactivated += new EventHandler<DeactivatedEventArgs>(Current_Deactivated);            PhoneApplicationService.Current.Activated += new EventHandler<ActivatedEventArgs>(Current_Activated);


Then, GamePlayScene is stored in IsolatedStorage during Deactived. And delete it in PhoneApplicationService, and take it out again during Actived. In fact, storing this item in IsolatedStorage also causes security exceptions. But it can be used after testing ..

void Current_Activated(object sender, ActivatedEventArgs e)        {            Debug.WriteLine("Activated");            GamePlayScene temp;            if (settings.TryGetValue("PlayScene", out temp))            {                PhoneApplicationService.Current.State["PlayScene"] = temp;                settings.Remove("PlayScene");            }        }        void Current_Deactivated(object sender, DeactivatedEventArgs e)        {            CCScene currentScene = CCDirector.sharedDirector().runningScene;            string currentSceneName = currentScene.GetType().ToString();            if (currentSceneName == "cocos2dSimpleGame.Classes.GamePlayScene" || currentSceneName == "cocos2dSimpleGame.Classes.GameOverScene")            {                if (PhoneApplicationService.Current.State.ContainsKey("PlayScene"))                {                    GamePlayScene pScene = (GamePlayScene)PhoneApplicationService.Current.State["PlayScene"];                    settings["PlayScene"] = pScene;                    PhoneApplicationService.Current.State.Remove("PlayScene");                }            }            else            {                if (PhoneApplicationService.Current.State.ContainsKey("PlayScene"))                    PhoneApplicationService.Current.State.Remove("PlayScene");            }            Debug.WriteLine("Deactived");        }


The test is successful. You can use the return key to return the program.

I will not talk about these two events. Learn about the lifecycle of a program.

 

Let's talk about the tombstone mechanism. Now I am using the 7.1 SDK, not the latest SDK. The latest version is 7.11. Now my problem here is that if the xNa program is re-deployed, isolatedstorage will be cleared, but the XAML program will not. After many times of tossing, it is still impossible to run in the background of the program, from the start of the starter can return to the previous running interface. After many attempts, I found that the problem was that the value stored in isolatedstorage in deactived can be obtained in actived, but not in launching. This value is not displayed in isolatedstorage. The re-deployment of isolatedstorage is cleared, so debugging fails. (The strange thing is that I can test it with a small program or store it .). Therefore, after entering the background, the restoration program from the initiator is not completed. This is something that has WP7 characteristics and is not well done.

Now let's do it. The original idea is to use the brute force method to directly Save the scenario. If the scenario enters the background from the starter, the scenario is directly transferred to the saved scenario. In fact, there is also a less violent method, that is, adding two methods to the game layer of the game scenario to set and obtain the variables to be stored for storage and recovery. However, this is much more troublesome.

Now think about it. The game programs on my mobile phone basically cannot be recovered from the starter after entering the background. Maybe it is also the limitation of xNa. After all, there are so many elves that need to be restored. Very slow.

Here, we have a very good game-a rotating turret, thousands of different types of enemies, multiple levels, win/lose scenarios, menu interface, about statement, of course, there are great sound effects!

Project download: http://dl.dbank.com/c0h47u84l3

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.