Cocos2d添加背景图像到图层? [英] Cocos2d adding a background image to a layer?
问题描述
我刚刚阅读了一些cocos2d文档,根据我的理解,添加一个背景图片到一个图层你必须做一些事情:
I just finished reading some of the cocos2d documentation, and as far as I understand to add a background image to a layer you have to do something like:
Sprite * bg = [Sprite spriteWithFile:@"Background.png"];
[layer addChild:bg z:0];
尽管对我的测试,你可以直接将sprite添加到场景中, / p>
Allthough as far as my testing goes you can just directly add the sprite to the scene like this:
@implementation IntroScene
- (id) init {
self = [super init];
if (self != nil) {
Sprite * bg = [Sprite spriteWithFile:@"Background.png"];
[bg setPosition:ccp(240, 160)];
[self addChild:bg z:0];
[self addChild:[MenuLayer node] z:1];
}
return self;
}
这2个选项,什么是更好的方式来设置图层的背景?任何帮助将非常感激。
What is the difference between this 2 options, and what is the better way to set a background for a layer?. Any help will be greatly appreciated.
推荐答案
你不需要有一个图层,但他们很方便。将层视为将sprite(视觉和组织)分组的一种方式。你可能希望你的背景图片分组为一个层,所有的敌人在游戏中作为另一个层。这允许你更干净地处理每个集。例如您可以移除特定图层,而不是找到所有敌人并删除它们。
You do not need to have a layer, but they are handy. Think of the layers as a way of grouping sprites (both visually and organizationally). You may want your background images grouped as a layer and all the enemies in a game as another layer. This allows you to deal with each set more cleanly. e.g. You can remove a specific layer instead of finding all of the enemies and removing them.
如果有帮助,请在Photoshop或图形编辑程序中播放图层。 cocos2d中的图层类似。
If it helps, play with layers in Photoshop or a graphics editing program. Layers in cocos2d work similarly.
对于一个简单的场景,将背景添加到图层或场景不会有什么区别。但它不需要很久以前的分层的好处。
For a simple scene, adding the background to a layer or to a scene won't make much difference. But it doesn't take long before the benefits of layering kicks in.
这篇关于Cocos2d添加背景图像到图层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!