如何在swift中正确缩放并模拟深度

时间:2017-08-24 18:48:35

标签: swift sprite-kit swift-playground depth

我想放大或缩小,前景和背景应该正确缩放。我不是缩放两个单独的元素,而是缩放到1.2主图层并将背景元素设置为0.8比例

Screenshot of the result

import SpriteKit
import PlaygroundSupport

//Basic dimensions that we will use more later
let frame = CGRect(x: 0, y: 0, width: 500, height: 500)

//Create a scene, add something to it
var scene = SKScene(size: frame.size)
scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)


let layer = SKNode()

let background = SKNode()
let foreground = SKNode()

let foregroundBox = SKSpriteNode(color: UIColor.brown, size: CGSize(width: CGFloat(400), height: 40))
foregroundBox.position.y = -50
foregroundBox.zPosition = 101

let backgroundBox = SKSpriteNode(color: UIColor.gray, size: CGSize(width: CGFloat(400), height: 40))
backgroundBox.position.y = 0
backgroundBox.zPosition = 9

foreground.addChild(foregroundBox)
background.addChild(backgroundBox)

background.setScale(0.8)

scene.addChild(foreground)
layer.addChild(background)

scene.addChild(layer)

// Should make the gray box the same size as the brown
layer.setScale(1.2)

//Set up the view and show the scene
let view = SKView(frame: frame)
view.presentScene(scene)
PlaygroundPage.current.liveView = view

两个方框都有400宽度,但背景元素有setScale(0.8)。

通过将父图层缩放到1.2(20%),它应该使背景元素再次具有400宽度

PS。我没有将前景添加到主图层,以便查看问题

scene.addChild(foreground)

0 个答案:

没有答案