我需要創建可重複的動作序列(CCMoveBy,CCMoveTo),但他們需要使用隨機參數(位置,持續時間)。我寫2種方法:CCCallFuncN中與CCSequence一起使用的動作
-(void) randomizeVec
和
-(void) calcTiltDuration
事實上,那些PARAMS取決於外部變量(因子)。我不能做這樣的事情:
id randomize = [CCCallFuncN actionWithTarget:self selector:@selector(randomizeVec)];
id calcTilt = [CCCallFuncN actionWithTarget:self selector:@selector(calcTiltDuration)];
CCMoveBy* tilt = [CCMoveBy actionWithDuration:mTIltDuration position:randomVec];
CCMoveTo* restore = [CCMoveTo actionWithDuration:mTIltDuration position:initialTowerNodePos];
CCDelayTime* wait = [CCDelayTime actionWithDuration:0.1];
CCSequence* seq = [CCSequence actions:wait,calcTilt,randomize, tilt, restore, nil];
[aNode runAction:[CCRepeatForever actionWithAction:seq]];
,因爲值被複制一次,randomizeVec和calcTiltDuration不影響行動,所以我寫了一個方法:
-(void) moveRandomVector:(CCNode*) node
{
int dx = rand_between(-1, 2) * mShakeFactor *2 ;
int dy = rand_between(-1, 2) * mShakeFactor *2;
CCMoveBy* action = [CCMoveBy actionWithDuration:0.1/mShakeFactor position:CGPointMake(dx, dy)];
CCMoveTo* action2 = [CCMoveTo actionWithDuration:0.1/mShakeFactor position:initialTowerNodePos];
CCEaseInOut* easyTilt = [CCEaseInOut actionWithAction:action rate:0.1];
CCEaseInOut* easyRestore = [CCEaseInOut actionWithAction:action2 rate:0.1];
CCSequence* seq = [CCSequence actions:easyTilt,easyRestore , nil];
[node runAction:seq];
}
它通過CCCallFuncN稱爲:
id action = [CCCallFuncN actionWithTarget:self selector:@selector(moveRandomVector:)];
CCSequence* sq = [CCSequence actionOne:action two:[CCDelayTime actionWithDuration:0.1]];
[towerNode runAction:[CCRepeatForever actionWithAction:sq]];
外部方法中的因素更改(mShakeFactor)。
這是正確的方法嗎?我不是Cocos2d專家,我擔心當在CCCallFuncN中運行序列的動作停止時,但是,MoveMand * Vector中的CCMove *動作和其他動作不會。也許你知道我可以實施的不同方法?