我需要创建可重复的动作序列(CCMoveBy、CCMoveTo),但它们需要使用随机参数(位置、持续时间)。我写了2个方法:
-(void) randomizeVec
和
-(void) calcTiltDuration
实际上,这些参数取决于外部变量(因子)。我不能这样做:
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 的序列的动作停止但 CCMove* 动作和 moveRandomVector 中的其他动作不会停止的情况。也许您知道我可以实施的不同方法?