我们接着第一部分教程继续写开始游戏部分
这部分教程源代码连接
下面我们开始接着昨天的内容添加,首先我们的游戏界面分为两个部分(游戏地图层,英雄信息层)
我们添加文件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];
然后运行一下结果如下
到这里我们得到了一个静态的界面没有,界面上的东西都是静态的,下一章我们将使我们的勇士移动起来
如果有什么意见大家可以提出来,谢谢
上一篇连接 下一篇连接
分享到:
相关推荐
《cocos2d-iphone之魔塔20层第五部分》是关于使用cocos2d-iphone框架开发经典游戏——魔塔的一个教程章节。在这个部分,我们将深入探讨如何利用cocos2d-iphone的特性来实现魔塔游戏的第20层的逻辑和交互。 首先,...
《cocos2d-iphone之魔塔20层第十部分》是针对移动平台游戏开发的一份教程,主要讲解如何利用cocos2d-iphone框架来实现一款经典的魔塔游戏。cocos2d-iphone是一个强大的2D游戏开发框架,它基于Objective-C语言,为iOS...
总之,cocos2d-iphone之魔塔20层第八部分的教程将涵盖游戏开发的多个方面,包括但不限于对象和场景的管理、用户交互、性能优化、数据持久化以及调试技术。通过深入学习和实践,开发者可以掌握创建类似“魔塔20层”...
《cocos2d-iphone之魔塔20层第四部分》是针对移动平台游戏开发的一份教程,主要基于cocos2d-iphone框架。cocos2d-iphone是一款开源的游戏开发框架,它允许开发者使用Objective-C语言来创建2D游戏、演示和其他图形/...
《cocos2d-iphone之魔塔20层第三部分》是针对移动平台,特别是iPhone设备上使用cocos2d游戏引擎开发的一款经典游戏——魔塔的教程。在这个教程中,我们将深入探讨如何利用cocos2d-iphone框架构建一个具有20层关卡的...
《cocos2d-iphone之魔塔20层第九部分》是针对iOS平台的游戏开发教程,专注于使用cocos2d-iPhone框架制作的一款经典游戏——魔塔的开发过程。cocos2d-iPhone是一个强大的2D游戏开发框架,它基于C++和Objective-C,为...
在本教程中,我们将深入探讨如何使用Cocos2d-iPhone框架开发一款名为“魔塔20层”的游戏。Cocos2d-iPhone是一个广泛应用于iOS平台的2D游戏开发库,它提供了一系列强大的功能,如图形渲染、动画处理、物理引擎支持...
《cocos2d-iphone2.0之魔塔20层》是一款基于Cocos2D-iPhone 2.0框架开发的20层魔塔游戏。Cocos2D-iPhone是一个开源的游戏开发库,它是Cocos2D项目的一个分支,专为iOS平台设计,提供了丰富的2D图形渲染和游戏开发...
《cocos2d-iphone 游戏码源 魔塔20层》是一个基于Cocos2D-iPhone的游戏开发项目,旨在实现一款名为“魔塔20层”的经典策略角色扮演游戏。Cocos2D-iPhone是Cocos2D的一个分支,是一个用于创建2D游戏、演示和其他图形...
【cocos2d-x】是基于C++的开源游戏开发框架,它被广泛用于创建2D游戏、动画以及其他视觉互动内容。cocos2d-x支持多平台开发,包括iOS、Android、Windows等,使得开发者可以编写一次代码,到处运行。 【xcode】是...
【cocos2d-x】是基于C++的开源游戏开发框架,主要用于2D游戏、交互式应用程序和实时视觉效果的开发。它支持多种平台,包括iOS、Android、Windows以及Mac等。cocos2d-x提供了丰富的API接口,使得开发者可以方便地进行...
Cocos2d-x是一个基于C++的2D游戏引擎,它源于cocos2d-iphone,并扩展到支持Android、iOS、Windows等多个平台。它提供了一套完整的2D图形渲染、动画、物理引擎、音频处理、资源管理等功能,使得开发者能够快速构建高...