如何在节点树中的不同层次结构的节点之间建立联系,例如场景中的节点(球)和另一个节点的子节点(墙)。
我试图建造一个有4个墙的板,它是板的子节点。然后我在板节点上添加了一个球(我也尝试让球在场景中)让它移动。在定义了物理体之后,我仍然无法得到联系。
这是我的代码:
class GameScene: SKScene {
let plate = SKSpriteNode ()
let wall1 = SKSpriteNode ()
let wall2 = SKSpriteNode ()
let wall3 = SKSpriteNode ()
let wall4 = SKSpriteNode ()
var ball = SKShapeNode ()
override func didMove(to view: SKView) {
self.backgroundColor = .white
plate.size = CGSize (width: self.size.width * 0.6 , height: self.size.width * 0.6)
plate.position = CGPoint (x: self.size.width / 2 , y: self.size.height / 2 )
plate.color = .orange
plate.zPosition = 0
self.addChild(plate)
plate.physicsBody = SKPhysicsBody (edgeLoopFrom: plate.frame)
plate.physicsBody?.isDynamic = false
plate.physicsBody?.friction = 0
plate.physicsBody?.restitution = 1
plate.physicsBody?.affectedByGravity = false
plate.physicsBody?.categoryBitMask = 1
plate.physicsBody?.contactTestBitMask = 0
plate.physicsBody?.collisionBitMask = 0
wall1.size = CGSize (width: plate.size.width * 0.1, height: plate.size.height)
wall1.position = CGPoint (x: plate.size.width / 2, y: 0 )
wall1.color = .gray
wall1.zPosition = 2
plate.addChild(wall1)
wall1.physicsBody = SKPhysicsBody (edgeLoopFrom: wall1.frame)
wall2.size = CGSize (width: plate.size.width * 0.1, height: plate.size.height)
wall2.position = CGPoint (x: 0, y: plate.size.height / 2 )
wall2.zRotation = plate.zRotation + .pi/2
wall2.color = .yellow
wall2.zPosition = 2
plate.addChild(wall2)
wall2.physicsBody = SKPhysicsBody (edgeLoopFrom: wall2.frame)
wall3.size = CGSize (width: plate.size.width * 0.1, height: plate.size.height)
wall3.position = CGPoint (x: -plate.size.width / 2, y: 0 )
wall3.color = .cyan
wall3.zPosition = 2
plate.addChild(wall3)
wall3.physicsBody = SKPhysicsBody (edgeLoopFrom: wall3.frame)
wall4.size = CGSize (width: plate.size.width * 0.1, height: plate.size.height)
wall4.position = CGPoint (x: 0, y: -plate.size.height / 2 )
wall4.zRotation = plate.zRotation + .pi/2
wall4.color = .magenta
wall4.zPosition = 2
plate.addChild(wall4)
wall4.physicsBody = SKPhysicsBody (edgeLoopFrom: wall4.frame)
wall4.physicsBody?.isDynamic = false
wall4.physicsBody?.friction = 0
wall4.physicsBody?.restitution = 1
wall4.physicsBody?.affectedByGravity = false
wall4.physicsBody?.categoryBitMask = 1
wall4.physicsBody?.contactTestBitMask = 0
wall4.physicsBody?.collisionBitMask = 0
ball = SKShapeNode (circleOfRadius: 10)
ball.strokeColor = .black
ball.lineWidth = 2
ball.fillColor = .blue
ball.position = CGPoint (x: self.size.width/2, y: self.size.height/2)
ball.zPosition = 2
plate.addChild(ball)
ball.physicsBody = SKPhysicsBody (circleOfRadius: 10)
ball.physicsBody?.isDynamic = true
ball.physicsBody?.affectedByGravity = true
ball.physicsBody?.friction = 0
ball.physicsBody?.restitution = 1
ball.physicsBody?.categoryBitMask = 0
ball.physicsBody?.contactTestBitMask = 1
ball.physicsBody?.collisionBitMask = 1
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
plate.zRotation -= .pi/36
}}
我的球和板块本身的边界之间甚至无法接触。
谢谢