我定义了几个SKActions,例如下面的SKActions,将各个节点移动到给定位置。我将目标X坐标定义为platform.position.x,将目标Y坐标定义为platform.position.y - 900.预期的行为是节点将向下移动900,但保持在相同的x位置。相反,节点以一定角度向下移动,朝向彼此,最终在中心处终止,然后最终消失在屏幕外。我无法弄清楚为什么会这样。
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMove(to view: SKView) {
let platform1 = SKSpriteNode(color: UIColor.yellow, size: CGSize(width: 400, height: 60))
platform1.xScale = 0.8
platform1.yScale = 0.8
platform1.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: platform1.size.width, height: platform1.size.width / 6))
platform1.position = CGPoint(x: -170, y: -270)
platform1.physicsBody?.categoryBitMask = 1
platform1.physicsBody?.collisionBitMask = 0
platform1.physicsBody?.isDynamic = false
platform1.physicsBody?.affectedByGravity = false
platform1.physicsBody?.friction = 0
addChild(platform1)
let platform1GlideOff = SKAction.move(to: CGPoint(x: platform1.position.x, y: platform1.position.y - 900), duration: TimeInterval(5))
var firstActionplatform1 = SKAction.run({
platform1.run(platform1GlideOff)
})
let platform2 = SKSpriteNode(color: UIColor.green, size: CGSize(width: 400, height: 60))
platform2.xScale = 0.8
platform2.yScale = 0.8
platform2.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: platform2.size.width, height: platform2.size.width / 6))
platform2.position = CGPoint(x: 200, y: -270)
platform2.physicsBody?.categoryBitMask = 1
platform2.physicsBody?.collisionBitMask = 0
platform2.physicsBody?.isDynamic = false
platform2.physicsBody?.affectedByGravity = false
platform2.physicsBody?.friction = 0
addChild(platform2)
let platform2GlideOff = SKAction.move(to: CGPoint(x: platform1.position.x, y: platform1.position.y - 900), duration: TimeInterval(5))
var firstActionplatform2 = SKAction.run({
platform2.run(platform2GlideOff)
})
var sequence = SKAction.sequence([SKAction.wait(forDuration: 1), firstActionplatform1, firstActionplatform2])
run(sequence)
}
}