Zhiyi Cocos2D-iPhone game development tutorial 006

Source: Internet
Author: User

 In the previous chapter, we talked about the scenario rolling of the game, which mainly includes3Type: vertical, horizontal, and vertical. Regardless of the image scrolling mode, you must travel the main character in the map. When traveling, you need to determine:

1) Whether an obstacle is encountered.

2) Whether it is hit by an enemy shell.

The above two judgments involve a very important concept in the game: collision detection (Collision Detection). Based on the previous chapter, this chapter describes how the Bishop genie roam in the map, how to implement collision detection, and how to hit the enemy through artillery. And give a simple enemyAISimulation. In short, after completing this chapter, readers can start to write basic simple games like the "tank Wars.

Is our exampleZyg007Game screen:

Game Architecture Programming Model of the game

Before proceeding with the example, let's give a brief overview of the overall programming model of the game. Every game is a so-called realistic simulation system: According to the predefined frequency, continuously output the status of the virtual world to the target screen (the number of frames per second is essentially the number of times the screen is re-painted per second) to simulate the display of the virtual world. User input and various Internal timer triggersProgramThe logic modifies the state of the virtual world by modifying the memory variable to realize the movement and scenario changes of the virtual world. For example:

As shown in, image engines per second30Times, the virtual world depicted by memory data is constantly paintedIPhoneOn the screen, this is the so-called30Frame/Seconds.

Generally3Independent Program Logic forms the main programming model of the game program. They are constantly modifying memory data:

1) User input: The Player uses "Touch"IPhoneScreen, and various commands are issued to the protagonist in the game: Move Up, down, left, right, and launch a gun. These commands directly change the status of the protagonist genie in the game.

2) AIEngine commands: commands from servers, commands from servers, and commands from hostile gamers in online games. These commands are the status change commands for non-player control genie.

3) Various timing logics. The first two types of commands directly modify the status of the Game Genie. Various Timing Check logics determine the possible triggering genie or environmental status changes based on the mutual location information of the Elves:

 I. Shells hit the local tank, causing the tank to disappear after the explosion, or the shells hit the brick wall, causing the brick wall to be broken and the road to pass. The other is the game prompt information update.

 II. Periodically count the number of remaining enemies and the number of remaining players ".

 III. Physical engine: displays the interaction between sprite Objects Based on Force Analysis.

 IV. Cocos2D-iPhoneBuilt-in actions and screen change effects.

The above is the main internal architecture of the game and is the basis for our understanding of game programming. This is very different from our common functional-oriented software programming. In contrast, it is difficult to find the game program debugging according to the normal single-step execution.BugUsually, you need to analyze the game running logs to find problems.

Cocos2d-iphone Programming Model

 Cocos2d-iPhoneThe game engine is also designed based on the above concepts. Here we will give a holistic overview of this image engine.

1) Memory Data.

CocosnodeIs the most basic data unit, throughAddchildFunctions are interconnected.CocosnodeInstances of the derived class form the overall memory data set of the game. Remember the tutorial2?

Each scenario represents the virtual gaming world of the current screen.DirectorThe object switch completes the change of each level of the game. All content in each scenario is composedCocosnode.

2) Update engine.

We further refine the previous figure:

The engine that constantly updates images at a predetermined frequency isDirectorObject,DirectorObject To implement this engine function includes the following two core content:

A) How to callMainloopFunction

Slave0.8.2Start,Cocos2d-iphoneStart to support4TypeDirectorWorking mode, which4The core difference between heavy mode is how to callMainloopFunction:

L Ccdirectortypenstimer: PassCocoaOfNstimerScheduled callMainloop. ThereforeUikitBut the execution efficiency is the slowest. You can set the maximum number of frames per second.

L Ccdirectortypemainloop: This is a passWhileLoop To continuously call and executeMainloopCannot matchUikitIntegration, high execution efficiency, the maximum number of frames per second cannot be set.

L Ccdirectortypethreadmainloop: AndCcdirectortypemainloopProcessing and features are similar, but letMainloopRun in the main thread.

L Ccdirectortypedisplaylink: ExploitationIphoneos 3.1New features, higherNstimerThe execution efficiencyUikit.

Regardless of the above work mode,MainloopThe basic mode of being called at a certain interval remains unchanged.CcdirectortypenstimerThis is the default working mode. Considering the battery problem of the handheld device, we recommend that you maintain the default mode when the real-time requirement of the game is not very high. The example in this chapter is the default mode.

 Cocos2d-iPhoneIn0.8.2Previously, onlyCcdirectortypenstimerAndCcdirectortypemainloopMethod.

B) MainloopFunction execution Content Analysis

MainloopThe function mainly performs the following two tasks:

L Trigger timing logic

Key call statements:[[Ccscheduler sharedscheduler] TICK: DT];

PairCocos2d-iPhoneSource codeIt indicates that the calling point of the entire mechanism is as follows:

[Self schedule: @ selector (keepdoing) interval: 1/30];

Cocos2d-iPhoneBuilt-in performer of various actionsActionmanagerThis mechanism is used for animation.

L Present current scenario

Key call statements:[Runningscene _ visit];

This function will cause allCocosnodeTheDrawThe function is called one by one based on the parent-child hierarchy, so that all game images are displayed.

3) Player instruction

We are in5The "Touch" event processing mechanism described in this chapter.

4) Timer

EveryCocosnodeOfScheduleMethod, and the built-in SingletonSharedschedulerThis is what we talked about earlier.[[Scheduler sharedscheduler] TICK: DT]. So far, we can see thatCocos2dDoes not increase the overall overhead of the system. All the scheduled call logic, whether it is the system'sActionOr the game development logic is ultimately implemented in a unified call:SchedulerClassTickMethod.

 Through the above analysis, I hope readers canCocos2d-iPhoneThe programming architecture of the game engine has a clear and holistic understanding:

1) Cocos2d-iPhoneDifferent update Implementation mechanisms are provided:NstimerAndWhileLoop.

2) MainloopFunction guarantee:

A) Status update is executed before the screen is drawn.

B) Various timers are called in line sequence.

Therefore, there are various timer-based (Schedule. In addition, at each specific processing time point of the timing logic, it can be handled in a static manner, that is, there is no memory object that changes at the same time. Modifications to the status of memory objects are performed in a continuous queue.

3) We can safely useCocos2d-iPhoneThe various actions and effects they provide can coexist with our specific Logic Programs, because the common foundation of everyone is the same. Do not directly useNstimerAnd their own timer.

...

If you are interested in this article,You can download the complete PDF here..Sample SourceCodeDownload.

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.