SKAction上的SKShapeNode不起作用,代码没有执行,节点也没有移动,即使控制台正在记录“Snake正在移动”。 是因为节点是SKScene的一个属性,并且这些操作是较低范围函数的一部分?
class LevelScene: SKScene, SnakeShower { var snake: Snake { let theSnake = Snake(inWorld: self.size) return theSnake } override func didMove(to view: SKView) { self.backgroundColor = .green snake.delegate = self } var myNode: SKShapeNode { let node = SKShapeNode(rectOf: snake.componentSize) node.position = snake.head node.fillColor = .red return node } func presentSnake() { // function called by the snake in the delegate (self) self.addChild(myNode) startMoving() } func startMoving() { print("snake is moving") myNode.run(SKAction.repeatForever(SKAction.sequence([ SKAction.move(by: self.snake.direction.getVector(), duration: 0.2), SKAction.run({ if self.myNode.position.y > (self.size.height / 2 - self.snake.componentSize.height / 2) { self.myNode.removeAllActions() } }) ]))) } }当他们的财产被声明为与行为相同的功能时,它曾经工作
The SKAction on my SKShapeNode isn't working, the code isn't getting executed, and the node isn't moving, even though the console is logging "Snake is moving". Is is because the node is a property of the SKScene and the actions are part of lower scope functions?
class LevelScene: SKScene, SnakeShower { var snake: Snake { let theSnake = Snake(inWorld: self.size) return theSnake } override func didMove(to view: SKView) { self.backgroundColor = .green snake.delegate = self } var myNode: SKShapeNode { let node = SKShapeNode(rectOf: snake.componentSize) node.position = snake.head node.fillColor = .red return node } func presentSnake() { // function called by the snake in the delegate (self) self.addChild(myNode) startMoving() } func startMoving() { print("snake is moving") myNode.run(SKAction.repeatForever(SKAction.sequence([ SKAction.move(by: self.snake.direction.getVector(), duration: 0.2), SKAction.run({ if self.myNode.position.y > (self.size.height / 2 - self.snake.componentSize.height / 2) { self.myNode.removeAllActions() } }) ]))) } }It used to work when they property was declared in the same function as the action
最满意答案
myNode是一个计算属性。 self.addChild(myNode)添加一个节点,但没有myNode存储属性。
myNode.run首先计算一个新节点,但不将其添加到场景中。 然后它调用运行它的行动。 但由于它是不在现场的不同节点,所以它永远不会运行。
将您的myNode定义更改为:
var myNode : SKShapeNode!并在didMove(to view:添加:
myNode = SKShapeNode(rectOf: snake.componentSize) myNode.position = snake.head myNode.fillColor = .redmyNode is a computed property. self.addChild(myNode) adds a node, but there is no myNode stored property.
myNode.run First computes a new node without adding it to the scene. Then it calls the run the action on it. but since it's a different node that is not on the scene, it will never run.
Change your myNode defintion to:
var myNode : SKShapeNode!and in didMove(to view: add:
myNode = SKShapeNode(rectOf: snake.componentSize) myNode.position = snake.head myNode.fillColor = .red更多推荐
发布评论