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

   我们接着第一部分教程继续写开始游戏部分

   这部分教程源代码连接

  下面我们开始接着昨天的内容添加,首先我们的游戏界面分为两个部分(游戏地图层,英雄信息层)

  我们添加文件Game01(游戏地图层), Herohp(英雄信息层),Hero(英雄)三个文件(在这里

我的命名不太规范可以自己修改)

Hero.h文件代码

#import<Foundation/Foundation.h>

#import"cocos2d.h"

//勇士

@interface Hero :CCSprite

{

}

//血量

@property (nonatomic,assign)int HP;

//攻击

@property (nonatomic,assign)int Attack;

//防御

@property (nonatomic,assign)int Defense;

@property (nonatomic,retain)CCAction *walkAction;

@property (nonatomic,retain)CCAction *moveAction;

@property (nonatomic,retain)CCAction *moveActions;

//是否在战斗状态

@property (nonatomic,assign)bool isFighting;

+(Hero*)getHero;

@end

Hero.m文件代码

#import"Hero.h"

@implementation Hero

@synthesize HP;

@synthesize Attack;

@synthesize Defense;

@synthesize moveAction;

@synthesize walkAction;

@synthesize moveActions;

@synthesize isFighting;

staticHero *_hero =nil;

-(id)initWith

{

    CCTexture2D *heroTexture = [[CCTextureCachesharedTextureCache]addImage:@"hero.png"];

    CCSpriteFrame *frame = [CCSpriteFrameframeWithTexture:heroTexturerect:CGRectMake(0,0,32,32)];

    if ((self = [superinitWithSpriteFrame:frame]))

    {

        self.HP = 1000;

        self.Attack = 10;

        self.Defense = 10;

    }

   returnself;

}

+(Hero*)getHero

{

    if (!_hero

    {

        _hero = [[self alloc] initWith];

    }

    return_hero;

}

-(void) dealloc

{

   self.walkAction =nil;

   self.moveAction =nil;

    [superdealloc];

}

@end

在这里我讲解一下Hero文件代码部分首先我们在游戏中只有一个勇士所以我把它写成了单例模式

我们的勇士有三个属性:血量、攻击、防御 。

Game01.h文件代码

#import<Foundation/Foundation.h>

#import"cocos2d.h"

@classHero;

@interface Game01 :CCLayer 

{

    CGSize size;

    //地图

    CCTMXTiledMap *curTiledMap;

    Hero *_hero;

}

@property (nonatomic,retain)Hero *hero;

@end

Game01.m文件代码

#import"Game01.h"

#import"Hero.h"

int _scale =2;

#define DOWNMIN50

#define LEFTMIN10

#define UPMAX750

#define RIGHTMAX690

@implementation Game01

@synthesize hero =_hero;

-(id)init

{

    if ((self = [superinit])) 

    {

        //获得屏幕大小

       size = [[CCDirectorsharedDirector]winSize];

        //加载游戏地图

       curTiledMap = [CCTMXTiledMaptiledMapWithTMXFile:@"1.tmx"];

       curTiledMap.scale =_scale;

       curTiledMap.position =ccp(LEFTMIN,DOWNMIN);

        [selfaddChild:curTiledMap];

        //添加勇士

        _hero = [Hero getHero];

        [_hero.parentremoveChild:_herocleanup:YES];

       _hero.scale =_scale;

        _hero.anchorPoint = ccp(0, 0);

        //添加主角

        _hero.position = CGPointMake(LEFTMIN + (5*32*_scale),DOWNMIN + (32*_scale));

        [selfaddChild:_hero];

    }

   returnself;

}

@end

Game01部分代码实现添加游戏地图,添加勇士,(代码不多但很重要)

Herohp.h文件代码

#import<Foundation/Foundation.h>

#import"cocos2d.h"

@classHero;

@interface Herohp :CCLayer 

{

   //设置获取地图参数

    CCTMXTiledMap *mapBackground;

    CCTMXTiledMap *background;

   //英雄等级

    CCSprite *heroGrade;

   //黄钥匙

    CCSprite *Key1;

   //蓝钥匙

    CCSprite *Key2;

   //红钥匙

    CCSprite *Key3;

   //英雄等级标签

    CCLabelTTF *GradeLable;

    CCLabelTTF *HpLable;

    CCLabelTTF *AttLable;

    CCLabelTTF *DefLable;

    CCLabelTTF *CoinLable;

    CCLabelTTF *ExperienceLable;

   //钥匙数量标签

    CCLabelTTF *Key1Lable;

    CCLabelTTF *Key2Lable;

    CCLabelTTF *Key3Lable;

   //楼层标签

    CCLabelTTF *floorValue;

    

    CCMenuItemFont *saveGame;

    CCMenuItemFont *readGame;

    CCMenuItemFont *exitGame;

    CCMenuItemFont *restartGame;

    

   //GameModel *model;

    //金币

    int _coin;

    //经验

    int _experience;

   //蓝钥匙

    int _yellowKey;

   //黄钥匙

    int _blueKey;

   //红钥匙

    int _redKey;

    //级别

    int _grade;

}

@property (nonatomic,retain)Hero *hero;

//金币

@property (nonatomic,assign)int Coin;

//经验

@property (nonatomic,assign)int Experience;

//黄钥匙

@property (nonatomic,assign)int YellowKey;

//蓝钥匙

@property (nonatomic,assign)int BlueKey;

//红钥匙

@property (nonatomic,assign)int RedKey;

//英雄级别

@property (nonatomic,assign)int Grade;

+(Herohp*) sharedHP;

@end

Herohp.m文件代码

#import"Herohp.h"

#import"Hero.h"

#define X750

#define Y150

@implementation Herohp

@synthesize hero;

@synthesize Coin;

@synthesize Experience;

@synthesize YellowKey =_YellowKey;

@synthesize BlueKey =_BlueKey;

@synthesize RedKey =_RedKey;

@synthesize Grade;

staticHerohp *_shareHP =nil;

+(Herohp*) sharedHP

{

   @synchronized([Herohpclass])

    {

        if (!_shareHP

        {

            _shareHP = [[self alloc] init];

        }

        return _shareHP;

    }

   returnnil;

}


-(id) init

{

    if ((self = [superinit]))

    {

        self.hero = [HerogetHero];

        CGSize size = [[CCDirector sharedDirector] winSize];

        //背景

       mapBackground = [CCTMXTiledMaptiledMapWithTMXFile:@"background.tmx"];

       mapBackground.position =ccp(0,0);

        [selfaddChild:mapBackground];

       background = [CCTMXTiledMaptiledMapWithTMXFile:@"heroHp.tmx"];

       background.scaleY =1.9;

       background.scaleX =2.5;

       background.position =ccp(X,Y);

        [selfaddChild:background];

        CCTexture2D *heroTexture = [[CCTextureCachesharedTextureCache]addImage:@"hero.png"];

        CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:heroTexture rect:CGRectMake(0,0,32,32)];

        heroGrade = [CCSprite spriteWithSpriteFrame:frame];

       heroGrade.scale =2.0;

       heroGrade.anchorPoint =ccp(0,0);

        heroGrade.position = ccp(X + 15, size.height -90);

        [selfaddChild:heroGrade];

        

        //英雄等级标签

       GradeLable = [CCLabelTTFlabelWithString:@"1"fontName:@"Verdana-Bold"fontSize:36];

       GradeLable.anchorPoint =ccp(1,0);

        GradeLable.position = ccp(X + 190, size.height -85);

        [selfaddChild:GradeLable];

        

        HpLable = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"生命 %d",self.hero.HP]fontName:@"Verdana-Bold"fontSize:32];

       HpLable.anchorPoint =ccp(0,0);

        HpLable.position = ccp(X + 20, size.height -130);

        [selfaddChild:HpLable];

       //[self schedule:@selector(updateHeroHp:) interval:1];

        AttLable = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"攻击 %d",self.hero.Attack]fontName:@"Verdana-Bold"fontSize:32];

       AttLable.anchorPoint =ccp(0,0);

        AttLable.position = ccp(X + 20 , size.height -175);

        [selfaddChild:AttLable];

        DefLable = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"防御 %d",self.hero.Defense]fontName:@"Verdana-Bold"fontSize:32];

       DefLable.anchorPoint =ccp(0,0);

        DefLable.position = ccp(X + 20, size.height -220);

        [selfaddChild:DefLable];

       CoinLable = [CCLabelTTFlabelWithString:@"金币 0"fontName:@"Verdana-Bold"fontSize:32];

       CoinLable.anchorPoint =ccp(0,0);

        CoinLable.position = ccp(X + 20, size.height -265);

        [selfaddChild:CoinLable];

       ExperienceLable = [CCLabelTTFlabelWithString:@"经验 0"fontName:@"Verdana-Bold"fontSize:32];

       ExperienceLable.anchorPoint =ccp(0,0);

        ExperienceLable.position = ccp(X + 20, size.height -310);

        [selfaddChild:ExperienceLable];

        

        self.Coin = 0;

        self.Experience = 0;

        self.Grade = 1;

       //钥匙图标和数量标签

        self.YellowKey = 0;

        self.BlueKey = 0;

        self.RedKey = 0;

        CCTexture2D *KeyTexture = [[CCTextureCachesharedTextureCache]addImage:@"item.png"];

        CCSpriteFrame *key1 = [CCSpriteFrame frameWithTexture:KeyTexture rect:CGRectMake(0,4*32,32,32)];

        CCSpriteFrame *key2 = [CCSpriteFrame frameWithTexture:KeyTexture rect:CGRectMake(1*32,4*32,32,32)];

        CCSpriteFrame *key3 = [CCSpriteFrame frameWithTexture:KeyTexture rect:CGRectMake(2*32,4*32,32,32)];

       Key1 = [CCSpritespriteWithSpriteFrame:key1];

        Key1.scale = 1.6;

        Key1.anchorPoint = ccp(0, 0);

        Key1.position = ccp(X + 15, size.height -360);

        [selfaddChild:Key1];

       Key1Lable = [CCLabelTTFlabelWithString:@"0"fontName:@"Verdana-Bold"fontSize:35];

       Key1Lable.anchorPoint =ccp(1,0);

        Key1Lable.position = ccp(X + 170, size.height -360);

        [selfaddChild:Key1Lable];

       Key2 = [CCSpritespriteWithSpriteFrame:key2];

        Key2.scale = 1.6;

        Key2.anchorPoint = ccp(0, 0);

        Key2.position = ccp(X + 15, size.height -410);

        [selfaddChild:Key2];

       Key2Lable = [CCLabelTTFlabelWithString:@"0"fontName:@"Verdana-Bold"fontSize:35];

       Key2Lable.anchorPoint =ccp(1,0);

        Key2Lable.position = ccp(X + 170, size.height -410);

        [selfaddChild:Key2Lable];

       Key3 = [CCSpritespriteWithSpriteFrame:key3];

        Key3.scale = 1.6;

        Key3.anchorPoint = ccp(0, 0);

        Key3.position = ccp(X + 15, size.height -460);

        [selfaddChild:Key3];

       Key3Lable = [CCLabelTTFlabelWithString:@"0"fontName:@"Verdana-Bold"fontSize:35];

       Key3Lable.anchorPoint =ccp(1,0);

        Key3Lable.position = ccp(X + 170, size.height -460);

        [selfaddChild:Key3Lable];

       floorValue = [CCLabelTTFlabelWithString:@"序章"fontName:@"Verdana-Bold"fontSize:35];

        floorValue.position = ccp(X + 110, size.height -490);

        [selfaddChild:floorValue];

        //确定按钮

        saveGame = [CCMenuItemFont itemFromString:@"保存"target:selfselector:@selector(onsaveGame)];

        saveGame.fontSize = 28;

       saveGame.fontName =@"Verdana-Bold";

        CCMenu *menu1 = [CCMenu menuWithItems:saveGame, nil];

        menu1.position =ccp(X +40, size.height -535);

        [selfaddChild:menu1];

        readGame = [CCMenuItemFont itemFromString:@"读取"target:selfselector:@selector(onreadGame)];

        readGame.fontSize = 28;

       readGame.fontName =@"Verdana-Bold";

        CCMenu *menu2 = [CCMenu menuWithItems:readGame, nil];

        menu2.position =ccp(X +40, size.height -580);

        [selfaddChild:menu2];

        restartGame = [CCMenuItemFont itemFromString:@"重新开始" target:self selector:@selector(onrestartGame)];

        restartGame.fontSize = 28;

       restartGame.fontName =@"Verdana-Bold";

        CCMenu *menu3 = [CCMenu menuWithItems:restartGame, nil];

        menu3.position =ccp(X +160, size.height -535);

        [selfaddChild:menu3];

        exitGame = [CCMenuItemFont itemFromString:@"退出游戏" target:self selector:@selector(onexitGame)];

        exitGame.fontSize = 28;

       exitGame.fontName =@"Verdana-Bold";

        CCMenu *menu4 = [CCMenu menuWithItems:exitGame, nil];

        menu4.position =ccp(X +160, size.height -580);

        [selfaddChild:menu4];

    }

   returnself;

}

-(void)onsaveGame

{

}

-(void)onreadGame

{

}

-(void)onrestartGame

{

}

-(void)onexitGame

{

    [[CCDirectorsharedDirector]end];

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

exit(0);

#endif

}

@end

下面我们讲解一下Herohp这部分代码,这部分代码比较多一点,但是很多都是重复的,难度不大

这部分内容主要是添加了勇士的基本信息:勇士等级,勇士的三个属性,各种钥匙的数量,楼层标签

和菜单项。这里只有菜单项用的是CCMenu实现的,其他的都是用CCLable实现的。

勇士信息这个类同样要使用单例模式!!!

然后在Scenemanager.m文件中goPlay方法中如下代码添加

    CCDirector *director = [CCDirectorsharedDirector];

    CCScene *scene = [CCScenenode];

    if ([directorrunningScene])

    {

        [director replaceScene:scene];

    }else

    {

        [director runWithScene:scene];

    }

    Herohp *layer2 = [Herohp sharedHP];

    [scene addChild:layer2 z:0];

    Game01 *layer = [Game01 node];

    [scene addChild:layer z:1];


然后运行一下结果如下


到这里我们得到了一个静态的界面没有,界面上的东西都是静态的,下一章我们将使我们的勇士移动起来


如果有什么意见大家可以提出来,谢谢

上一篇连接                        下一篇连接

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值