xcode 在 cocos2d 中更改背景图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2902343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the background image in cocos2d
提问by V.V
I am making a game using cocos2d. in that I want to change the background after each second of time. I don't know how to do this in cocos2d. I am having 15 different images and one by one each image will be shown, i.e. after1 second next image will appear.
我正在使用 cocos2d 制作游戏。因为我想在每一秒后改变背景。我不知道如何在 cocos2d 中做到这一点。我有 15 张不同的图像,每张图像都会一一显示,即 1 秒后将出现下一张图像。
I am new to cocos2d so, If any one can help me???
我是 cocos2d 的新手,所以,如果有人可以帮助我???
thank you in advance to all.
提前谢谢大家。
回答by LearnCocos2D
I assume by background you mean an image with the full 320x480 resolution.
我假设背景是指具有完整 320x480 分辨率的图像。
You will have to create and add a CCSprite:
您必须创建并添加一个 CCSprite:
CCSprite* background = [CCSprite spriteWithFile:@"bg1.png"];
background.tag = 1;
background.anchorPoint = CGPointMake(0, 0);
[self addChild:background];
To schedule replacement use a scheduler:
要安排更换,请使用调度程序:
[self schedule:@selector(replaceBackground:) interval:1.0f];
When replacing the background, don't forget to remove the old background:
更换背景时,不要忘记删除旧背景:
-(void) replaceBackground:(ccTime)delta
{
// add new background here...
[self removeChildByTag:1];
}
Tag should increase with each image of course.
标签当然应该随着每个图像而增加。
One word of caution: loading a 320x480 (which will be a 512x512 texture in memory, using 1 MB of memory unless 16-bit or PVR compressed) from file will cause a noticeable lag. If you're doing an action game you will have to preload the background images. This will leave you with little memory to go with for the rest of the game (15 images x 1 MB = 15 MB of maybe 25 MB available memory).
一个警告:从文件加载 320x480(这将是内存中的 512x512 纹理,使用 1 MB 内存,除非 16 位或 PVR 压缩)将导致明显的延迟。如果您正在制作动作游戏,则必须预加载背景图像。这将使您在游戏的其余部分几乎没有内存可用(15 张图像 x 1 MB = 15 MB,可能有 25 MB 的可用内存)。
PS: more Q&A is available in the cocos2d forum: http://www.cocos2d-iphone.org/forumand i also keep adding FAQ answers to my http://www.learn-cocos2d.comwebsite.
PS:在 cocos2d 论坛中提供了更多问答:http: //www.cocos2d-iphone.org/forum并且我还在我的http://www.learn-cocos2d.com网站上不断添加常见问题解答。