2013-02-26 112 views
0

我已经定义:随机分配的精灵

#define kNumAstroids 15

和雪碧添加

_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]; 

更新方法

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]]; 

我想兰迪,randY1之间随机分发这些精灵randY2

我可以使用arc4random()功能吗?如果是...如何?

回答

0

使用arc4random()来找到你的随机Y位置

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;}