使节点以相同的速度从不同的起点移动到终点

时间:2016-01-10 08:51:13

标签: swift animation sprite-kit skaction

我有一行SKSPriteNodes,我希望那些节点从屏幕顶部移动到底部,那些节点不在同一个CGPoint,它们在彼此后面形成一条线,当我使用正常的SKAction时。 MoveToY()和我给它持续时间它们以不同的速度移动,以便所有节点同时到达,我想要的是所有节点以相同的速度移动并一个接一个地到达,保持它们的线形式。

以下是我创建它们的方法:

     for i in 0...5 {
         let enemy = SKSPriteNode(imageNamed: "Enemy")
         enemy.position = CGPoint(x: size.width / 2, y: size.height + enemy.size.height * CGFloat(i))
         addChild(enemy)
         enemy.runAction(SKAction.moveToY(0 - size.height, duration: 5))
        }

2 个答案:

答案 0 :(得分:1)

因为动作的持续时间参数直接影响节点的移动速度,所以必须正确计算该参数,因为在您的情况下,每个敌方节点都应该行进不同的距离。正如我在评论中所说,计算时间(持续时间参数)的公式是:

  

时间=距离/速度

你可以这样做:

class GameScene: SKScene{

struct Constants {
    static let enemySpeed:CGFloat = 100
}

override func didMoveToView(view: SKView) {


    print("Scene height : \(size.height)")

    print("Enemy speed : \(Constants.enemySpeed)")


    for i in 1...6 {

        //Creating random colors just for easier debugging.
        let randomColor = UIColor(red   : CGFloat(arc4random()) / CGFloat(UInt32.max),
            green : CGFloat(arc4random()) / CGFloat(UInt32.max),
            blue  : CGFloat(arc4random()) / CGFloat(UInt32.max),
            alpha : 1.0)

        let enemy = SKSpriteNode(color: randomColor, size:CGSize(width: 30, height: 30))
        enemy.zPosition = 10

        // Time = Distance / Speed

        let distance:CGFloat = frame.size.height + CGFloat(i) * enemy.size.height
        print("Enemy \(i) will travel \(distance) points")

        let duration = NSTimeInterval(distance / Constants.enemySpeed)
        print("Enemy \(i) will stop moving after \(duration) seconds")

        let enemyX = CGRectGetMidX(frame)
        let enemyY = frame.size.height + enemy.size.height/2 +  CGFloat(i) * enemy.size.height

        enemy.position = CGPoint(x: enemyX, y: enemyY)

        print("Enemy \(i) start position is \(enemy.position)")

        addChild(enemy)

        let endPosition = -enemy.size.height/2

        let move = SKAction.moveToY(endPosition, duration: duration)

         print("Enemy \(i) end position is \(-enemy.size.height/2)")

        enemy.runAction(move)
    }
}

}

根据0x141E的建议,我已经纠正了距离计算。这是输出:

Scene height : 667.0
Enemy speed : 100.0
Enemy 1 will travel 697.0 points
Enemy 1 will stop moving after 6.97 seconds
Enemy 1 start position is (187.5, 712.0)
Enemy 1 end position is -15.0
Enemy 2 will travel 727.0 points
Enemy 2 will stop moving after 7.27 seconds
Enemy 2 start position is (187.5, 742.0)
Enemy 2 end position is -15.0
Enemy 3 will travel 757.0 points
Enemy 3 will stop moving after 7.57 seconds
Enemy 3 start position is (187.5, 772.0)
Enemy 3 end position is -15.0
Enemy 4 will travel 787.0 points
Enemy 4 will stop moving after 7.87 seconds
Enemy 4 start position is (187.5, 802.0)
Enemy 4 end position is -15.0
Enemy 5 will travel 817.0 points
Enemy 5 will stop moving after 8.17 seconds
Enemy 5 start position is (187.5, 832.0)
Enemy 5 end position is -15.0
Enemy 6 will travel 847.0 points
Enemy 6 will stop moving after 8.47 seconds
Enemy 6 start position is (187.5, 862.0)
Enemy 6 end position is -15.0

答案 1 :(得分:1)

尝试使用moveBy:而不是moveTo: - 这样你就可以使所有的精灵移动相同的量。