对于我在 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]];
}
}