我想创建一个带有按 x 和 y 移动的矩形的游戏。 我的问题是:我是否移动 View (播放器的查看器),或者是否同时将矩形移动 x 和 y?
我尝试使用以下代码同时将矩形移动 x 和 y,但矩形仅移动 y。
@objc func addRects() {
rects = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: rects) as! [String]
rect = SKSpriteNode(imageNamed: rects[0])
rect.position = CGPoint(x: self.frame.width / 256 + 320, y: self.frame.height / 256)
rect.size = CGSize(width: 180, height: 120)
rect.zPosition = 1
self.addChild(rect)
moveByX = SKAction.moveTo(x: -500, duration: 2.0)
moveByY = SKAction.moveTo(y: -800, duration: 2.0)
removeRects = SKAction.removeFromParent()
wait = SKAction.wait(forDuration: 2.0)
rect.run(SKAction.sequence([moveByY,moveByY]))
rect.run(SKAction.sequence([wait,removeRects]))
}
如何解决这个问题? 谢谢
最佳答案
不要使用序列:
rect.run(moveByY)
rect.run(moveByX)
那么它应该同时移动“moveByY”和“moveByX”
另一个问题是,您使用了:
rect.run(SKAction.sequence([moveByY,moveByY]))
希望我的回答有帮助!
关于swift - 同时按 x 和 y 移动节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47337481/