0

I have defined:

#define kNumAstroids 15

and Sprite Add

 _robbers = [[CCArray alloc] initWithCapacity:kNumAstroids];
    for (int i = 0; i < kNumAstroids; ++i) {
        CCSprite *asteroid = [CCSprite spriteWithSpriteFrameName:@"robber.png"];
        asteroid.visible = NO;
        [_batchNode addChild:asteroid];
        [_robbers addObject:asteroid];

Update Method

double curTime = CACurrentMediaTime();
if (curTime > _nextRunemanSpawn) {
    float randSecs = [self randomValueBetween:0.20 andValue:1.0];
    _nextRunemanSpawn = randSecs + curTime;

    float randY = 80.0;
    float randY1 = 185.0;
    float randY2 = 290.0;
    float randDuration = [self randomValueBetween:4.0 andValue:4.0];
    float randDuration1 = [self randomValueBetween:1.0 andValue:1.0];

    CCSprite *asteroid = [_robbers objectAtIndex:_nextRobber];
    _nextRobber++;

    if (_nextRobber >= _robbers.count) {
        _nextRobber = 0;
    }
    //[asteroid stopAllActions];
    asteroid.position = ccp(winSize.width +asteroid.contentSize.width / 2 , randY);
    asteroid.visible = YES;

    [asteroid runAction:[CCSequence actions:[CCMoveBy actionWithDuration:randDuration position:ccp(-winSize.width-asteroid.contentSize.width, 0)],
                         [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)],nil]];

I want to distribute these sprites randomly between randY , randY1 and randY2

Can I use arc4random() function? If yes ... how ?

4

1 回答 1

0

使用 arc4random() 找到你的随机 Y pos

float random_Y = randY + arc4random() % randY2;

然后在您的代码中使用 random_Y 值

if (curTime > _nextRunemanSpawn)
{
float randSecs = [self randomValueBetween:0.20 andValue:1.0];
_nextRunemanSpawn = randSecs + curTime;

float randY = 80.0;
float randY1 = 185.0;
float randY2 = 290.0;
float randDuration = [self randomValueBetween:4.0 andValue:4.0];
float randDuration1 = [self randomValueBetween:1.0 andValue:1.0];

CCSprite *asteroid = [_robbers objectAtIndex:_nextRobber];
_nextRobber++;

if (_nextRobber >= _robbers.count) {
    _nextRobber = 0;
}
//[asteroid stopAllActions];
asteroid.position = ccp(winSize.width +asteroid.contentSize.width / 2 , random_Y);//in this place
asteroid.visible = YES;}
于 2013-02-26T06:52:02.977 回答