我正在开发一个简单的 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)
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但问题不在于转换,而在于将节点加载到场景二中,这发生在此处:
loadLevelContents(level: 2, world: 1, borderPercentage: 30)
Run Code Online (Sandbox Code Playgroud)
定位效果很好,但问题是一切都是基于屏幕中间的中心点或相对 (0,0) 。
场景2的中心点位于左下角
如何将这个相对点或中心点(不确定它叫什么)更改为所有 SKScene 的屏幕中间
您正在寻找的点称为锚点。在 Spritekit 中,左下角是 0,0,右上角是 1, 1。所以要将它放在中心,你必须在 didMove 函数中设置 achorPoint 为 0.5,0.5
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
Run Code Online (Sandbox Code Playgroud)
对于SKScenes,默认值为(0,0),但不仅仅是SKScenes有锚点,SKSpriteNodes也有。
| 归档时间: |
|
| 查看次数: |
258 次 |
| 最近记录: |