下面我们开始接着昨天的内容添加,首先我们的游戏界面分为两个部分(游戏地图层,英雄信息层)
我们添加文件Game01(游戏地图层), Herohp(英雄信息层),Hero(英雄)三个文件(在这里
我的命名不太规范可以自己修改)
Hero.h文件代码
#import<Foundation/Foundation.h>
#import"cocos2d.h"
//勇士
@inte易做图ce 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 = [CCSpriteFrame frameWithTexture:heroTexture rect: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;
@inte易做图ce 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;
@inte易做图ce 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];