【发布时间】:2020-01-15 23:42:46
【问题描述】:
我创建了一个行为非常简单的类:这个类的每个对象都是一个顶部有名字的正方形,它们会在屏幕上随机浮动。
class XSquare: SKNode {
private var squareShape: SKSpriteNode
private var text: SKLabelNode
init() {
// Sets up the square
squareShape = SKSpriteNode(color: .red, size: CGSize(width: 100, height: 100))
// Sets up the text
text = SKLabelNode(text: "Here goes the name")
text.position = CGPoint(x: 0, y: 100)
// Calls the super initializator
super.init()
// Sets up the square
squareShape.physicsBody = SKPhysicsBody(rectangleOf: squareShape.size)
squareShape.physicsBody?.categoryBitMask = squareBitMask
squareShape.physicsBody?.collisionBitMask = wallBitMask
squareShape.physicsBody?.velocity = CGVector(dx: 10, dy: 10)
squareShape.physicsBody?.angularVelocity = CGFloat(5)
squareShape.physicsBody?.allowsRotation = true
squareShape.physicsBody?.pinned = false
squareShape.physicsBody?.affectedByGravity = false
addChild(squareShape)
addChild(text)
}
}
我在这个类中使用SKNode 的原因是因为我想在每个方块的顶部提供一个小文本(名称指示器)。
一切看起来都很好,但是当我运行代码时,名称保持固定,而方块在屏幕上随机移动(可能是因为我没有使用SKAction 移动方块,而是使用PhysicsBody)。另一方面,如果我使用squareShape.addChild(text),文本也会跟随正方形的物理特性旋转。
我是使用 SpriteKit 的新手,我确定我错过了一些东西。谁能帮我理解一下?
【问题讨论】:
标签: xcode sprite-kit skphysicsbody