在cocos2d中向屏幕添加sprite数组的问题?

时间:2013-04-07 18:33:05

标签: arrays cocos2d-iphone sprite ccspritebatchnode

对于我在cocos2d中的游戏,我正在尝试向屏幕添加一个精灵数组,不断向左移动。当我将方法添加到我的更新方法时,它不能按预期方式工作。我首先设置了一个批处理节点。

- (void)setupBatchNode
{
    _batchNode = [CCSpriteBatchNode batchNodeWithFile:@"berries-hd.pvr.ccz"]; 
    [self addChild:_batchNode z:-1];
    [[CCSpriteFrameCache sharedSpriteFrameCache]
     addSpriteFramesWithFile:@"berries-hd.plist"];
}

-(void)updatePinkBerries:(ccTime)dt
{
    CGSize winSize = [CCDirector sharedDirector].winSize; // Is it time to spawn a pink berry?

    double curTime = CACurrentMediaTime();

    if (curTime > _nextPinkBerrySpawn)
    {
        // Figure out the next time to spawn an asteroid
        float randSecs = randomValueBetween(0.20, 1.0); _nextPinkBerrySpawn = randSecs + curTime;

        // Figure out a random Y value to spawn at
        float randY = randomValueBetween(0.0, winSize.height);

        // Figure out a random amount of time to move from right to left
        float randDuration = randomValueBetween(2.0, 10.0);

        // Create a new berry sprite
        CCSprite *pinkBerry = [CCSprite spriteWithSpriteFrameName:@"testt.png"]; 
        [_batchNode addChild:pinkBerry];

        // Set its position to be offscreen to the right
        pinkBerry.position = ccp(winSize.width + pinkBerry.contentSize.width/2, randY);

        // Move it offscreen to the left, and when it's done, call removeNode
        [pinkBerry runAction:
         [CCSequence actions:
          [CCMoveBy actionWithDuration:randDuration position:ccp(-winSize.width- pinkBerry.contentSize.width, 0)],
          [CCCallFuncN actionWithTarget:self selector:@selector(removeNode:)], nil]];
    }
}

0 个答案:

没有答案