cocos2d-iphone之魔塔20层第八部分

为了感谢大家对我的支持这里给大家提供一下这八部分的代码下载

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

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

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

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

循环的下面如图:


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文件内容

#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

ShopLayer.m文件内容

#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)方法中添加以下代码:

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文件的触摸响应事件中去

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;

金币和经验也可以适当修改,修改过之后,让我们再次随着勇士的脚步来到5楼吧,这里

你会看到两个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

Business.m代码:

#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代码:

#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

好了这两个类都有了,那么就让我们添加Game01.m里面的代码吧

这次我们要添加到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;

这样我们的“可用”npc又多了几个,之后我们会继续完善我们的npc,好了继续添加我们的代码:

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];这样就行了。


上一篇连接 下一篇连接


©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值