博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d-iphone之魔塔20层第八部分
阅读量:6271 次
发布时间:2019-06-22

本文共 36328 字,大约阅读时间需要 121 分钟。

hot3.png

原文链接

 

    昨天我们的勇士可以穿越楼层了,但是我们的游戏界面还不够“生动”,接下来我们就要

添加一些代码使其“生动”起来。

    这部分代码不多但是却能让地图上的怪物都动来,那么就让我们来添加一下这部分代

码吧,这部分代码需要添加到TitledMap.m中titledMapAnalytic方法中if(heroPoint_tileGid)

循环的下面如图:

[html]
  1. if (enemy_tileGid)  
  2.             { 
  3.                 NSDictionary *props = [self propertiesForGID:enemy_tileGid]; 
  4.                 NSString *value = [props valueForKey:@"enemy"]; 
  5.                 int type = [value intValue]; 
  6.                 CCTexture2D *Texture = [[CCTextureCache sharedTextureCache] addImage:@"enemy.png"]; 
  7.                 CCSpriteFrame *frame0 ,*frame1,*frame2,*frame3; 
  8. //第二个参数表示显示区域的x, y, width, height,根据type来确定显示的y坐标
  9.                 frame0 = [CCSpriteFrame frameWithTexture:Texture rect:CGRectMake(32*0, type*32, 32, 32)]; 
  10.                 frame1 = [CCSpriteFrame frameWithTexture:Texture rect:CGRectMake(32*1, type*32, 32, 32)]; 
  11.                 frame2 = [CCSpriteFrame frameWithTexture:Texture rect:CGRectMake(32*2, type*32, 32, 32)]; 
  12.                 frame3 = [CCSpriteFrame frameWithTexture:Texture rect:CGRectMake(32*3, type*32, 32, 32)]; 
  13.                 NSMutableArray *animFrames = [NSMutableArray array]; 
  14.                 [animFrames addObject:frame0]; 
  15.                 [animFrames addObject:frame1]; 
  16.                 [animFrames addObject:frame2]; 
  17.                 [animFrames addObject:frame3]; 
  18.                 //循环动画序列 
  19.                 CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:0.2f]; 
  20.                 CCAnimate *animate = [CCAnimate actionWithAnimation:animation]; 
  21.                 CCSequence *seq = [CCSequence actions: animate,nil]; 
  22.                 [[self.enemy tileAt:towerLoc]runAction:[CCRepeatForever actionWithAction: seq]]; 
  23.             } 
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]];            }

这个跟之前的开门动画加载方式差不多我就不多讲了。运行一下游戏,你就会发现我们的怪物

动起来了,这样看着是不是更有感觉了!

下面让我们随着游戏一直来到三楼,直接到商店你会发现商店没有任何响应,接下来我们就要

开始添加商店的响应事件了,不过在这之前我们要先把我们的商店界面设计好。代码:

ShopLayer.h文件内容

[html]
  1. #import <Foundation/Foundation.h> 
  2. #import "cocos2d.h" 
  3.  
  4. ShopLayer : CCLayer 
  5.     CCLabelTTF *introduction; 
  6.     CCLabelTTF *addHP; 
  7.     CCLabelTTF *addAttack; 
  8.     CCLabelTTF *addDefense; 
  9.     CCLabelTTF *exitShop; 
  10.     CCLabelTTF *enter; 
  11. @property (nonatomic,assign)int curHP; 
  12. @property (nonatomic,assign)int curAttack; 
  13. @property (nonatomic,assign)int curDefense; 
  14. @property (nonatomic,assign)int curCoin; 
  15. //当前选中商店编号 
  16. @property (nonatomic,assign) int selID; 
  17. //是否在购买状态 
  18. @property (nonatomic,assign) int isOnShop; 
  19. @property (nonatomic,retain) CCSprite *selSprite;  
  20.  
  21. -(id)initWithID:(int) shopID; 
  22. @end 
#import 
#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
ShopLayer.m文件内容

[html]
  1. #import "ShopLayer.h" 
  2. #import "Hero.h" 
  3. #import "Herohp.h" 
  4.  
  5. @implementation ShopLayer 
  6. @synthesize curHP; 
  7. @synthesize curCoin; 
  8. @synthesize curAttack; 
  9. @synthesize curDefense; 
  10. @synthesize selSprite; 
  11. @synthesize isOnShop; 
  12. @synthesize selID; 
  13.  
  14. -(void)viewInit 
  15.     //获取屏幕大小 
  16.     CGSize size = [[CCDirector sharedDirector] winSize]; 
  17.     self.selSprite = [CCSprite spriteWithFile:@"logo_select.png"]; 
  18.     self.selSprite.scaleX = 2.0; 
  19.     id blink = [CCBlink actionWithDuration:2 blinks:2]; 
  20.     [self.selSprite runAction:[CCRepeatForever actionWithAction:blink]]; 
  21.     self.selID = 0; 
  22.     //背景 
  23.     CCTMXTiledMap *background = [CCTMXTiledMap tiledMapWithTMXFile:@"shopbg.tmx"]; 
  24.     background.scale = 2.0; 
  25.     background.position = ccp(40, size.height / 2 - 200); 
  26.     [self addChild:background]; 
  27.     //介绍标签 
  28.     introduction = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"勇士你好,这里是商店你花%d金币即可选择一下任意一项",self.curCoin] dimensions:CGSizeMake(500, 100) alignment:UITextAlignmentCenter fontName:@"Verdana-Bold" fontSize:32]; 
  29.     introduction.position = ccp(350, size.height - 165); 
  30.     [self addChild:introduction]; 
  31.     //加血标签 
  32.     addHP = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"增加%d血量",self.curHP] fontName:@"Verdana-Bold" fontSize:30]; 
  33.     addHP.position = ccp(350, size.height - 260); 
  34.     [self addChild:addHP]; 
  35.     //加攻标签 
  36.     addAttack = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"增加%d攻击",self.curAttack] fontName:@"Verdana-Bold" fontSize:30]; 
  37.     addAttack.position = ccp(350, size.height - 320); 
  38.     [self addChild:addAttack]; 
  39.     //加防标签 
  40.     addDefense = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"增加%d防御",self.curDefense] fontName:@"Verdana-Bold" fontSize:30]; 
  41.     addDefense.position = ccp(350, size.height - 380); 
  42.     [self addChild:addDefense]; 
  43.     //离开商店 
  44.     exitShop = [[CCLabelTTF alloc] initWithString:@"离开商店" fontName:@"Verdana-Bold" fontSize:30]; 
  45.     exitShop.position = ccp(350, size.height - 440); 
  46.     [self addChild:exitShop]; 
  47.     //确定 
  48.     enter = [[CCLabelTTF alloc] initWithString:@"确定" fontName:@"Verdana-Bold" fontSize:30]; 
  49.     enter.position = ccp(350,size.height - 500); 
  50.     [self addChild:enter]; 
  51.     [self addChild:self.selSprite]; 
  52.     self.selSprite.position = addHP.position; 
  53. -(id)initWithID:(int) shopID 
  54.     if ((self = [super init]))  
  55.     { 
  56.         if (shopID == 1)  
  57.         { 
  58.             self.curCoin = 25; 
  59.             self.curAttack = 4; 
  60.             self.curDefense = 4; 
  61.             self.curHP = 800; 
  62.         } 
  63.         else 
  64.         { 
  65.             self.curCoin = 100; 
  66.             self.curAttack = 20; 
  67.             self.curDefense = 20; 
  68.             self.curHP = 4000; 
  69.         } 
  70.         [self viewInit]; 
  71.     } 
  72.     return self; 
  73. @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
这部分代码我就不多讲了,下面我们就开始添加Game01.m中调用部分和操作控制

首先我们要在if(other_tileGid)方法中添加以下代码:

[html]
  1. NSDictionary *props = [self.curtitleMap propertiesForGID:other_tileGid]; 
  2. NSString *value = [props valueForKey:@"shop"]; 
  3. int type = [value intValue]; 
  4. if (type)  
  5.    _hero.isFighting = YES; 
  6.    shop = [[ShopLayer alloc] initWithID:type]; 
  7.    shop.isOnShop = YES; 
  8.    [self addChild:shop]; 
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];}
当我们在购买状态时是不能进行除了商店以外的操作的,所以我把勇士的战斗状态赋值为YES

在我们这个游戏中是有两个商店的,所以我们在初始化商店的时候通过商店编号来识别。

到这里我们的商店界面出现了,下面就要开始添加商店操作了,这部分代码需要添加到

Game01.m文件的触摸响应事件中去

[html]
  1. if (shop.isOnShop)  
  2.     { 
  3.         for (int i = 0; i <= 4; i++)  
  4.         { 
  5.             CGRect Rect1 = CGRectMake(200, 
  6.                                       size.height - 290 - i*60, 
  7.                                       400, 
  8.                                       60); 
  9.             //检测触点是否在控件区 
  10.             if (CGRectIntersectsRect(Rect, Rect1)) 
  11.             { 
  12.                 if (i == 4)  
  13.                 { 
  14.                     if (herohp.Coin >= shop.curCoin)  
  15.                     { 
  16.                         switch (shop.selID) 
  17.                         { 
  18.                             case 0: 
  19.                                 _hero.HP += shop.curHP; 
  20.                                 [herohp updateHeroHp]; 
  21.                                 break; 
  22.                             case 1: 
  23.                                 _hero.Attack += shop.curAttack; 
  24.                                 [herohp updateHeroAttack]; 
  25.                                 break; 
  26.                             case 2: 
  27.                                 _hero.Defense += shop.curDefense; 
  28.                                 [herohp updateHeroDefense]; 
  29.                                 break; 
  30.                             default: 
  31.                                 break; 
  32.                         } 
  33.                         herohp.Coin -= shop.curCoin; 
  34.                         [herohp updateCoin]; 
  35.                     } 
  36.                 } 
  37.                 else if (i == 3)  
  38.                 { 
  39.                     _hero.isFighting = NO; 
  40.                     shop.isOnShop = NO; 
  41.                     [self removeChild:shop cleanup:YES]; 
  42.                 } 
  43.                 else 
  44.                     shop.selID = i; 
  45.                 if (i<3)  
  46.                 { 
  47.                     shop.selSprite.position = ccp(350, size.height - 260 - i*60); 
  48.                 } 
  49.             } 
  50.         } 
  51.     } 
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);                }            }        }    }
终于我们的商店可以使用了,但是一个问题来了我们没有金币啊,金币是打怪的时候得到的

那么我们就要在消除怪物的时候更新一下金币和经验了代码如下:

[html]
  1. canmove = YES; 
  2.     herohp.Coin += self.selenemy.Coin; 
  3.     herohp.Experience += self.selenemy.Experience; 
  4.     [herohp updateCoin]; 
  5.     [herohp updateExperience]; 
canmove = YES;    herohp.Coin += self.selenemy.Coin;    herohp.Experience += self.selenemy.Experience;    [herohp updateCoin];    [herohp updateExperience];
这样我们就能够获得金币和经验了,不过为了测试方便,这里我们可以修改一些参数,

[html]
  1. self.HP = 10000; 
  2.         self.Attack = 1000; 
  3.         self.Defense = 1000; 
self.HP = 10000;        self.Attack = 1000;        self.Defense = 1000;
金币和经验也可以适当修改,修改过之后,让我们再次随着勇士的脚步来到5楼吧,这里

你会看到两个npc,地图左边的是神秘老人,右边的是商人,神秘老人是可以说是经验商店

在这里你可以用经验提升勇士的能力;商人是卖钥匙的,在这里你可以买到各种钥匙。

讲到这里我们又需要添加两个类Business和Buykey跟商店差不多这里我就不多讲了,直接

上代码:

Business.h代码:

[html]
  1. #import <Foundation/Foundation.h> 
  2. #import "cocos2d.h" 
  3.  
  4. Business : CCLayer  
  5.     CCLabelTTF *introduction; 
  6.     CCLabelTTF *addGrade; 
  7.     CCLabelTTF *addAttack; 
  8.     CCLabelTTF *addDefense; 
  9.     CCLabelTTF *exitShop; 
  10.     CCLabelTTF *enter; 
  11.  
  12. @property (nonatomic,assign)int curUpgrade; 
  13. @property (nonatomic,assign)int curAttack; 
  14. @property (nonatomic,assign)int curDefense; 
  15. @property (nonatomic,assign)int curExperience; 
  16. @property (nonatomic,assign)int curExperience2; 
  17. //当前选中商店编号 
  18. @property (nonatomic,assign) int selID; 
  19. //是否在购买状态 
  20. @property (nonatomic,assign) int isOnShop; 
  21. @property (nonatomic,retain) CCSprite *selSprite;  
  22.  
  23. -(id)initWithID:(int) shopID; 
  24. @end 
#import 
#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
Business.m代码:

[html]
  1. #import "Business.h" 
  2.  
  3. @implementation Business 
  4. @synthesize curUpgrade; 
  5. @synthesize curExperience; 
  6. @synthesize curExperience2; 
  7. @synthesize curAttack; 
  8. @synthesize curDefense; 
  9. @synthesize selSprite; 
  10. @synthesize isOnShop; 
  11. @synthesize selID; 
  12.  
  13. -(void)viewInit 
  14.     //获取屏幕大小 
  15.     CGSize size = [[CCDirector sharedDirector] winSize]; 
  16.     self.selSprite = [CCSprite spriteWithFile:@"logo_select.png"]; 
  17.     self.selSprite.scaleX = 2.0; 
  18.     id blink = [CCBlink actionWithDuration:2 blinks:2]; 
  19.     [self.selSprite runAction:[CCRepeatForever actionWithAction:blink]]; 
  20.     self.selID = 0; 
  21.     //背景 
  22.     CCTMXTiledMap *background = [CCTMXTiledMap tiledMapWithTMXFile:@"shopbg.tmx"]; 
  23.     background.scale = 2.0; 
  24.     background.position = ccp(35, size.height / 2 - 200); 
  25.     [self addChild:background]; 
  26.     //介绍标签 
  27.     introduction = [[CCLabelTTF alloc] initWithString:@"勇士你好,你可以用经验值提升自己的能力" fontName:@"Verdana-Bold" fontSize:30]; 
  28.     introduction = [[CCLabelTTF alloc] initWithString:@"勇士你好,你可以用经验值提升自己的能力" dimensions:CGSizeMake(500, 100) alignment:UITextAlignmentCenter fontName:@"Verdana-Bold" fontSize:32]; 
  29.     introduction.position = ccp(350, size.height - 200); 
  30.     [self addChild:introduction]; 
  31.     //加血标签 
  32.     addGrade = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"%d经验提升%d级",self.curExperience,self.curUpgrade] fontName:@"Verdana-Bold" fontSize:30]; 
  33.     addGrade.position = ccp(350, size.height - 260); 
  34.     [self addChild:addGrade]; 
  35.     //加攻标签 
  36.     addAttack = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"%d经验增加%d攻击",self.curExperience2,self.curAttack] fontName:@"Verdana-Bold" fontSize:30]; 
  37.     addAttack.position = ccp(350, size.height - 320); 
  38.     [self addChild:addAttack]; 
  39.     //加防标签 
  40.     addDefense = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"%d经验增加%d防御",self.curExperience2,self.curDefense] fontName:@"Verdana-Bold" fontSize:30]; 
  41.     addDefense.position = ccp(350, size.height - 380); 
  42.     [self addChild:addDefense]; 
  43.     //离开商店 
  44.     exitShop = [[CCLabelTTF alloc] initWithString:@"离开商店" fontName:@"Verdana-Bold" fontSize:30]; 
  45.     exitShop.position = ccp(350, size.height - 440); 
  46.     [self addChild:exitShop]; 
  47.     //确定 
  48.     enter = [[CCLabelTTF alloc] initWithString:@"确定" fontName:@"Verdana-Bold" fontSize:30]; 
  49.     enter.position = ccp(350,size.height - 500); 
  50.     [self addChild:enter]; 
  51.     [self addChild:self.selSprite]; 
  52.     self.selSprite.position = addGrade.position; 
  53. -(id)initWithID:(int) shopID 
  54.     if ((self = [super init]))  
  55.     { 
  56.         if (shopID == 4)  
  57.         { 
  58.             self.curUpgrade = 1; 
  59.             self.curAttack = 5; 
  60.             self.curDefense = 5; 
  61.             self.curExperience = 100; 
  62.             self.curExperience2 = 30; 
  63.         } 
  64.         else 
  65.         { 
  66.             self.curUpgrade = 3; 
  67.             self.curAttack = 17; 
  68.             self.curDefense = 17; 
  69.             self.curExperience = 270; 
  70.             self.curExperience2 = 90; 
  71.         } 
  72.         [self viewInit]; 
  73.     } 
  74.     return self; 
  75.  
  76. @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
Buykey.h代码:

[html]
  1. #import <Foundation/Foundation.h> 
  2. #import "cocos2d.h" 
  3.  
  4. Buykey : CCLayer  
  5.     CCLabelTTF *introduction; 
  6.     CCLabelTTF *Key1Lable; 
  7.     CCLabelTTF *Key2Lable; 
  8.     CCLabelTTF *Key3Lable; 
  9.     CCLabelTTF *exitShop; 
  10.     CCLabelTTF *enter; 
  11.  
  12. @property (nonatomic,assign)int yellowKeyPrice; 
  13. @property (nonatomic,assign)int blueKeyPrice; 
  14. @property (nonatomic,assign)int redKeyPrice; 
  15. //当前选中商店编号 
  16. @property (nonatomic,assign) int selID; 
  17. //是否在购买状态 
  18. @property (nonatomic,assign) int isOnShop; 
  19. @property (nonatomic,retain) CCSprite *selSprite;  
  20.  
  21. -(void)viewInit:(int) shopID; 
  22. @end 
#import 
#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代码:

[html]
  1. #import "Buykey.h" 
  2.  
  3.  
  4. @implementation Buykey 
  5. @synthesize yellowKeyPrice; 
  6. @synthesize blueKeyPrice; 
  7. @synthesize redKeyPrice; 
  8. @synthesize selSprite; 
  9. @synthesize isOnShop; 
  10. @synthesize selID; 
  11.  
  12. -(void)viewInit:(int) shopID 
  13.     if (shopID == 5)  
  14.     { 
  15.         self.yellowKeyPrice = 10; 
  16.         self.blueKeyPrice = 50; 
  17.         self.redKeyPrice = 100; 
  18.         [introduction setString:@"勇士你好,我这里有各种钥匙如果你有钱我可以卖给你"]; 
  19.     } 
  20.     else 
  21.     { 
  22.         self.yellowKeyPrice = 5; 
  23.         self.blueKeyPrice = 25; 
  24.         self.redKeyPrice = 50; 
  25.         [introduction setString:@"勇士你好,如果你有多余的钥你可以卖给我"]; 
  26.     } 
  27.     [Key1Lable setString:[NSString stringWithFormat:@"黄钥匙(%d)",self.yellowKeyPrice]]; 
  28.     [Key2Lable setString:[NSString stringWithFormat:@"蓝钥匙(%d)",self.blueKeyPrice]]; 
  29.     [Key3Lable setString:[NSString stringWithFormat:@"红钥匙(%d)",self.redKeyPrice]]; 
  30.     [self removeChild:self.selSprite cleanup:YES]; 
  31.     id blink = [CCBlink actionWithDuration:2 blinks:2]; 
  32.     [self.selSprite runAction:[CCRepeatForever actionWithAction:blink]]; 
  33.     [self addChild:self.selSprite]; 
  34.     self.selSprite.position = Key1Lable.position; 
  35. -(id)init 
  36.     if ((self = [super init]))  
  37.     { 
  38.         //获取屏幕大小 
  39.         CGSize size = [[CCDirector sharedDirector] winSize]; 
  40.         self.selSprite = [CCSprite spriteWithFile:@"logo_select.png"]; 
  41.         self.selSprite.scaleX = 2.0; 
  42.         id blink = [CCBlink actionWithDuration:2 blinks:2]; 
  43.         [self.selSprite runAction:[CCRepeatForever actionWithAction:blink]]; 
  44.         self.selID = 0; 
  45.         //背景 
  46.         CCTMXTiledMap *background = [CCTMXTiledMap tiledMapWithTMXFile:@"shopbg.tmx"]; 
  47.         background.scale = 2.0; 
  48.         background.position = ccp(35, size.height / 2 - 200); 
  49.         [self addChild:background]; 
  50.         //介绍标签 
  51.         //introduction = [[CCLabelTTF alloc] initWithString:@"勇士你好,我这里有各种钥匙如果你有钱我可以卖给你" fontName:@"Verdana-Bold" fontSize:30]; 
  52.         introduction = [[CCLabelTTF alloc] initWithString:@"勇士你好,我这里有各种钥匙如果你有钱我可以卖给你" dimensions:CGSizeMake(500, 100) alignment:UITextAlignmentCenter fontName:@"Verdana-Bold" fontSize:32]; 
  53.         introduction.position = ccp(350, size.height - 200); 
  54.         [self addChild:introduction]; 
  55.         //黄钥匙标签 
  56.         Key1Lable = [[CCLabelTTF alloc] initWithString:@"黄钥匙(10)" fontName:@"Verdana-Bold" fontSize:30]; 
  57.         Key1Lable.position = ccp(350, size.height - 260); 
  58.         [self addChild:Key1Lable]; 
  59.         //蓝钥匙标签 
  60.         Key2Lable = [[CCLabelTTF alloc] initWithString:@"蓝钥匙(50)" fontName:@"Verdana-Bold" fontSize:30]; 
  61.         Key2Lable.position = ccp(350, size.height - 320); 
  62.         [self addChild:Key2Lable]; 
  63.         //红钥匙标签 
  64.         Key3Lable = [[CCLabelTTF alloc] initWithString:@"红钥匙(100)" fontName:@"Verdana-Bold" fontSize:30]; 
  65.         Key3Lable.position = ccp(350, size.height - 380); 
  66.         [self addChild:Key3Lable]; 
  67.         //离开商店 
  68.         exitShop = [[CCLabelTTF alloc] initWithString:@"离开商店" fontName:@"Verdana-Bold" fontSize:30]; 
  69.         exitShop.position = ccp(350, size.height - 440); 
  70.         [self addChild:exitShop]; 
  71.         //确定 
  72.         enter = [[CCLabelTTF alloc] initWithString:@"确定" fontName:@"Verdana-Bold" fontSize:30]; 
  73.         enter.position = ccp(350,size.height - 500); 
  74.         [self addChild:enter]; 
  75.         [self addChild:self.selSprite]; 
  76.         self.selSprite.position = Key1Lable.position; 
  77.     } 
  78.     return self; 
  79. @end 
#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
好了这两个类都有了,那么就让我们添加Game01.m里面的代码吧

这次我们要添加到if(npc_tileGid)里面:

[html]
  1. case 4: 
  2.      business = [[Business alloc] initWithID:type]; 
  3.      business.isOnShop = YES; 
  4.      [self addChild:business]; 
  5.      break; 
  6. case 5: 
  7.      [buykey viewInit:5]; 
  8.      [self addChild:buykey]; 
  9.      buykey.isOnShop = YES; 
  10.      break; 
  11. case 6: 
  12.      [buykey viewInit:6]; 
  13.      [self addChild:buykey]; 
  14.      buykey.isOnShop = YES; 
  15.      break; 
  16. case 7: 
  17.      business = [[Business alloc] initWithID:type]; 
  18.      business.isOnShop = YES; 
  19.      [self addChild:business]; 
  20.      break; 
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;
这样我们的“可用”npc又多了几个,之后我们会继续完善我们的npc,好了继续添加我们的代码:

[html]
  1. if (business.isOnShop)  
  2.     { 
  3.         for (int i = 0; i <= 4; i++)  
  4.         { 
  5.             CGRect Rect1 = CGRectMake(200, 
  6.                                       size.height - 290 - i*60, 
  7.                                       400, 
  8.                                       60); 
  9.             //检测触点是否在控件区 
  10.             if (CGRectIntersectsRect(Rect, Rect1)) 
  11.             { 
  12.                 if (i == 4)  
  13.                 { 
  14.                     if (herohp.Experience >= business.curExperience2)  
  15.                     { 
  16.                         switch (business.selID) 
  17.                         { 
  18.                             case 0: 
  19.                                 if (herohp.Experience >= business.curExperience)  
  20.                                 { 
  21.                                     _hero.HP += 1000*business.curUpgrade; 
  22.                                     _hero.Attack += 7*business.curUpgrade; 
  23.                                     _hero.Defense += 7*business.curUpgrade; 
  24.                                     herohp.Grade += 1; 
  25.                                     [herohp updateGrade]; 
  26.                                     herohp.Experience -= business.curExperience; 
  27.                                 } 
  28.                                 break; 
  29.                             case 1: 
  30.                                 if (herohp.Experience >= business.curExperience2) 
  31.                                 { 
  32.                                     _hero.Attack += business.curAttack; 
  33.                                     herohp.Experience -= business.curExperience2; 
  34.                                 } 
  35.                                 break; 
  36.                             case 2: 
  37.                                 if (herohp.Experience >= business.curExperience2) 
  38.                                 { 
  39.                                     _hero.Defense += business.curDefense; 
  40.                                     herohp.Experience -= business.curExperience2; 
  41.                                 } 
  42.                                 break; 
  43.                             default: 
  44.                                 break; 
  45.                         } 
  46.                         [herohp updateHeroHp]; 
  47.                         [herohp updateHeroAttack]; 
  48.                         [herohp updateHeroDefense]; 
  49.                         [herohp updateExperience]; 
  50.                     } 
  51.                 } 
  52.                 else if (i == 3)  
  53.                 { 
  54.                     _hero.isFighting = NO; 
  55.                     business.isOnShop = NO; 
  56.                     [self removeChild:business cleanup:YES]; 
  57.                 } 
  58.                 else 
  59.                     business.selID = i; 
  60.                 if (i<3)  
  61.                 { 
  62.                     business.selSprite.position = ccp(350, size.height - 260 - i*60); 
  63.                 } 
  64.             } 
  65.         } 
  66.     } 
  67.     if (buykey.isOnShop)  
  68.     { 
  69.         for (int i = 0; i <= 4; i++)  
  70.         { 
  71.             CGRect Rect1 = CGRectMake(200, 
  72.                                       size.height - 290 - i*60, 
  73.                                       400, 
  74.                                       60); 
  75.             //检测触点是否在控件区 
  76.             if (CGRectIntersectsRect(Rect, Rect1)) 
  77.             { 
  78.                 if (i == 4)  
  79.                 { 
  80.                     switch (buykey.selID)  
  81.                     { 
  82.                         case 0: 
  83.                             if (buykey.yellowKeyPrice > 5)  
  84.                             { 
  85.                                 if (herohp.Coin >= 10)  
  86.                                 { 
  87.                                     herohp.Coin -= 10; 
  88.                                     herohp.YellowKey += 1; 
  89.                                 } 
  90.                             } 
  91.                             else 
  92.                             { 
  93.                                 herohp.Coin += 5; 
  94.                                 herohp.YellowKey -= 1;   
  95.                             }   
  96.                             break; 
  97.                         case 1: 
  98.                             if (buykey.yellowKeyPrice > 5)  
  99.                             { 
  100.                                 if (herohp.Coin >= 50)  
  101.                                 { 
  102.                                     herohp.Coin -= 50; 
  103.                                     herohp.BlueKey += 1; 
  104.                                 } 
  105.                             } 
  106.                             else 
  107.                             { 
  108.                                 herohp.Coin += 25; 
  109.                                 herohp.BlueKey -= 1;   
  110.                             } 
  111.                             break; 
  112.                         case 2: 
  113.                             if (buykey.yellowKeyPrice > 5)  
  114.                             { 
  115.                                 if (herohp.Coin >= 100)  
  116.                                 { 
  117.                                     herohp.Coin -= 100; 
  118.                                     herohp.RedKey += 1; 
  119.                                 } 
  120.                             } 
  121.                             else 
  122.                             { 
  123.                                 herohp.Coin += 50; 
  124.                                 herohp.RedKey -= 1;   
  125.                             } 
  126.                             break; 
  127.                         default: 
  128.                             break; 
  129.                     } 
  130.                     [herohp updateCoin]; 
  131.                     [herohp updateKey1]; 
  132.                 } 
  133.                 else if (i == 3)  
  134.                 { 
  135.                     _hero.isFighting = NO; 
  136.                     buykey.isOnShop = NO; 
  137.                     [self removeChild:buykey cleanup:YES]; 
  138.                 } 
  139.                 else 
  140.                 { 
  141.                     buykey.selID = i; 
  142.                 } 
  143.                 if (i<3)  
  144.                 { 
  145.                     buykey.selSprite.position = ccp(350, size.height - 260 - i*60); 
  146.                 } 
  147.             } 
  148.         } 
  149.          
  150.     } 
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];这样就行了。

 

转载于:https://my.oschina.net/u/992577/blog/110601

你可能感兴趣的文章
mybatis学习
查看>>
从不同层面看cocos2d-x
查看>>
Struts2技术详解
查看>>
MFC应用程序向导生成的文件
查看>>
Oracle体系结构之oracle密码文件管理
查看>>
【leetcode】Remove Element (easy)
查看>>
mysql多表查询及其 group by 组内排序
查看>>
alsa的snd_pcm_readi()函数和snd_pcm_writei()
查看>>
Android学习网站推荐(转)
查看>>
嵌入式根文件系统的移植和制作详解
查看>>
MEF部件的生命周期(PartCreationPolicy)
查看>>
LCD的接口类型详解
查看>>
nginx 基础文档
查看>>
LintCode: Unique Characters
查看>>
Jackson序列化和反序列化Json数据完整示例
查看>>
.net 中的DllImport
查看>>
nyoj 517 最小公倍数 【java睑板】
查看>>
include与jsp:include区别
查看>>
ftp的20 21端口和主动被动模式
查看>>
MySQL存储引擎选型
查看>>