Cocos2d 给图层添加背景图片?

Cocos2d adding a background image to a layer?(Cocos2d 给图层添加背景图片?)

本文介绍了Cocos2d 给图层添加背景图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了一些 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];

尽管就我的测试而言,您可以像这样直接将精灵添加到场景中:

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;

}

这两个选项有什么区别,为图层设置背景的更好方法是什么?任何帮助将不胜感激.

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.

推荐答案

你不需要有层,但它们很方便.将图层视为对精灵进行分组的一种方式(无论是在视觉上还是在组织上).您可能希望将背景图像分组为一个图层,并将游戏中的所有敌人作为另一个图层.这使您可以更干净地处理每个集合.例如您可以移除特定层,而不是找到所有敌人并移除它们.

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 给图层添加背景图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Cocos2d 给图层添加背景图片?

基础教程推荐