为什么我的SKSpriteNode不能按照代码的意图直线移动?

时间:2019-05-08 22:36:52

标签: ios swift sprite-kit skspritenode

我有一个SKScene子类的代码,该代码显示了从右向左行驶的汽车。由于某种原因,汽车以一定角度而不是直线行驶。下面是我的子类的代码,不包括与该问题无关的部分。为什么汽车不能直线行驶?

override func didMove(to view: SKView) {

    run(SKAction.repeatForever(
        SKAction.sequence([
            SKAction.run(addCompetitor),
            SKAction.wait(forDuration: 3.0)
            ])
    ))

}

func random() -> CGFloat {
    return CGFloat(Float(arc4random()) / /* 0xFFFFFFFF */ 4294967296)
}

func random(min: CGFloat, max: CGFloat) -> CGFloat {
    return random() * (max - min) + min
}

func addCompetitor() {
    // Create sprite
    let car = SKSpriteNode(imageNamed: "utili")

    car.physicsBody = SKPhysicsBody(rectangleOf: car.size) // 1

    // Determine where to spawn the car along the Y axis
    let actualY = random(min: car.size.height/2, max: size.height - car.size.height/2)

    // Position the car slightly off-screen along the right edge,
    // and along a random position along the Y axis as calculated above
    car.position = CGPoint(x: size.width + car.size.width/2, y: actualY)

    // Add the car to the scene
    addChild(car)

    // Determine speed of the car
    let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0))

    // Create the actions
    let actionMove = SKAction.move(to: CGPoint(x: -car.size.width/2, y: actualY), duration: TimeInterval(actualDuration))
    let actionMoveDone = SKAction.removeFromParent()
    car.run(SKAction.sequence([actionMove, actionMoveDone]))
}

0 个答案:

没有答案