(No. 00005) iOS pop-up game (III): Starting from the main scenario

Source: Internet
Author: User

(No. 00005) iOS pop-up game (III): Starting from the main scenario

 

I also read the code of the entire project in a rough way. There are many examples:

Many classes are closely related to other classes, so you may encounter code in some other classes when you explain them later. if I really don't understand it, it's all about the reasons I 've described. It's not related to your understanding ;). you can send a message to me directly following the blog post at any time. If time permits, I will reply to each message.

Starting from MainScene class

The MainScene class is one of the most code classes in the entire project. There are more than 60 methods:

Vc + examples/examples + 3U291bE9mUGFuZGE6PC9wPg0KPHA + PGltZyBhbHQ9 "here write picture description" src = "http://www.bkjia.com/uploads/allimg/151225/04013560J-2.gif" title = "\"/>

The game contains a large number of sub-scenarios and embedded layers, but this is an out-of-the-box question.

The main scenario includes all the user interaction parts and map display parts. In any case, no game can be started without giving players or enemies a space. So let's talk about the map first.

Game Map

We want to automatically generate the following map:

The gray walls are fixed and the red bricks are randomly placed. because there are items and exits under the bricks, we naturally need to divide the whole map into different layers.

I know that Tiled is a great map editing software. Although there are some unsatisfactory aspects in the current version, for example, only tiles can be selected in one gallery on the same layer. however, the Tiled function is still very powerful in general. If I have the opportunity in the future, I will open a series of blog posts about Tiled map creation.

Tiled can also create special layers called object layers. the Object layer is not used to place blocks, but to place geometric perimeter objects. It is used in RPG games but not in the pop-up App, so you don't need to worry about it for the moment.

First, create a blank map:

Copy all the map tmx files and related gallery files to the tileMap folder under the Resources Directory of the project.

Open the MainScene. m file in Xcode. The initialization code is as follows:

-(Void) didLoadFromCCB {_ viewSize = [CCDirector shareddire]. viewSize; // Number of game frames displayed on the screen [CCDirector shareddire]. displayStats = YES; // initialize the speech gesture reader [self initGestureRecognizer]; // other initialization code... // load the blank map to the game _ tileMap = [CCTiledMap tiledMapWithFile: @ "EmptyMap. tmx "]; _ mapHelper = [MapHelper sharedInstance]; _ mapHelper. tileMap = _ tileMap; [self addChild: _ tileMap]; // coordinate CGPoint spawnTileCoord = ccp (0, 0) in the Player Scene; CGPoint spawnPos = [self positionForTileCoord: spawnTileCoord]; [self setViewpointCenter: spawnPos]; // gets the reference of each layer in the map _ bgLayer = [_ tileMap layerNamed: @ "Background"]; _ objectLayer = [_ tileMap layerNamed: @ "Objects"]; _ underObjectsLayer = [_ tileMap layerNamed: @ "UnderObjectsLayer"]; _ propertiesLayer = [_ tileMap layerNamed: @ "PropertiesLayer"]; _ batchNode = [CCNode node]; [_ tileMap addChild: _ batchNode]; // omit other initialization code... // [self initGameData]; // use game data to initialize the current level [self initGameDataFromGD]; // enable game testing // [self test];}

To put it simply, in the initialization method of the MainScene class, first enable display debugging, initialize the gesture reader, and then initialize the instance array and other initialization code. next, let's take an important part: Initialize the map. First, load the blank map and create a MapHelper class instance. There are many convenient methods for calling; create a reference for each layer in the map. Then, create a game leader, and call the initGameDataFromGD method. This includes the randomly generated map code, which we will discuss later.

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.