Cocos2d adding a background image to a layer?(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 给图层添加背景图片?
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01