(No. 00005) iOS pop-up games (5): Game data initialization (2)
Next, let's take a look at the methods mentioned in the previous article. First, we will introduce the updateStateLabel method:
# Pragma mark MainScene game-related auxiliary method // update the protagonist status tag in the format of: X-ray invincible burst through the wall via bullet Remote Control-(void) updateStateLabel {NSString * state = [NSString stringWithFormat: @ "% @", _ isPlayerSpeedUp? @ "Shen Su": @ "", _ player. isInvincible? @ "Invincible ":@"",! _ IsFearBomb? @ "Burst": @ "", _ player. canw.ssbrick? @ "Wall-crossing": @ "", _ player. canw.ssbomb? @ "Wear bullet": @ "", _ isManualExplode? @ "Remote Control": @ ""]; [_ hudLayer updateStateLabel: state];}
The code is easy to understand, that is, the status string is created based on the current status of the main character and then displayed in the tag. Of course, the more desirable way is to use the icon for display, which can be reserved for future extension.
Next, let's take a look at the initNoWallTiles method. The function of this method is to traverse the block coordinates of all non-wall blocks, prepare for subsequent method calls, and serve as a cache data change speed:
// Initialization is not the coordinate of all tiles of the wall. Three tiles (0, 0), (0, 1), and (1, 0) must be excluded. -(void) initNoWallTiles {CGPoint badTilePos1 = ccp (0, 0); CGPoint badTilePos2 = ccp (0, 1); CGPoint badTilePos3 = ccp (1, 0 ); for (int x = 0; x <_ tileMap. mapSize. width; x ++) {for (int y = 0; y <_ tileMap. mapSize. height; y ++) {CGPoint tilePos = ccp (x, y); if (CGPointEqualToPoint (tilePos, badTilePos1) | CGPointEqualToPoint (tilePos, badTilePos2) | CGPointEqu AlToPoint (tilePos, badTilePos3) {continue;} else if (! [Self isWallAtTileCoord: tilePos]) {[_ notWallTiles addObject: [NSValue valueWithCGPoint: tilePos] ;}} NSAssert (_ notWallTiles. count = 249, @ "_ notWallTiles must equ 249 ");}
The Code traverses all tiles in sequence and adds the coordinates of non-wall tiles to the _ notWallTiles array. In this way, we can easily use the data in the random map generation method. note that three coordinates are excluded:
(0, 0), (0, 1), and (1, 0)
Because the game's protagonist needs to be created at (0, 0), blocks (0, 1) and (1, 0) cannot be placed. Otherwise, the protagonist will "die ", finally, an assertion is used to check whether the number of non-wall tiles is 249. This is calculated manually. The total number is 30x10 = 300 blocks, subtract 6*8 = 48 non-destructible walls, and then subtract 3 of the above coordinates. The remaining 249 blocks can be operated.