0

私はcocos2dが初めてなので、できれば助けてください

背景が右から左に移動しており、背景には 3 行のウィンドウを持つ小さなウィンドウが含まれています

_spaceDust1 = [CCSprite spriteWithFile:@"bg_front_spacedust.png"];
_spaceDust2 = [CCSprite spriteWithFile:@"bg_front_spacedust.png"];

    CGPoint dustSpeed = ccp(0.1 , 0.1);
    CGPoint bgSpeed = ccp(0.05 , 0.05);

    [_backgroundNode addChild:_spaceDust1 z:0 parallaxRatio:dustSpeed positionOffset:ccp(0,winSize.height / 2)];
    [_backgroundNode addChild:_spaceDust2 z:0 parallaxRatio:dustSpeed positionOffset:ccp(_spaceDust1.contentSize.width , winSize.height / 2)];

同じ速度で右から左に移動する敵を追加します

        _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 = 293.0;
    float randDuration = [self randomValueBetween:5.2 andValue:5.2];
    float randDuration1 = [self randomValueBetween:1.0 andValue:1.0];

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

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

    }
    else if(winChoice == 1){

        asteroid.position = ccp(winSize.width +asteroid.contentSize.width / 2 , randY1);
        asteroid.visible = YES;

    }else {
        asteroid.position = ccp(winSize.width +asteroid.contentSize.width / 2 , randY2);
        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]];

すべて順調に進んでいますが、この敵をウィンドウとランダムな位置に設定したい
ので、敵の x 引数を設定して背景のウィンドウに固定するにはどうすればよいですか?

4

1 に答える 1

0

何らかの理由でコメントできないため、これは回答として書かれています。正確に何をしようとしていますか?私は少し混乱しています。X 位置を 3 つのランダムな位置のいずれかに設定する必要があるようですが、正しいですか?

于 2013-02-28T05:59:15.887 に答える