咱們的遊戲大部分功能已經實現了接下來咱們就要來完善npc有關內容了,
昨天有個朋友說序章中遇到仙子是有一個劇情的,這個確實有並且遊戲中
還有不少npc的劇情,今天咱們就開始添加這部份內容吧!
首先咱們要添加一個用於建立npc劇情的類:TalkTonpc,要知道遊戲中有很
多與npc交談的劇情,咱們如何用這個類來添加全部的劇情呢,下面咱們就來
說一下這些劇情的共性與區別在哪裏:
第1、他們都是由勇士和npc組成,因此咱們把這個場景分爲兩部分一個是
勇士部分,一個是npc部分。
第2、勇士部分和npc部分都是由一個個談話窗體組成,因此咱們須要再建
一個談話窗體類TalkCell。
第3、談話窗體部分,只有文本內容不同其它的都是相同的,因此這裏我
們還須要在建一個用於控制文本內容的類:TalkCell。
講到這裏咱們的劇情就只差一個文本內容了,那麼咱們就須要建一個數據類
(NpcTalkText)來存儲這些談話文本了,因爲這個類內容太多這裏我就不粘貼
代碼了,在咱們把另外三個類講解完以後在講一下這個類。
下面咱們就要開始添加代碼部分了:
TalkTonpc.h代碼:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "TalkCell.h"
#import "NpcTalkText.h"
@interface TalkTonpc : CCLayer
{
//談話對象npc名稱
NSString *npcName;
//勇士
NSString *hero;
TalkCell *seltalkCell;
//npc談話內容
NpcTalkText *npcTalkText;
//跳過菜單
CCMenuItemFont *Skip;
//當前選中npc對話內容
NSMutableArray *selnpcTalkText;
//文本編號
NSInteger textID;
//當前npc類型
int selnpcType;
}
@property (nonatomic,assign) bool talking;
-(void)updateWith:(int) type;
-(void)removeTalking;
@end
TalkTonpc.m代碼:
#import "TalkTonpc.h"
@implementation TalkTonpc
@synthesize talking;
-(id)init
{
if ((self = [super init]))
{
npcTalkText = [NpcTalkText getNpcTalkText];
//跳過對話
Skip = [CCMenuItemFont itemFromString:@"--Skip--" target:self selector:@selector(removetext)];
Skip.fontSize = 30;
CCMenu *menu = [CCMenu menuWithItems:Skip, nil];
menu.position = ccp(455, 100);
[self addChild:menu];
}
return self;
}
-(void)addTalking
{
TalkCellText *cellText = [selnpcTalkText objectAtIndex:textID];
seltalkCell = [[TalkCell alloc] initWith:cellText];
if (cellText.name == @"勇士")
{
seltalkCell.position = ccp(200, 240);
}
else
seltalkCell.position = ccp(100, 420);
[self addChild:seltalkCell];
}
-(void)updateWith:(int) type
{
self.talking = NO;
textID = 0;
selnpcType = type;
switch (type)
{
case 0:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc0;
break;
case 1:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc1;
break;
case 2:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc2;
break;
case 3:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc3;
break;
case 4:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc4;
break;
case 6:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc6;
break;
case 7:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc7;
break;
case 8:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc8;
break;
case 9:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc9;
break;
case 10:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc10;
break;
case 11:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc11;
break;
case 12:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc12;
break;
case 13:
selnpcTalkText = nil;
selnpcTalkText = npcTalkText.npc13;
break;
default:
break;
}
[self addTalking];
}
-(void)removeTalking
{
if (textID >= (selnpcTalkText.count - 1))
{
self.talking = YES;
}
if (self.talking)
{
[self removeChild:seltalkCell cleanup:YES];
[self.parent talkTonpc:selnpcType];
[self.parent removeChild:self cleanup:YES];
}
else
{
[self removeChild:seltalkCell cleanup:YES];
textID += 1;
[self addTalking];
}
}
-(void)removetext
{
[self removeChild:seltalkCell cleanup:YES];
[self.parent talkTonpc:selnpcType];
[self.parent removeChild:self cleanup:YES];
}
@end
TalkCell.h代碼:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "TalkCellText.h"
@interface TalkCell : CCLayer
{
//談話窗體背景
CCTMXTiledMap *background;
CCLabelTTF *name;
CCLabelTTF *celltext1;
CCLabelTTF *celltext2;
CCMenuItemFont *enter;
}
-(id)initWith:(TalkCellText*) text;
@end
TalkCell.m代碼:
#import "TalkCell.h"
@implementation TalkCell
-(id)initWith:(TalkCellText*) text
{
if ((self = [super init]))
{
//背景
background = [CCTMXTiledMap tiledMapWithTMXFile:@"fightbg.tmx"];
background.position = ccp(0, 0);
background.scaleX = 1.6;
[self addChild:background];
//名稱
name = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%@:",text.name] fontName:@"Verdana-Bold" fontSize:35];
name.anchorPoint = ccp(0, 0);
name.position = ccp(10, 110);
[self addChild:name];
//談話內容
celltext1 = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%@",text.labelText1] fontName:@"Verdana-Bold" fontSize:24];
celltext2 = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%@",text.labelText2] fontName:@"Verdana-Bold" fontSize:24];
celltext1.anchorPoint = ccp(0, 0);
celltext2.anchorPoint = ccp(0, 0);
celltext1.position = ccp(30, 70);
celltext2.position = ccp(10, 30);
[self addChild:celltext1];
[self addChild:celltext2];
//肯定按鈕
enter = [CCMenuItemFont itemFromString:@"肯定" target:self selector:@selector(removetext)];
enter.fontSize = 28;
CCMenu *menu = [CCMenu menuWithItems:enter, nil];
menu.position = ccp(455, 10);
[self addChild:menu];
}
return self;
}
-(void)removetext
{
[self.parent removeTalking];
}
@end 數組
TalkCellText.h代碼: atom
#import <Foundation/Foundation.h> spa
#import "cocos2d.h" .net
@interface TalkCellText : NSObject orm
@property (nonatomic,retain) NSString *name;
@property (nonatomic,retain) NSString *labelText1;
@property (nonatomic,retain) NSString *labelText2;
@end
TalkCellText.m代碼:
#import "TalkCellText.h"
@implementation TalkCellText
@synthesize name;
@synthesize labelText1;
@synthesize labelText2;
-(id)init
{
if ((self = [super init]))
{
}
return self;
}
@end
下面咱們說一下TalkCellText這個類,這個類很簡單,可是很重要,你們看到了
它有三個屬性,你們一看就明白了,不過這裏的兩個label其實能夠合成一個的
我當時寫的時候沒想那麼多,這裏我就不修改了,大家能夠本身修改一下試試。
接着咱們講一下NpcTalkText這個類,首先這個類要寫成單例模式,而且咱們
要定義一些可變數組用於存儲咱們的文本內容,下面我以其中一個npc對話爲例
子講一下,代碼以下:
-(void)addNPC1
{
TalkCellText *text1 = [[[TalkCellText alloc] init] autorelease];
text1.name = yong;
text1.labelText1 = @"您已經得救了!";
text1.labelText2 = @"";
[self.npc1 addObject:text1];
TalkCellText *text2 = [[[TalkCellText alloc] init] autorelease];
text2.name = @"神祕老人";
text2.labelText1 = @"哦,太感謝你了!我這裏有一個寶物,就送給";
text2.labelText2 = @"你了!孩子再見";
[self.npc1 addObject:text2];
TalkCellText *text3 = [[[TalkCellText alloc] init] autorelease];
text3.name = yong;
text3.labelText1 = @"???怎麼不見了?考,你這退場方式太牛";
text3.labelText2 = @"逼了,變魔術的吧!";
[self.npc1 addObject:text3];
TalkCellText *text4 = [[[TalkCellText alloc] init] autorelease];
text4.name = @"神祕老人";
text4.labelText1 = @"...遠處傳來...哈哈,導演就是這麼安排的,";
text4.labelText2 = @"沒辦法,其實哥也想低調一點.不要崇拜哥哦!";
[self.npc1 addObject:text4];
TalkCellText *text5 = [[[TalkCellText alloc] init] autorelease];
text5.name = yong;
text5.labelText1 = @"...........";
text5.labelText2 = @"";
[self.npc1 addObject:text5];
}
咱們的數組裏面存儲的是實例化的TalkCellText,咱們只須要對其三個屬性賦值就好了
呵呵,很簡單吧,使用的時候咱們就從相應的數組中提取相應的文本就好了。
這裏我對其文本內容略加修改,改的很差僅供參考。
其實有時候咱們要有本身的想法,不要拘泥於形式,想到什麼就能夠去嘗試修改一下
也許就有意想不到的效果。
好了讓咱們繼續咱們的遊戲製做吧:
咱們先說一下TalkTonpc類中的updateWith: 這個方法,咱們接下來在Game01中添加
的內容就跟它有關,這個方法經過接收一個int值來獲取相應的文本數組,而後調用方
法:addTalking來添加勇士和npc的談話內容。在咱們點擊肯定按鈕是調用切換談話內
容的方法:removeTalking 這裏咱們經過textID來獲取下一個談話內容。
下面讓咱們添加Game01中的代碼吧,先修改if(npc_tileGid)中的switch循環的內容
case 0:
[self addChild:talkTonpc];
[talkTonpc updateWith:type];
break;
case 1:
[self addChild:talkTonpc];
[talkTonpc updateWith:type];
break;
case 2:
[self addChild:talkTonpc];
[talkTonpc updateWith:type];
break;
case 3:
if (thiefID == 1)
{
[self addChild:talkTonpc];
[talkTonpc updateWith:3];
//打開二樓的門
TitledMap *twofloor = [self.model.titleMapArray1 objectAtIndex:2];
CGPoint point = ccp(1, 6);
[twofloor.door removeTileAt:point];
thiefID = 2;
}
else
{
if (canopen)
{
[self addChild:talkTonpc];
[talkTonpc updateWith:4];
TitledMap *floor = [self.model.titleMapArray1 objectAtIndex:18];
CGPoint point = ccp(5, 8);
CGPoint point2 = ccp(5, 9);
[floor.other removeTileAt:point];
[floor.other removeTileAt:point2];
}
}
_hero.isFighting = NO;
break;
case 8:
[self addChild:talkTonpc];
if (herohp.Coin >= 500)
{
[talkTonpc updateWith:8];
}
else
{
[talkTonpc updateWith:6];
}
break;
case 9:
[self addChild:talkTonpc];
if (herohp.Experience >= 500)
{
[talkTonpc updateWith:9];
}
else
{
[talkTonpc updateWith:7];
}
break;
case 10:
[self addChild:talkTonpc];
[talkTonpc updateWith:10];
break;
case 21:
_hero.isFighting = NO;
咱們還要在Game01初始化方法裏實例化talkTonpc。
修改過這部分代碼咱們就能在遊戲中看到與npc的交談了,不過咱們還要再添加一個
交談結束時相應事件的方法:
-(void)talkTonpc:(int) type
{
switch (type)
{
case 0:
herohp.YellowKey ++;
herohp.BlueKey ++;
herohp.RedKey ++;
[herohp updateKey1];
[self.curtitleMap.npc removeTileAt:towerLoc];
_hero.isFighting = NO;
break;
case 1:
_hero.Defense += 30;
[herohp updateHeroDefense];
[self.curtitleMap.npc removeTileAt:towerLoc];
_hero.isFighting = NO;
break;
case 2:
_hero.Attack += 30;
[herohp updateHeroAttack];
[self.curtitleMap.npc removeTileAt:towerLoc];
_hero.isFighting = NO;
break;
case 6:
_hero.isFighting = NO;
break;
case 7:
_hero.isFighting = NO;
break;
case 8:
_hero.Defense += 120;
herohp.Coin -= 500;
[herohp updateHeroDefense];
[herohp updateCoin];
[self.curtitleMap.npc removeTileAt:towerLoc];
_hero.isFighting = NO;
break;
case 9:
_hero.Attack += 120;
herohp.Experience -= 500;
[herohp updateHeroAttack];
[herohp updateExperience];
[self.curtitleMap.npc removeTileAt:towerLoc];
_hero.isFighting = NO;
break;
case 10:
[self.curtitleMap.other removeTileAt:CGPointMake(10, 10)];
//self.curtitleMap.hiddenFloor = NO;
_hero.isFighting = NO;
break;
case 11:
canAttack = YES;
_hero.isFighting = NO;
break;
case 12:
//win = [[[GameWin alloc] init] autorelease];
//[self addChild:win];
break;
case 13:
self.selenemy.enemyID = 0;
[self removeEnemy];
break;
default:
break;
}
}
這樣咱們npc這塊就基本結束了,這裏面還有幾個boss的談話場景和一些特殊事件
到後面的文章裏在講,今天的內容也挺多的你們好好看看,消化一下。