2016-12-10 60 views

回答

1

虽然这可能不是最好的方法,但这是我总是这样做,它的工作原理。

假设你有一个形象,是完全一样的艾泽为您的场景,你可以这样做:

// please declare bg as a class level variable 
bg = SKSpriteNode(imageNamed: "name of your texture") 

// the below two lines of code is my preference only. I want the 
// background's anchor point to be the bottom left of the screen 
// because IMO it's easier to add other sprites as children of the background. 
bg.anchorPoint = CGPoint.zero 
bg.position = CGPoint(x: self.frame.width/-2, y: self.frame.height/-2) 

self.addChild(bg) 

Alernatively,只是这样做在SKS文件。这很容易。

之后,添加所有的游戏精灵作为bg的孩子,而不是self,因为它更容易管理。所有的

2

首先,你可以叫你的场景与修改SKScene的实际尺寸完全匹配SKView将scaleMode .resizeFill

scene.scaleMode = .resizeFill 

.resizeFill - 现场不进行缩放。它只是调整大小,以使其符合观点。由于场景未缩放,图像将保持其原始大小和宽高比。内容将全部保持相对于场景原点(左下角)。

默认情况下,场景的原点位于视图的左下角。因此,场景初始化的高度为1024,宽度为768,左下角为原点(0,0),右上角为(1024,768)坐标。框架属性为(0,0) - (1024,768)。定位点的默认值为CGPointZero(因此您不需要更改它),它将其放置在左下角。

最后,你可以用下面的代码添加到您的背景图片(例如所谓的bg.jpg):

// Set background 
let txt = SKTexture(imageNamed: "bg.jpg") 
let backgroundNode = SKSpriteNode(texture: txt, size:size) 
self.addChild(backgroundNode) 
backgroundNode.position = CGPoint(x: self.frame.midX, y: self.frame.midY)