我为某种类型的节点创建了一个子类,称为TargetNode。这是什么样的,以及我如何为该子类定义PhysicsBody。
class TargetNode: SKSpriteNode, CustomNodeEvents {
func didMoveToScene() {
isUserInteractionEnabled = true
physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.size.width, height: self.size.height))
physicsBody!.isDynamic = true
physicsBody!.restitution = 1.0
physicsBody!.allowsRotation = true
physicsBody!.affectedByGravity = true
}
}
然后,在场景中,我尝试将其添加到场景中(这是一场游戏)。
let name = createTarget()
let targetNode = TargetNode(imageNamed: name)
targetNode.didMoveToScene() // added this after question below
targetNode.name = name
fgNode.addChild(targetNode)
chickenNodes.append(targetNode)
targetNode.physicsBody?.velocity = CGVector(dx: 100, dy: 0)
targetNode.position = generateZoneAndPoint()
然而,即使我已经为场景定义了PhysicsWorld等,节点也没有响应引力:
let playableRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
self.physicsBody = SKPhysicsBody(edgeLoopFrom: playableRect)
self.physicsWorld.contactDelegate = self
它也没有回应我申请的任何冲动。为什么会这样?我需要调整或更改什么?
编辑:我之前也调用了这些节点......代码是:
enumerateChildNodes(withName: "*//*, using: { node, _ in
if let customNode = node as? CustomNodeEvents {
customNode.didMoveToScene()
}
})