我在 cocos2d 中有一个代码块,它通过循环浏览一组 10 张图像来创建背景,如下所示:
- (void)addBackground{
CGSize winSize = [CCDirector sharedDirector].winSize;
//Add images to batchNode
float maxReach = 0;
for (int imageNumber=1; imageNumber < 13; imageNumber++) {
CCLOG(@"Adding image intro%d.png to the introAnimation.",imageNumber);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CCSprite *background = [CCSprite spriteWithFile:[NSString stringWithFormat:@"national_scenery_part%d-iPad.png",imageNumber]];
background.position = ccp((winSize.width/2)+maxReach, winSize.height/2);
[self addChild:background z:0];
maxReach = maxReach + background.contentSize.width;
} else {
CCSprite *background = [CCSprite spriteWithFile:[NSString stringWithFormat:@"national_scenery_part%d.png",imageNumber]];
background.position = ccp((winSize.width/2)+maxReach, winSize.height/2);
[self addChild:background z:0];
maxReach = maxReach + background.contentSize.width;
}
}
}
但当然它只循环一次。我希望它循环 3 次。我正在考虑将一个整数设置为 0 并在每个循环的末尾添加 1,然后再次运行它直到它达到 3。这听起来像是最好的方法吗?