如何在 cocos2d xcode 中为整个场景设置背景图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12383228/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:27:59  来源:igfitidea点击:

How to set background image for the entire scene in cocos2d xcode?

xcodecocos2d-iphone

提问by Sahara Pune

I'm working with cocos2D.I want to set the background image for the entire scene just I'm getting the image within the scene but it's not covering the entire screen.can anyone provide me some information regarding this.

我正在使用 cocos2D。我想为整个场景设置背景图像,只是我在场景中获取图像,但它没有覆盖整个屏幕。任何人都可以向我提供有关此的一些信息。

Thanks.

谢谢。

回答by Morion

use picture with size, equal to size of the screen(480x320 for non-retina iPhone/iPod, 960x640 for retina iPhone/iPod, 1024x768 for iPad, 2048x1536 for retina iPad). If you want to stretch picture to make it fill whole screen, change it's scaleXand scaleYproperties:

使用大小等于屏幕大小的图片(非视网膜 iPhone/iPod 为 480x320,视网膜 iPhone/iPod 为 960x640,iPad 为 1024x768,视网膜 iPad 为 2048x1536)。如果要拉伸图片以使其填满整个屏幕,请更改它的scaleXscaleY属性:

CGSize winSize = [[CCDirector sharedDirector] winSize];
CGSize imageSize = image.contentSize;
imageSprite.scaleX = winSize.width / imageSize.width;
imageSprite.scaleY = winSize.height / imageSize.height;

回答by Prabakaran

To set the background for cocos2d 3.x

为 cocos2d 3.x 设置背景

CGSize wSize=[[CCDirector sharedDirector] viewSize];
CGSize scrSize={640,1136};
CCSprite* background1 = [CCSprite spriteWithFile:@"1136.png"];
background1.anchorPoint=ccp(0, 0);
background1.scaleX=wSize.width/scrSize.width;
background1.scaleY=wSize.height/scrSize.height;
[self addChild:background1];