Cocos2d-iphone magic tower layer 20 Part 6

Source: Internet
Author: User

Today, let's start with the fifth part. In the previous chapter, we made warriors and monsters ready to fight, but we will find a problem: the warrior's blood volume has been reduced, however, the life value on the right side does not change. Next, we will start to add the update Methods of these labels so that they will change with the progress. The game data is updated in the Herohp class and the code to be added is Herohp. h. Add the Code [html]-(void) updateHeroHp;-(void) updateHeroAttack;-(void) updateHeroDefense;-(void) updateCoin;-(void) updateExperience; -(void) updateGrade;-(void) updateKey; Herohp. m to add the Code [html]-(void) updateHeroHp {[maid setString: [NSString stringWithFormat: @ "life % d", self. hero. HP];}-(void) updateHeroAttack {[AttLable setString: [NSString stringWithFormat :@ "Attack % d", self. hero. attack];}-(void) updateHeroDefense {[DefLable setString: [NSString stringWithFormat: @ "defense % d", self. hero. defense];}-(void) updateCoin {[CoinLable setString: [NSString stringWithFormat: @ "gold coin % d", self. coin];}-(void) updateExperience {[ExperienceLable setString: [NSString stringWithFormat: @ "experience % d", self. experience];}-(void) updateGrade {[GradeLable setString: [NSString stringWit HFormat: @ "% d", self. grade];}-(void) updateKey1 {[Key1Lable setString: [NSString stringWithFormat: @ "% d", self. yellowKey]; [Key2Lable setString: [NSString stringWithFormat: @ "% d", self. blueKey]; [Key3Lable setString: [NSString stringWithFormat: @ "% d", self. redKey];} with these update methods, we only need to call the corresponding update method when the data changes. First, we need to use Game01.m to eliminate the enemy (removeEnemy) add a line of code [html] [herohp updateHeroHp] In the method, and add herohp = [Herohp sharedH in the initialization method P]; then our warrior's blood volume will change. Next we will start to let our warriors really "eat" the items on the map. First we will introduce our Items 1. Red Blood bottle (+ life 200) 2. Blue Blood bottle (+ life 500) 3. Ruby (+ attack 3) 4. Sapphire (+ defense 3) 5. Red key (+ 1) 6. Blue key (+ 1) 7. Yellow key (+ 1) 8. Rhubarb key (various keys + 1) 9. Gold Coins (+ 500) 10. Upgrade mark 11, insight into Pin 12, floor aircraft 13, Red dragonfly head 14, cross 15, life magic bottle 16, sword, shield, etc. The following is the code. We can get it based on the block attributes. corresponding to the attribute value, add [html] NSDictionary * props = [self. curtitleMap propertiesForGID: item_tileGid]; NSString * value = [props valueForKey: @ "HP"]; NSString * value1 = [props valueForKey: @ "Attack"]; NSString * value2 = [props valueForKey: @ "Defense"]; NSString * value3 = [props valueForKey: @ "key"]; NSString * value4 = [props valueForKey: @ "predict"]; NSString * value5 = [props valueForKey: @ "hoe"]; NSString * value6 = [props valueForKey: @ "double"]; NSString * value7 = [props valueForKey: @ "Promote"]; NSString * value8 = [props valueForKey: @ "grade"]; NSString * value9 = [props valueForKey: @ "coin"]; then, change the corresponding data [html] if (value) {int hp = [value intValue]; _ hero. HP + = hp; [herohp updateHeroHp];} if (value1) {int attack = [value1 intValue]; _ hero. attack + = attack; [herohp updateHeroAttack];} if (value2) {int defense = [value2 intValue]; _ hero. defense + = defense; [herohp updateHeroDefense];} if (value3) {int key = [value3 intValue]; switch (key) {case 1: herohp. yellowKey ++; break; case 2: herohp. blueKey ++; break; case 3: herohp. redKey ++; break; case 4: herohp. yellowKey ++; herohp. blueKey ++; herohp. redKey ++; break; default: break;} [herohp updateKey1];} if (value4) {} if (value5) {} if (value6) {_ hero. HP * = 2; [herohp updateHeroHp];} if (value7) {_ hero. HP * = 1.3; _ hero. attack * = 1.3; _ hero. defense * = 1.3; [herohp updateHeroHp]; [herohp updateHeroAttack]; [herohp updateHeroDefense];} if (value8) {int grade = [value8 intValue]; _ hero. HP ++ = 1000 * grade; _ hero. attack + = 7 * grade; _ hero. defense + = 7 * grade; herohp. grade + = grade; [herohp updateGrade]; [herohp updateHeroHp]; [herohp updateHeroAttack]; [herohp attachment];} if (value9) {www.2cto.com int coin = [value9 intValue] herohp. coin ++ = coin; [herohp updateCoin];} so that our warriors can "eat" various items and run the game: our warriors cannot operate during the fight, so we need to add _ hero in the creatFightScene method. isFighting = YES; and the value is NO at the end of the fight. We want to add the Key Usage and door opening animation, and add [html] _ hero in if (door_tileGid. isFighting = YES; canmove = NO; NSDictionary * props = [self. curtitleMap propertiesForGID: door_tileGid]; NSString * value = [props valueForKey: @ "door"]; if (value) {int type = [value intValue]; bool canoppen = NO; switch (type) {case 0: if (herohp. yellowKey> 0) {herohp. yellowKey --; canoppen = YES;} break; case 1: if (herohp. blueKey> 0) {herohp. blueKey --; canoppen = YES;} break; case 2: if (herohp. redKey> 0) {herohp. redKey --; canoppen = YES;} break; default: break;} if (canoppen) {[herohp updateKey1]; CCTexture2D * heroTexture = [[CCTextureCache sharedTextureCache] addImage: @ "door.png"]; CCSpriteFrame * frame0, * frame1, * frame2, * frame3; // The second parameter indicates the x, y, width, height, the y coordinate frame0 = [CCSpriteFrame frameWithTexture: heroTexture rect: CGRectMake (32 * type, 32*0, 32, 32)] is determined based on direction; frameWithTexture: heroTexture rect: CGRectMake (32 * type, 32*1, 32, 32)]; frame2 = [CCSpriteFrame frameWithTexture: heroTexture rect: CGRectMake (32 * type, 32*2, 32, 32)]; frame3 = [CCSpriteFrame frameWithTexture: heroTexture rect: CGRectMake (32 * type, 32*3, 32, 32)]; NSMutableArray * animFrames = [NSMutableArray array]; [animFrames addObject: frame0]; [animFrames addObject: frame1]; [animFrames addObject: frame2]; [animFrames addObject: frame3]; // loop animation sequence CCAnimation * animation = [CCAnimation Duration: animFrames delay: 0.1f]; CCAnimate * animate = [CCAnimate Duration: animation]; id action = [CCCallFunc actionWithTarget: self selector: @ selector (removeDoor)]; CCSequence * seq = [CCSequence actions: animate, action, nil]; // [[self. curtitleMap. door tileAt: towerLoc] runAction: seq];} else _ hero. isFighting = NO;} first checks whether the corresponding key is used to open the door. If yes, the number of corresponding keys is reduced and the opening animation is played, to play an animation, you must first load the animation texture set. After obtaining the animation image of each frame, you can load it into the animation sequence and finally play the animation, at the end of the playback, call the method to remove the door [html] // remove the opened door-(void) removeDoor {_ hero. isFighting = NO; canmove = YES; [self. curtitleMap. door removeTileAt: towerLoc];}

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.