gpt4 book ai didi

swift - 更改 SKScene 中心点

转载 作者:可可西里 更新时间:2023-11-01 00:57:18 27 4
gpt4 key购买 nike

我正在玩一个简单的 2d 游戏,我正在尝试从级别 1(场景一)切换到级别 2(场景二)。为了测试,两个级别包含相同的内容。这是我的转换代码:

    let transition = SKTransition.push(with: SKTransitionDirection.left, duration: 1)
let nextScene = Scene(size: levelBuilder.scene.size)
nextScene.scaleMode = .aspectFill
nextScene.backgroundColor = UIColor(red:0.17, green:0.24, blue:0.31, alpha:1.0)
levelBuilder.scene.view?.presentScene(nextScene, transition: transition)

nextScene.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
nextScene.physicsWorld.contactDelegate = levelBuilder.scene.physicsWorld.contactDelegate

loadLevelContents(level: 2, world: 1, borderPercentage: 30)

这很完美,但问题不在于过渡,而在于将节点加载到场景二中,发生在此处:

loadLevelContents(level: 2, world: 1, borderPercentage: 30)

定位没问题,但问题是一切都基于屏幕中间的中心点或相对 (0,0)

场景 2 的中心点位于左下角

enter image description here

虽然我需要它像场景 1 一样,它位于屏幕中间: enter image description here

如何将这个相对点或中心点更改为所有 SKScenes 的屏幕中间,不确定它叫什么

最佳答案

您正在寻找的点称为 anchor 。在 Spritekit 中,左下角是 0,0,右上角是 1, 1。所以要将它放在中心,您必须在 didMove 函数中设置 achorPoint 为 0.5,0.5

self.anchorPoint = CGPoint(x: 0.5, y: 0.5)

对于 SKScenes,默认值为 (0,0),但不仅 SKScenes 有 anchor ,SKSpriteNodes 也有。

enter image description here

关于swift - 更改 SKScene 中心点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43218213/

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