改变node / Swift 3.0的速度

时间:2016-12-08 06:11:33

标签: swift sprite-kit

我正在尝试更改节点的速度。现在我的节点只需在你触摸的地方移动屏幕 - 非常基本的测试。我只是想让我的第一个节点(bubble1 - 参考代码)移动得更快一点,这样我就可以再次改变速度等等。感谢您的帮助。

*现在我只想改变" bubble1"的速度,没有别的。我查看了有关如何执行此操作的其他帖子,但没有一个非常真实的更新。所以我发布了自己的。 =)

import SpriteKit
import GameplayKit

class GameScene: SKScene {

//var simpleS = SKSpriteNode()
var ball = SKSpriteNode(imageNamed: "Ball")
var bubble = SKSpriteNode(imageNamed: "square")
var bubble1 = SKSpriteNode(imageNamed: "square")
var bubble2 = SKSpriteNode(imageNamed: "square")
var bubble3 = SKSpriteNode(imageNamed: "square")
var bubble4 = SKSpriteNode(imageNamed: "square")


override func didMove(to view: SKView) {

    //simpleS = self.childNode(withName: "simpleS") as! SKSpriteNode

    //simpleS.physicsBody?.affectedByGravity = true
    //simpleS.setScale(3.0)

    ball.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    ball.setScale(0.1)
    //self.addChild(ball)

    bubble.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bubble.setScale(0.3)
    self.addChild(bubble)

    bubble1.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bubble1.setScale(0.3)
    self.addChild(bubble1)

    bubble2.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bubble2.setScale(0.3)
    self.addChild(bubble2)

    bubble3.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bubble3.setScale(0.3)
    self.addChild(bubble3)

    bubble4.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bubble4.setScale(0.3)
    self.addChild(bubble4)     
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {        
    for touch in touches {
        let location = touch.location(in: self)

        //simpleS.run(SKAction.moveTo(x: location.x, duration: 0.5))
        //simpleS.run(SKAction.moveTo(y: location.y, duration: 0.5))

        ball.run(SKAction.moveTo(x: location.x, duration: 0.0))
        ball.run(SKAction.moveTo(y: location.y, duration: 0.0))

        bubble.run(SKAction.moveTo(x: location.x, duration: 0.3))
        bubble.run(SKAction.moveTo(y: location.y, duration: 0.3))

        bubble1.run(SKAction.moveTo(x: location.x, duration: 0.4))
        bubble1.run(SKAction.moveTo(y: location.y, duration: 0.4))

        bubble2.run(SKAction.moveTo(x: location.x, duration: 0.5))
        bubble2.run(SKAction.moveTo(y: location.y, duration: 0.5))

        bubble3.run(SKAction.moveTo(x: location.x, duration: 0.6))
        bubble3.run(SKAction.moveTo(y: location.y, duration: 0.6))

        bubble4.run(SKAction.moveTo(x: location.x, duration: 0.8))
        bubble4.run(SKAction.moveTo(y: location.y, duration: 0.8))
    }
}

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。

  1. 更改持续时间,因为这会加快速度,但我不认为这是你要问的。我想你想知道如何加快SKAction ......

  2. 通过速度属性

  3. 更改SKAction的执行率

    记录速度属性here.

    您需要做一些事情才能使这个值可访问并且对于bubble1唯一可寻址

    1. 使bubble1上的SKAction独一无二,因此您可以控制它。有两种方法:

      • 在其&#39; .name&#39;中为其命名。财产,查找并找到
      • 引用bubble1并解决其SKAction(a
    2. 通过更改其.speed属性告诉影响bubble1的SKAction运行得更快

    3. 或者......你可以懒惰,只需调整任何给定节点上所有操作的速度值,使用:https://developer.apple.com/reference/spritekit/sknode/1483036-speed