我在场景中间有四个正方形,设置了各种锚点.当敲击时,它们一起移动并根据什么位置分开:
func rotate(angle : CGFloat, animated : Bool) { var rotateAction : SKAction! if animated { rotateAction = SKAction.rotateByAngle(angle, duration: 0.6) } else { rotateAction = SKAction.rotateByAngle(angle, duration: 0) } for node in self.children as! [SKSpriteNode] { node.runAction(rotateAction) } }
}
我遇到的问题是,节点的物理主体严格地停留在锚点而不是节点本身,这给我带来了一堆问题.我怎样才能使我能够拥有每个节点所需的锚点并使物理体直接停留在节点上?如有必要,将发布更多代码.
你需要将锚点应用到物理体,它不知道sprite是什么,它只是sprite使用的另一条信息,所以使用下面的计算来确定sprite的中心应该在哪里,并应用它物理机构,以便它可以转移:
let centerPoint = CGPointMake(sprite.size.width / 2 - (sprite.size.width * sprite.anchorPoint.x), sprite.size.height / 2 - (sprite.size.height * sprite.anchorPoint.y)) sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size, center: centerPoint)
Swift 3+:
let centerPoint = CGPoint(x:sprite.size.width / 2 - (sprite.size.width * sprite.anchorPoint.x), y:sprite.size.height / 2 - (sprite.size.height * sprite.anchorPoint.y)) sprite.physicsBody = SKPhysicsBody(rectangleOf: sprite.size, center: centerPoint)
减少文字:
let centerPoint = CGPoint(x:sprite.size.width * (0.5 - sprite.anchorPoint.x), y:sprite.size.height *(0.5 - sprite.anchorPoint.y))