SKSpriteNode上没有运行SKAction(SKAction not running on SKSpriteNode)

编程入门 行业动态 更新时间:2024-10-19 15:24:22
SKSpriteNode上没有运行SKAction(SKAction not running on SKSpriteNode)

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 = .red

myNode 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

更多推荐

本文发布于:2023-07-24 00:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239243.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SKSpriteNode   SKAction   running

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!