如何防止 child 转换为 parent
How to keep child from transforming with parent
我正在创建一个从枪管中射出子弹的函数。我将子弹作为枪支的 child 保留,以便无论玩家面向何处都能将其放置在正确的位置。但是,如果我在玩家移动时射击,子弹将相对于玩家移动。如何在子弹产生时删除相对引用。
func creatBullet() {
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.zPosition = 4
bullet.position = CGPoint(x: gun.position.x-1, y: gun.position.y-20)
//add physics
gun.addChild(bullet)
let xDirection = CGFloat(bullet.position.x)
let yDirection = CGFloat(bullet.position.y + 150)
let bulletMove = SKAction.moveBy(x: xDirection, y: yDirection, duration: 2)
let sequence = SKAction.sequence([bulletMove, SKAction.removeFromParent()])
bullet.run(sequence)
}
来自 Apple...
Converts a point in this node’s coordinate system to the coordinate
system of another node in the node tree. Declaration
func convert(_ point: CGPoint, to node: SKNode) -> CGPoint
Parameters
point A point in this node’s coordinate system.
node Another node in the same node tree as this node.
Returns The same point converted to the other node’s coordinate
system.
所以我只会使用(假设自己是场景)
let pos = convert(gun.position, to self)
bullet.position = pos
addChild(bullet)
EDIT
如果您的对象是另一个对象的子对象,您可能必须使用 convert from 和 convert to 来获取绝对部分
pos = convert(convert(gun.position, from: gun.parent!), to: self)
我正在创建一个从枪管中射出子弹的函数。我将子弹作为枪支的 child 保留,以便无论玩家面向何处都能将其放置在正确的位置。但是,如果我在玩家移动时射击,子弹将相对于玩家移动。如何在子弹产生时删除相对引用。
func creatBullet() {
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.zPosition = 4
bullet.position = CGPoint(x: gun.position.x-1, y: gun.position.y-20)
//add physics
gun.addChild(bullet)
let xDirection = CGFloat(bullet.position.x)
let yDirection = CGFloat(bullet.position.y + 150)
let bulletMove = SKAction.moveBy(x: xDirection, y: yDirection, duration: 2)
let sequence = SKAction.sequence([bulletMove, SKAction.removeFromParent()])
bullet.run(sequence)
}
来自 Apple...
Converts a point in this node’s coordinate system to the coordinate system of another node in the node tree. Declaration
func convert(_ point: CGPoint, to node: SKNode) -> CGPoint
Parameters
point A point in this node’s coordinate system.
node Another node in the same node tree as this node.
Returns The same point converted to the other node’s coordinate system.
所以我只会使用(假设自己是场景)
let pos = convert(gun.position, to self)
bullet.position = pos
addChild(bullet)
EDIT
如果您的对象是另一个对象的子对象,您可能必须使用 convert from 和 convert to 来获取绝对部分
pos = convert(convert(gun.position, from: gun.parent!), to: self)