gpt4 book ai didi

ios - 使用 SpriteKit 的 GameScene 中的 0,0 在哪里?

转载 作者:行者123 更新时间:2023-11-28 09:07:27 25 4
gpt4 key购买 nike

我在将 Sprite 定位到游戏场景中的正确点时遇到问题。据我阅读position and anchorpointspritekit programming guide应该清楚 0,0 在左下角。 我只使用横向模式。

但是当我使用我的代码时:

import SpriteKit

class GameScene: SKScene {

override func didMoveToView(view: SKView) {
var ground = createGround()
self.addChild(ground)
}

func createGround()->SKSpriteNode {
var ground = SKSpriteNode(imageNamed: "ground.png")
ground.name = "ground"
ground.anchorPoint = CGPointMake(0, 0)
ground.position = CGPointMake(0, 0)

return ground
}
}

我实现了非常标准的场景:

override func viewDidLoad() {
super.viewDidLoad()

if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true

/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true

/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill

skView.presentScene(scene)
}
}

这是结果:

Screenshot of iPhone Simulator

我的“ground.png”大小为 120x120。我仔细检查了场景 anchor ,它位于 (0,0),即使我在 didMoveToView 中将它设置为 (0,0),我仍然遇到同样的问题。

不知何故,Y 因素被搞乱了。当我将地面位置设置为 (0,156) 时,它直接位于左下角。所以它看起来像游戏场景 y-anchor-point 是 -36 并且地面的 anchor 在左上而不是左下。任何线索为什么?

我找到了 almost same problem但是 frame 在 (0.0, 0.0, 1024.0, 768.0) 下看起来不错。

最佳答案

(.5,.5) 的 sprite anchor 意味着它将在放置时通过其中心定位。因此,如果它位于屏幕左下角,您将只能看到图像的上半部分和右半部分。

如果您希望图像完全适合左下角而不需要额外的数学运算,请将其 anchor 设置为:

sprite.anchorPoint = CGPointMake(0, 1)

关于ios - 使用 SpriteKit 的 GameScene 中的 0,0 在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30444718/

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