【问题标题】:SKSpriteNode Subclass Position issueSKSpriteNode 子类位置问题
【发布时间】:2019-03-09 22:56:37
【问题描述】:

我正在开发一个你有角色的游戏。该角色是 SKSpriteNode 的子类:

class Character: SKSpriteNode {

init() {
    let texture = SKTexture(imageNamed: "character")
    super.init(texture: texture, color: UIColor.white, size: texture.size())
    self.isUserInteractionEnabled = true
    self.zPosition = 10
    self.position = CGPoint(x: 0, y: 0)
    self.name = "character"
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("Touch!")

    // Create small +1 sprite
    let plusNode = SKSpriteNode(imageNamed: "plus1Node")
    plusNode.zPosition = 10000 //To make sure its always in front
    plusNode.position = self.position
    self.addChild(plusNode)
}

角色通过 GameScene.swift 添加到游戏中:

    func spawnCharacter() {
        //Random postion for the pigs
        let randomX = Functions().randomBetweenNumbers(firstNum: -140, secondNum: 140)
        let randomY = Functions().randomBetweenNumbers(firstNum: -240, secondNum: 240)

        let newCharacter = Character()
        newCharacter = CGPoint(x: randomX, y: randomY)
        gameArea!.addChild(newCharacter)
    }

当玩家点击角色时,会生成一个小的“+1”节点,以向玩家显示他已经触摸了角色。我希望 +1 节点生成在角色的顶部,但它放置得很远 - 就像多个点一样。

如果我将 +1 代码移动到 GameScene(就在角色创建并添加到场景之后,位置就在现场)。

我在这里缺少什么?

【问题讨论】:

  • 您将 plusNode 添加到错误的节点。

标签: swift sprite-kit


【解决方案1】:
PulzeNode.position = CGpoint.zero

当你将它添加到角色时,它应该在父节点的原点

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多