Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CCCallFuncN中与CCSequence一起使用的操作_Ios_Cocos2d Iphone - Fatal编程技术网

Ios CCCallFuncN中与CCSequence一起使用的操作

Ios CCCallFuncN中与CCSequence一起使用的操作,ios,cocos2d-iphone,Ios,Cocos2d Iphone,我需要创建可重复的操作序列(CCMoveBy、CCMoveTo),但它们需要使用随机参数(位置、持续时间)。我写了两种方法: -(void) randomizeVec 及 事实上,这些参数取决于外部变量(因子)。我不能做那样的事: id randomize = [CCCallFuncN actionWithTarget:self selector:@selector(randomizeVec)]; id calcTilt = [CCCallFuncN actionWithTarget:self

我需要创建可重复的操作序列(CCMoveBy、CCMoveTo),但它们需要使用随机参数(位置、持续时间)。我写了两种方法:

-(void) randomizeVec

事实上,这些参数取决于外部变量(因子)。我不能做那样的事:

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中的其他操作不会停止。也许您知道我可以实现的不同方法吗?

最后,我将负责启动操作的代码移到了更新方法中。每次调用更新时,我都会检查是否可以运行操作序列(布尔变量)。如果可以,我运行它,然后将变量设置为否,但在序列中的最后一个操作之后,我调用方法,该方法检查是否可以再次运行序列。它很舒服,因为它允许我更改影响动作的变量(CCMoveBy、CCMoveTo),并且它类似于CCRepeatForever

id action = [CCCallFuncN actionWithTarget:self selector:@selector(moveRandomVector:)];
CCSequence* sq = [CCSequence actionOne:action two:[CCDelayTime actionWithDuration:0.1]];
[towerNode runAction:[CCRepeatForever actionWithAction:sq]];