(No. 00005) iOS pop-up games (6): Game data initialization (3)

Source: Internet
Author: User

(No. 00005) iOS pop-up games (6): Game data initialization (3)

 

Now let's take a look at the randomCreateMap Method for initializing the map:

// Create a level map randomly based on the current level data on the blank map // The total number of blocks on the map is 30x10 = 300, and the Wall that cannot be moved is discarded (48) 252 spaces left. // subtract the space occupied by the player and the enemy. players appear on the (0, 0) bucket of each level. // to ensure that the player is not stuck, three spaces (0, 0), (0, 1), and (1, 0) cannot be placed with bricks. // the enemy position cannot be placed with bricks, so // The number of bricks that can be placed is 252-3 = 249-> 249-_ gd. curLevelEnemyCount-(void) randomCreateMap {NSInteger rndIdx; NSInteger brickCount = _ gd. curLevelBrickCount; CGPoint tmpTilePos; NSAssert (brickCount <= 249-(_ gd. curLevelFSCount + _ gd. curLevelFS GhostCount), @ "too success bricks !!! "); // Delete the placeholder brick at the object Layer (0, 0) [self removeObjectAtTileCoord: ccp (0, 0)]; NSMutableArray * bricksAry = [NSMutableArray array]; // randomly place bricks for (int I = 0; I <brickCount; I ++) {rndIdx = arc4random_uniform (_ notWallTiles. count); tmpTilePos = [_ notWallTiles [rndIdx] CGPointValue]; [_ objectLayer setTileGID: 4 at: tmpTilePos]; [bricksAry addObject: [NSValue usage: tmpTilePos]; [_ notWallTiles removeObjectAtInd Ex: rndIdx];} // randomly placed game item GameProperty * gp = [GameProperty sharedInstance]; GamePropertyType gpType; for (int I = 0; I <_ gd. curLevelGPCount; I ++) {gpType = [gp randomGP]; NSAssert (bricksAry. count! = 0, @ "there are too few remaining bricks to place the remaining items"); rndIdx = arc4random_uniform (bricksAry. count); tmpTilePos = [bricksAry [rndIdx] CGPointValue]; // randomly place a prop [gp putGPAt: tmpTilePos InLayer: _ propertiesLayer type: gpType]; // There are items under the brick, so remove it from bricksAry [bricksAry removeObjectAtIndex: rndIdx];} // SET _ propertiesLayer) delete the placeholder block of the position [gp removeGPAt: ccp (0, 0) InLayer: _ propertiesLayer]; NSInteger exitTileID = [_ underObjectsLayer tileGIDAt: ccp (0, 0)]; rndIdx = arc4random_uniform (bricksAry. count); tmpTilePos = [bricksAry [rndIdx] CGPointValue]; // randomly placed egress [_ underObjectsLayer setTileGID: exitTileID at: tmpTilePos]; [bricksAry removeObjectAtIndex: rndIdx]; _ exitTilePos = tmpTilePos; // Delete the placeholder block at the _ underObjectsLayer () position [_ underObjectsLayer removeTileAt: ccp (0, 0)];}

Amount... To avoid confusion, I made a detailed comment. Let's take a look at the code in sequence.

First, an assertion ensures that there are enough bricks to use, because there are items and exits under the bricks. If there are too few bricks, they cannot completely cover the underlying items.

Delete the tiles at the (0, 0) Position of the map object Layer. Why do we need to place an unused tile in the map layer? The Tiled tool has a small problem. As I mentioned in the previous blog, it is not feasible to dynamically add tiles at a certain layer.

I have also tried it. Although the tile Map Method in Cocos2D has a method that associates a gallery with a certain layer, the actual operation fails and always fails.

So here we first place a tile in the Tiled, which is equivalent to associating a gallery, and then we can add or remove other tiles at will during operation.

Return to the code stream, and then initialize the bricks randomly based on the number of bricks specified in the current level.

Similarly, the Code for randomly placing items is followed by the number of items specified. here, we can use "real" items to generate items based on the probability of the rare level of items. Otherwise, some customs clearance items may increase in quantity, compromising the game balance.

Finally, the export is placed and the generated export coordinates are saved to the _ exitTilePos instance variable for future operations.

This is basically the game initialization content. Many children's shoes may wonder where the game data comes from. This is actually stored in the plist file and will be discussed in the next article.

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.