简体   繁体   English

如何更改SpriteKit的坐标系

[英]How to change SpriteKit's coordinate system

All SKSpriteNode s have their origin, also called anchorPoint , if I'm not wrong, in their middle. 所有SKSpriteNode的起源都在它们中间,如果我没记错的话,也称为anchorPoint I want to translate that origin to be in the middle of the top line of the node. 我想将该原点翻译为该节点顶行的中间。 I've tried the following inside the init() method, but it doesn't work: 我已经在init()方法中尝试了以下方法,但是它不起作用:

self.anchorPoint = self.anchorPoint.applying(
    CGAffineTransform.init(translationX: 0, y: self.frame.size.height/2)
)

PS: I'm using this to simplify the following: I've created an array of SpriteNodes, which all have are in the new CGPoint.zero position, then, i'll have to apply this on them: PS:我正在使用它来简化以下操作:我创建了一个SpriteNodes数组,所有这些数组都位于新的CGPoint.zero位置,然后,我将其应用于它们:

for i in 0..<arr.count {
    arr[i].position.y += self.size.height*CGFloat((i+1)/(arr.count+1))
}

Here is the code: 这是代码:

import SpriteKit

class SKLayer: SKSpriteNode {

init(inside scene: SKScene, withLabels texts: [String]) {
    super.init(texture: nil, color: .clear, size: scene.size)

    self.anchorPoint = CGPoint(x: 0.5, y: 1)
    converted(texts).forEach(self.addChild(_:))

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

private func converted(_ text: [String]) -> [SKNode] {

    var arr = [SKLabelNode]()

    text.forEach {
        arr.append(SKLabelNode(text: $0))
    }

    for i in 0..<arr.count {
        arr[i].position.y = CGFloat(i) * arr[0].frame.height
    }

    return arr
}

required init?(coder aDecoder: NSCoder) {
    fatalError()
}

}

class GameScene: SKScene {

override func didMove(to view: SKView) {
    self.addChild(
        SKLayer(inside: self, withLabels: ["Welcome", "Hello", "Bye", "Help"])
    )
}

}

Here's the pic of what the code compiles to: 这是代码编译成的图片: 屏幕截图

I want the nodes to go from up to down, not the other way around. 我希望节点从上到下,而不是相反。 Iow, I wanna see the "Welcome" Label at the top, then the "texT" one, then the others. 现在,我想在顶部看到“ Welcome”标签,然后是“ texT”标签,然后是其他标签。

According to the docs , 根据文档

This is how anchor points work: 这是锚点的工作方式:

Anchor points are specified in the unit coordinate system, shown in the following illustration. 锚点在单位坐标系中指定,如下图所示。 The unit coordinate system places the origin at the bottom left corner of the frame and (1,1) at the top right corner of the frame. 单位坐标系将原点放置在框架的左下角,(1,1)放置在框架的右上角。 A sprite's anchor point defaults to (0.5,0.5), which corresponds to the center of the frame. 精灵的锚点默认为(0.5,0.5),它对应于框架的中心。

在此处输入图片说明

From this image, we can see that to set the anchor point to the middle of the top border, you need to set it to (0.5, 1) . 从此图像中,我们可以看到要将锚点设置到顶部边框的中间,需要将其设置为(0.5, 1)

You tried to transform the anchor point to self.frame.size / 2 which is probably a number larger than 1. Well, according to the docs, the range of (0, 0) - (1, 1) covers the whole frame of the sprite node. 您尝试将锚点转换为self.frame.size / 2 ,该数字可能大于1。好吧,根据文档, (0, 0) - (1, 1)范围覆盖了精灵节点。 If you set it to a value larger than (1, 1), the anchor point will be outside of the frame of the node. 如果将其设置为大于(1,1)的值,则锚点将位于节点框架的外部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM