0

for my game in cocos2d, I'm trying to add an array of sprites to the screen that constantly move to the left. When I add the method to my update method, it doesn't work as expected. I first set up a batch node.

- (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]];
    }
}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Surz
  • 984
  • 3
  • 11
  • 36

0 Answers0