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

Update メソッド

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

これらのスプライトを randY 、 randY1 、および randY2 の間でランダムに配布したい

機能は使えarc4random()ますか?はいの場合...どのように?

4

1 に答える 1

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;}
于 2013-02-26T06:52:02.977 に答える