原文連接http://blog.csdn.net/fire_work/article/details/8608551 html
爲了感謝你們對個人支持這裏給你們提供一下這八部分的代碼下載昨天咱們的勇士能夠穿越樓層了,可是咱們的遊戲界面還不夠「生動」,接下來咱們就要 測試
添加一些代碼使其「生動」起來。 動畫
這部分代碼很少可是卻能讓地圖上的怪物都動來,那麼就讓咱們來添加一下這部分代 atom
碼吧,這部分代碼須要添加到TitledMap.m中titledMapAnalytic方法中if(heroPoint_tileGid) spa
循環的下面如圖: .net
if (enemy_tileGid) { NSDictionary *props = [self propertiesForGID:enemy_tileGid]; NSString *value = [props valueForKey:@"enemy"]; int type = [value intValue]; CCTexture2D *Texture = [[CCTextureCache sharedTextureCache] addImage:@"enemy.png"]; CCSpriteFrame *frame0 ,*frame1,*frame2,*frame3; //第二個參數表示顯示區域的x, y, width, height,根據type來肯定顯示的y座標 frame0 = [CCSpriteFrame frameWithTexture:Texture rect:CGRectMake(32*0, type*32, 32, 32)]; frame1 = [CCSpriteFrame frameWithTexture:Texture rect:CGRectMake(32*1, type*32, 32, 32)]; frame2 = [CCSpriteFrame frameWithTexture:Texture rect:CGRectMake(32*2, type*32, 32, 32)]; frame3 = [CCSpriteFrame frameWithTexture:Texture rect:CGRectMake(32*3, type*32, 32, 32)]; NSMutableArray *animFrames = [NSMutableArray array]; [animFrames addObject:frame0]; [animFrames addObject:frame1]; [animFrames addObject:frame2]; [animFrames addObject:frame3]; //循環動畫序列 CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:0.2f]; CCAnimate *animate = [CCAnimate actionWithAnimation:animation]; CCSequence *seq = [CCSequence actions: animate,nil]; [[self.enemy tileAt:towerLoc]runAction:[CCRepeatForever actionWithAction: seq]]; }
這個跟以前的開門動畫加載方式差很少我就很少講了。運行一下游戲,你就會發現咱們的怪物 設計
動起來了,這樣看着是否是更有感受了! code
下面讓咱們隨着遊戲一直來到三樓,直接到商店你會發現商店沒有任何響應,接下來咱們就要 orm
開始添加商店的響應事件了,不過在這以前咱們要先把咱們的商店界面設計好。代碼: htm
ShopLayer.h文件內容
#import <Foundation/Foundation.h> #import "cocos2d.h" @interface ShopLayer : CCLayer { CCLabelTTF *introduction; CCLabelTTF *addHP; CCLabelTTF *addAttack; CCLabelTTF *addDefense; CCLabelTTF *exitShop; CCLabelTTF *enter; } @property (nonatomic,assign)int curHP; @property (nonatomic,assign)int curAttack; @property (nonatomic,assign)int curDefense; @property (nonatomic,assign)int curCoin; //當前選中商店編號 @property (nonatomic,assign) int selID; //是否在購買狀態 @property (nonatomic,assign) int isOnShop; @property (nonatomic,retain) CCSprite *selSprite; -(id)initWithID:(int) shopID; @end
#import "ShopLayer.h" #import "Hero.h" #import "Herohp.h" @implementation ShopLayer @synthesize curHP; @synthesize curCoin; @synthesize curAttack; @synthesize curDefense; @synthesize selSprite; @synthesize isOnShop; @synthesize selID; -(void)viewInit { //獲取屏幕大小 CGSize size = [[CCDirector sharedDirector] winSize]; self.selSprite = [CCSprite spriteWithFile:@"logo_select.png"]; self.selSprite.scaleX = 2.0; id blink = [CCBlink actionWithDuration:2 blinks:2]; [self.selSprite runAction:[CCRepeatForever actionWithAction:blink]]; self.selID = 0; //背景 CCTMXTiledMap *background = [CCTMXTiledMap tiledMapWithTMXFile:@"shopbg.tmx"]; background.scale = 2.0; background.position = ccp(40, size.height / 2 - 200); [self addChild:background]; //介紹標籤 introduction = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"勇士你好,這裏是商店你花%d金幣便可選擇一下任意一項",self.curCoin] dimensions:CGSizeMake(500, 100) alignment:UITextAlignmentCenter fontName:@"Verdana-Bold" fontSize:32]; introduction.position = ccp(350, size.height - 165); [self addChild:introduction]; //加血標籤 addHP = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"增長%d血量",self.curHP] fontName:@"Verdana-Bold" fontSize:30]; addHP.position = ccp(350, size.height - 260); [self addChild:addHP]; //加攻標籤 addAttack = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"增長%d攻擊",self.curAttack] fontName:@"Verdana-Bold" fontSize:30]; addAttack.position = ccp(350, size.height - 320); [self addChild:addAttack]; //加防標籤 addDefense = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"增長%d防護",self.curDefense] fontName:@"Verdana-Bold" fontSize:30]; addDefense.position = ccp(350, size.height - 380); [self addChild:addDefense]; //離開商店 exitShop = [[CCLabelTTF alloc] initWithString:@"離開商店" fontName:@"Verdana-Bold" fontSize:30]; exitShop.position = ccp(350, size.height - 440); [self addChild:exitShop]; //肯定 enter = [[CCLabelTTF alloc] initWithString:@"肯定" fontName:@"Verdana-Bold" fontSize:30]; enter.position = ccp(350,size.height - 500); [self addChild:enter]; [self addChild:self.selSprite]; self.selSprite.position = addHP.position; } -(id)initWithID:(int) shopID { if ((self = [super init])) { if (shopID == 1) { self.curCoin = 25; self.curAttack = 4; self.curDefense = 4; self.curHP = 800; } else { self.curCoin = 100; self.curAttack = 20; self.curDefense = 20; self.curHP = 4000; } [self viewInit]; } return self; } @end
首先咱們要在if(other_tileGid)方法中添加如下代碼:
NSDictionary *props = [self.curtitleMap propertiesForGID:other_tileGid]; NSString *value = [props valueForKey:@"shop"]; int type = [value intValue]; if (type) { _hero.isFighting = YES; shop = [[ShopLayer alloc] initWithID:type]; shop.isOnShop = YES; [self addChild:shop]; }
在咱們這個遊戲中是有兩個商店的,因此咱們在初始化商店的時候經過商店編號來識別。
到這裏咱們的商店界面出現了,下面就要開始添加商店操做了,這部分代碼須要添加到
Game01.m文件的觸摸響應事件中去
if (shop.isOnShop) { for (int i = 0; i <= 4; i++) { CGRect Rect1 = CGRectMake(200, size.height - 290 - i*60, 400, 60); //檢測觸點是否在控件區 if (CGRectIntersectsRect(Rect, Rect1)) { if (i == 4) { if (herohp.Coin >= shop.curCoin) { switch (shop.selID) { case 0: _hero.HP += shop.curHP; [herohp updateHeroHp]; break; case 1: _hero.Attack += shop.curAttack; [herohp updateHeroAttack]; break; case 2: _hero.Defense += shop.curDefense; [herohp updateHeroDefense]; break; default: break; } herohp.Coin -= shop.curCoin; [herohp updateCoin]; } } else if (i == 3) { _hero.isFighting = NO; shop.isOnShop = NO; [self removeChild:shop cleanup:YES]; } else shop.selID = i; if (i<3) { shop.selSprite.position = ccp(350, size.height - 260 - i*60); } } } }
那麼咱們就要在消除怪物的時候更新一下金幣和經驗了代碼以下:
canmove = YES; herohp.Coin += self.selenemy.Coin; herohp.Experience += self.selenemy.Experience; [herohp updateCoin]; [herohp updateExperience];
self.HP = 10000; self.Attack = 1000; self.Defense = 1000;
你會看到兩個npc,地圖左邊的是神祕老人,右邊的是商人,神祕老人是能夠說是經驗商店
在這裏你能夠用經驗提高勇士的能力;商人是賣鑰匙的,在這裏你能夠買到各類鑰匙。
講到這裏咱們又須要添加兩個類Business和Buykey跟商店差很少這裏我就很少講了,直接
上代碼:
Business.h代碼:
#import <Foundation/Foundation.h> #import "cocos2d.h" @interface Business : CCLayer { CCLabelTTF *introduction; CCLabelTTF *addGrade; CCLabelTTF *addAttack; CCLabelTTF *addDefense; CCLabelTTF *exitShop; CCLabelTTF *enter; } @property (nonatomic,assign)int curUpgrade; @property (nonatomic,assign)int curAttack; @property (nonatomic,assign)int curDefense; @property (nonatomic,assign)int curExperience; @property (nonatomic,assign)int curExperience2; //當前選中商店編號 @property (nonatomic,assign) int selID; //是否在購買狀態 @property (nonatomic,assign) int isOnShop; @property (nonatomic,retain) CCSprite *selSprite; -(id)initWithID:(int) shopID; @end
#import "Business.h" @implementation Business @synthesize curUpgrade; @synthesize curExperience; @synthesize curExperience2; @synthesize curAttack; @synthesize curDefense; @synthesize selSprite; @synthesize isOnShop; @synthesize selID; -(void)viewInit { //獲取屏幕大小 CGSize size = [[CCDirector sharedDirector] winSize]; self.selSprite = [CCSprite spriteWithFile:@"logo_select.png"]; self.selSprite.scaleX = 2.0; id blink = [CCBlink actionWithDuration:2 blinks:2]; [self.selSprite runAction:[CCRepeatForever actionWithAction:blink]]; self.selID = 0; //背景 CCTMXTiledMap *background = [CCTMXTiledMap tiledMapWithTMXFile:@"shopbg.tmx"]; background.scale = 2.0; background.position = ccp(35, size.height / 2 - 200); [self addChild:background]; //介紹標籤 introduction = [[CCLabelTTF alloc] initWithString:@"勇士你好,你能夠用經驗值提高本身的能力" fontName:@"Verdana-Bold" fontSize:30]; introduction = [[CCLabelTTF alloc] initWithString:@"勇士你好,你能夠用經驗值提高本身的能力" dimensions:CGSizeMake(500, 100) alignment:UITextAlignmentCenter fontName:@"Verdana-Bold" fontSize:32]; introduction.position = ccp(350, size.height - 200); [self addChild:introduction]; //加血標籤 addGrade = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"%d經驗提高%d級",self.curExperience,self.curUpgrade] fontName:@"Verdana-Bold" fontSize:30]; addGrade.position = ccp(350, size.height - 260); [self addChild:addGrade]; //加攻標籤 addAttack = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"%d經驗增長%d攻擊",self.curExperience2,self.curAttack] fontName:@"Verdana-Bold" fontSize:30]; addAttack.position = ccp(350, size.height - 320); [self addChild:addAttack]; //加防標籤 addDefense = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"%d經驗增長%d防護",self.curExperience2,self.curDefense] fontName:@"Verdana-Bold" fontSize:30]; addDefense.position = ccp(350, size.height - 380); [self addChild:addDefense]; //離開商店 exitShop = [[CCLabelTTF alloc] initWithString:@"離開商店" fontName:@"Verdana-Bold" fontSize:30]; exitShop.position = ccp(350, size.height - 440); [self addChild:exitShop]; //肯定 enter = [[CCLabelTTF alloc] initWithString:@"肯定" fontName:@"Verdana-Bold" fontSize:30]; enter.position = ccp(350,size.height - 500); [self addChild:enter]; [self addChild:self.selSprite]; self.selSprite.position = addGrade.position; } -(id)initWithID:(int) shopID { if ((self = [super init])) { if (shopID == 4) { self.curUpgrade = 1; self.curAttack = 5; self.curDefense = 5; self.curExperience = 100; self.curExperience2 = 30; } else { self.curUpgrade = 3; self.curAttack = 17; self.curDefense = 17; self.curExperience = 270; self.curExperience2 = 90; } [self viewInit]; } return self; } @end
#import <Foundation/Foundation.h> #import "cocos2d.h" @interface Buykey : CCLayer { CCLabelTTF *introduction; CCLabelTTF *Key1Lable; CCLabelTTF *Key2Lable; CCLabelTTF *Key3Lable; CCLabelTTF *exitShop; CCLabelTTF *enter; } @property (nonatomic,assign)int yellowKeyPrice; @property (nonatomic,assign)int blueKeyPrice; @property (nonatomic,assign)int redKeyPrice; //當前選中商店編號 @property (nonatomic,assign) int selID; //是否在購買狀態 @property (nonatomic,assign) int isOnShop; @property (nonatomic,retain) CCSprite *selSprite; -(void)viewInit:(int) shopID; @end
Buykey.m代碼:
#import "Buykey.h" @implementation Buykey @synthesize yellowKeyPrice; @synthesize blueKeyPrice; @synthesize redKeyPrice; @synthesize selSprite; @synthesize isOnShop; @synthesize selID; -(void)viewInit:(int) shopID { if (shopID == 5) { self.yellowKeyPrice = 10; self.blueKeyPrice = 50; self.redKeyPrice = 100; [introduction setString:@"勇士你好,我這裏有各類鑰匙若是你有錢我能夠賣給你"]; } else { self.yellowKeyPrice = 5; self.blueKeyPrice = 25; self.redKeyPrice = 50; [introduction setString:@"勇士你好,若是你有多餘的鑰你能夠賣給我"]; } [Key1Lable setString:[NSString stringWithFormat:@"黃鑰匙(%d)",self.yellowKeyPrice]]; [Key2Lable setString:[NSString stringWithFormat:@"藍鑰匙(%d)",self.blueKeyPrice]]; [Key3Lable setString:[NSString stringWithFormat:@"紅鑰匙(%d)",self.redKeyPrice]]; [self removeChild:self.selSprite cleanup:YES]; id blink = [CCBlink actionWithDuration:2 blinks:2]; [self.selSprite runAction:[CCRepeatForever actionWithAction:blink]]; [self addChild:self.selSprite]; self.selSprite.position = Key1Lable.position; } -(id)init { if ((self = [super init])) { //獲取屏幕大小 CGSize size = [[CCDirector sharedDirector] winSize]; self.selSprite = [CCSprite spriteWithFile:@"logo_select.png"]; self.selSprite.scaleX = 2.0; id blink = [CCBlink actionWithDuration:2 blinks:2]; [self.selSprite runAction:[CCRepeatForever actionWithAction:blink]]; self.selID = 0; //背景 CCTMXTiledMap *background = [CCTMXTiledMap tiledMapWithTMXFile:@"shopbg.tmx"]; background.scale = 2.0; background.position = ccp(35, size.height / 2 - 200); [self addChild:background]; //介紹標籤 //introduction = [[CCLabelTTF alloc] initWithString:@"勇士你好,我這裏有各類鑰匙若是你有錢我能夠賣給你" fontName:@"Verdana-Bold" fontSize:30]; introduction = [[CCLabelTTF alloc] initWithString:@"勇士你好,我這裏有各類鑰匙若是你有錢我能夠賣給你" dimensions:CGSizeMake(500, 100) alignment:UITextAlignmentCenter fontName:@"Verdana-Bold" fontSize:32]; introduction.position = ccp(350, size.height - 200); [self addChild:introduction]; //黃鑰匙標籤 Key1Lable = [[CCLabelTTF alloc] initWithString:@"黃鑰匙(10)" fontName:@"Verdana-Bold" fontSize:30]; Key1Lable.position = ccp(350, size.height - 260); [self addChild:Key1Lable]; //藍鑰匙標籤 Key2Lable = [[CCLabelTTF alloc] initWithString:@"藍鑰匙(50)" fontName:@"Verdana-Bold" fontSize:30]; Key2Lable.position = ccp(350, size.height - 320); [self addChild:Key2Lable]; //紅鑰匙標籤 Key3Lable = [[CCLabelTTF alloc] initWithString:@"紅鑰匙(100)" fontName:@"Verdana-Bold" fontSize:30]; Key3Lable.position = ccp(350, size.height - 380); [self addChild:Key3Lable]; //離開商店 exitShop = [[CCLabelTTF alloc] initWithString:@"離開商店" fontName:@"Verdana-Bold" fontSize:30]; exitShop.position = ccp(350, size.height - 440); [self addChild:exitShop]; //肯定 enter = [[CCLabelTTF alloc] initWithString:@"肯定" fontName:@"Verdana-Bold" fontSize:30]; enter.position = ccp(350,size.height - 500); [self addChild:enter]; [self addChild:self.selSprite]; self.selSprite.position = Key1Lable.position; } return self; } @end
此次咱們要添加到if(npc_tileGid)裏面:
case 4: business = [[Business alloc] initWithID:type]; business.isOnShop = YES; [self addChild:business]; break; case 5: [buykey viewInit:5]; [self addChild:buykey]; buykey.isOnShop = YES; break; case 6: [buykey viewInit:6]; [self addChild:buykey]; buykey.isOnShop = YES; break; case 7: business = [[Business alloc] initWithID:type]; business.isOnShop = YES; [self addChild:business]; break;
if (business.isOnShop) { for (int i = 0; i <= 4; i++) { CGRect Rect1 = CGRectMake(200, size.height - 290 - i*60, 400, 60); //檢測觸點是否在控件區 if (CGRectIntersectsRect(Rect, Rect1)) { if (i == 4) { if (herohp.Experience >= business.curExperience2) { switch (business.selID) { case 0: if (herohp.Experience >= business.curExperience) { _hero.HP += 1000*business.curUpgrade; _hero.Attack += 7*business.curUpgrade; _hero.Defense += 7*business.curUpgrade; herohp.Grade += 1; [herohp updateGrade]; herohp.Experience -= business.curExperience; } break; case 1: if (herohp.Experience >= business.curExperience2) { _hero.Attack += business.curAttack; herohp.Experience -= business.curExperience2; } break; case 2: if (herohp.Experience >= business.curExperience2) { _hero.Defense += business.curDefense; herohp.Experience -= business.curExperience2; } break; default: break; } [herohp updateHeroHp]; [herohp updateHeroAttack]; [herohp updateHeroDefense]; [herohp updateExperience]; } } else if (i == 3) { _hero.isFighting = NO; business.isOnShop = NO; [self removeChild:business cleanup:YES]; } else business.selID = i; if (i<3) { business.selSprite.position = ccp(350, size.height - 260 - i*60); } } } } if (buykey.isOnShop) { for (int i = 0; i <= 4; i++) { CGRect Rect1 = CGRectMake(200, size.height - 290 - i*60, 400, 60); //檢測觸點是否在控件區 if (CGRectIntersectsRect(Rect, Rect1)) { if (i == 4) { switch (buykey.selID) { case 0: if (buykey.yellowKeyPrice > 5) { if (herohp.Coin >= 10) { herohp.Coin -= 10; herohp.YellowKey += 1; } } else { herohp.Coin += 5; herohp.YellowKey -= 1; } break; case 1: if (buykey.yellowKeyPrice > 5) { if (herohp.Coin >= 50) { herohp.Coin -= 50; herohp.BlueKey += 1; } } else { herohp.Coin += 25; herohp.BlueKey -= 1; } break; case 2: if (buykey.yellowKeyPrice > 5) { if (herohp.Coin >= 100) { herohp.Coin -= 100; herohp.RedKey += 1; } } else { herohp.Coin += 50; herohp.RedKey -= 1; } break; default: break; } [herohp updateCoin]; [herohp updateKey1]; } else if (i == 3) { _hero.isFighting = NO; buykey.isOnShop = NO; [self removeChild:buykey cleanup:YES]; } else { buykey.selID = i; } if (i<3) { buykey.selSprite.position = ccp(350, size.height - 260 - i*60); } } } }
在這裏我要感謝這幾天你們對個人支持,大家的支持是我最大的動力!
還有請轉載我文章的朋友們注意添加原文鏈接,謝謝!
在這裏我補充一點內容就是以前的代碼中鑰匙商店沒有初始化,咱們須要在
Game01.m初始化方法中添加buykey = [[Buykey alloc] init];這樣就好了。