gpt4 book ai didi

ios - 如何使节点位置适应横向和纵向

转载 作者:行者123 更新时间:2023-11-30 11:00:41 24 4
gpt4 key购买 nike

我正在尝试制作一个通用的游戏板,可以在 ipad/iphone 上以纵向或横向方式玩。这些瓷砖在纵向看起来不错,但当我切换到横向时,它会被切断。我如何确保板始终居中?

override func didMove(to view: SKView) {

let numRows = 7
let numCols = 7
var counter = 0
let squareSize = CGSize(width: view.bounds.width/7, height: view.bounds.width/7)

for row in 0..<numRows{
for col in 0..<numCols {
if counter == 49 {
break
}
let spriteNode = SKSpriteNode(imageNamed: "piece\(counter)")
spriteNode.size = squareSize
spriteNode.position = CGPoint(x: 0-(frame.width*0.43)+(CGFloat(col)*spriteNode.size.width), y: -CGFloat(row)*spriteNode.size.height + frame.height/3 )
addChild(spriteNode)
counter = counter+1
}
}
}

override func viewDidLoad() {
super.viewDidLoad()

if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .resizeFill
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}

看起来像这样:

enter image description here

enter image description here

最佳答案

我会将场景缩放模式从 scene.scaleMode = .resizeFill 更改为 scene.scaleMode = .aspectFit。在游戏中,SKView 帧大小不是必需的,您可以在自己的坐标空间(场景的大小属性)内工作,例如设置为 100 x 100。-> Interface Builder 设置。

不要在 SKView 的 View 框架/边界上关联任何内容。场景 Controller 中的代码应该如下所示工作,所有这些都与场景的大小有关。

override func didMove(to view: SKView) {

let numRows = 7
let numCols = 7
var counter = 0
let squareSize = CGSize(width: size.width/7, height: size.width/7)

for row in 0..<numRows{
for col in 0..<numCols {
if counter == 49 {
break
}
let spriteNode = SKSpriteNode(imageNamed: "piece\(counter)")
spriteNode.size = squareSize
spriteNode.position = CGPoint(x: 0-(size.width*0.43)+(CGFloat(col)*spriteNode.size.width), y: -CGFloat(row)*spriteNode.size.height + size.height/3 )
addChild(spriteNode)
counter = counter+1
}
}
}

关于ios - 如何使节点位置适应横向和纵向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53417581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com