大熊貓豬·侯佩原創或翻譯作品.歡迎轉載,轉載請注明出處.
如果覺得寫的不好請多提意見,如果覺得不錯請多多支持點贊.謝謝! hopy ;)
上篇介紹了游戲主角的初始化方法,下面我們一次來實現主角的其他方法,首先來看看runAnimation方法,我們使用這個方法來播放主角的動畫:
-(void)runAnimation:(CCAnimation*)animation{
if (_curAnimation == animation) {
return;
}
_curAnimation = animation;
if (_curAnimate) {
[self stopAction:_curAnimate];
}
_curAnimate = [CCActionRepeatForever actionWithAction:
[CCActionAnimate actionWithAnimation:animation]];
[self runAction:_curAnimate];
}
代碼首先檢查將要播放的動畫是否和當前正在播放的動畫相同,如果相同則直接退出.然後如果當前正在播放動畫動作,則將其停止.
接下來為需要播放的動畫創建一個永久循環的動作,然後運行該動作.
vcfF9snPudbO77nStfS1xLavu63SssrHyOe0yy48L3A+DQo8cD7W973HwODW0Lu509DSu7j21tjSqrXEt723qCzTprjDu7mw/MCouMPTzs+31tDL+dPQudbO77a80OjSqtXiw7TSu7j2t723qCy+zcrHQSq1xNLGtq/L47eoLrK7yuzPpEEqy+O3qLXE0KG777Dpv8nS1LW9ztK3rdLrus3UrbS0tcRBKs+1wdCyqc7E1tC528nNOjwvcD4NCjxwPtSttLQ6yOe6ztTaQ29jb3MyRNPOz7fW0Mq1z9ZBKtGwwrfL47eoKNK7KTxiciAvPg0Kt63S6zpBKtGwwrfL47eoyOvDxSjSuyk8L3A+DQo8cD64w7e9t6i+zcrHbW92ZVRvd2FyZEJ5QVN0YXK3vbeoOjwvcD4NCjxwcmUgY2xhc3M9"brush:java;">
//使用A*尋路算法移動至目標坐標
-(void)moveTowardByAStar:(CGPoint)targetLocation{
if (_currentStepAction) {
_pendingMove = [NSValue valueWithCGPoint:targetLocation];
return;
}
CGPoint fromTileCoord = [_mainScene tileCoordForPosition:self.position];
CGPoint toTileCoord = [_mainScene tileCoordForPosition:targetLocation];
if (CGPointEqualToPoint(fromTileCoord, toTileCoord)) { return;
}
if (![_mainScene isWalkableTile:toTileCoord forRole:self]) {
return;
}
_spOpenSteps = [NSMutableArray array];
_spClosedSteps = [NSMutableArray array];
_shortestPath = nil;
[StarA insertStep:[[ShortestPathStep alloc] initWithPosition:fromTileCoord]
toOpenList:_spOpenSteps];
do{
ShortestPathStep *currentStep = _spOpenSteps[0];
[_spClosedSteps addObject:currentStep];
[_spOpenSteps removeObjectAtIndex:0];
if (CGPointEqualToPoint(currentStep.position, toTileCoord)) {
//如果已經找到最短路徑則完成該最短路徑的移動動作
[self constructPathAndStartAnimationFromStep:currentStep];
_spOpenSteps = nil;
_spClosedSteps = nil;
break;
}
NSArray *adjSteps = [_mainScene walkableAdjacentTilesCoordDiagonallyForTileCoord:
currentStep.position forRole:self];
for (NSValue *v in adjSteps) {
ShortestPathStep *step = [[ShortestPathStep alloc]initWithPosition:[v CGPointValue]];
if ([_spClosedSteps containsObject:step]) {
continue;
}
int moveCost = [StarA costToMoveDiagonallyFromStep:currentStep toAdjacentStep:step];
NSUInteger index = [_spOpenSteps indexOfObject:step];
if (index == NSNotFound) {
step.parent = currentStep;
step.gScore = currentStep.gScore + moveCost;
step.hScore = [StarA computeHScoreFromCoord:step.position toCoord:toTileCoord];
[StarA insertStep:step toOpenList:_spOpenSteps];
}else{//已經存在於開放列表
step = _spOpenSteps[index];
if ((currentStep.gScore + moveCost) < step.gScore) {
step.gScore = currentStep.gScore + moveCost;
step.parent = currentStep;
[_spOpenSteps removeObjectAtIndex:index];
[StarA insertStep:step toOpenList:_spOpenSteps];
}
}
}
}while (_spOpenSteps.count > 0);
}
其中比較長,這裡就不詳細說明了,大家需要的信息上面2個系列的博文完全涵蓋了.有一點要指出的是,因為我也才開始寫這類代碼,所以一開始對類元素的把握也不是太強,按理說這個方法是所有游戲角色都需要的,所以應該放在它們的父類中.的確是這樣,我在另一個RPG游戲<<熊貓之魂>>中正是這樣做的,我們有機會再敘.