CCCallFunc的随机值:值未更新?

时间:2012-08-15 05:04:33

标签: objective-c random cocos2d-iphone

您知道如何更改flashPosX的值吗? changeFlashPosX中的“log”工作正常,但值不会改变:

-(void)changeFlashPosX{
    CCLOG(@"change");
    flashPosX = random() % 300;
    CCLOG(@"rando : %f", flashPosX);
}

-(void)animFlash{
    CCScaleTo *to1 = [CCScaleTo actionWithDuration:.2 scale:1];
    CCScaleTo *to0 = [CCScaleTo actionWithDuration:.6 scale:0];
    CCMoveTo *moveTo = [CCMoveTo actionWithDuration:0.1 position:ccp(flashPosX, flash.position.y)];
    CCCallFunc *callChange = [CCCallFunc actionWithTarget:self selector:@selector(changeFlashPosX)];
    CCSequence *seq = [CCSequence actions:to1, to0, callChange, moveTo, nil];
    //CCAction *repeatSeq = [CCRepeatForever actionWithAction:seq];
    CCRepeatForever *repeatSeq = [CCRepeatForever actionWithAction:seq];
    [flash runAction:repeatSeq];
}

输出:

rando : 164.000000
change
rando : 217.000000
change
rando : 241.000000
change
rando : 75.000000

1 个答案:

答案 0 :(得分:1)

试试这个:

CCMoveTo *moveTo = [CCMoveTo actionWithDuration:0.1 position:ccp(random() % 300, flash.position.y)];

如果有效,那么问题在于flashPosX变量的范围和/或保留,或者是CCSequence调用的例程循环。

另外,试试这个:

CCRepeatForever *repeatSeq = [CCRepeatForever actionWithAction:seq];