我正在开发一款有角色的游戏。该字符是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(在角色创建并添加到场景之后,位置就定位了)。
我在这里想念什么?
答案 0 :(得分:2)
PulzeNode.position = CGpoint.zero
当您将其添加到角色中时,它应该位于父节点的原点