【问题标题】:Sprite move once time in random positions精灵在随机位置移动一次
【发布时间】:2017-05-12 12:19:20
【问题描述】:

我试图移动精灵在屏幕上随机移动,但精灵移动一次到随机位置并停止移动

我在这里调用以使形状与计时器一起使用

    //Make shape Timer
func makeshapetimer () {
    maketimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(makerandomShape), userInfo: nil, repeats: true)
}

//Make random shape
func makerandomShape () {

    //Check if have more than 12 shapes
    if shapesamount <= 12 {

        let sprite = shape.copy() as! SKShapeNode
        sprite.name = "Shpae \(shapesamount)"
        sprite.position = CGPoint(x: frame.minX - sprite.frame.width, y: frame.maxY)

        shapes.addChild(sprite)

        shapesamount += 1

        moveRandom(node: sprite)
    }
}

在这里我做了一个随机的位置动作并永远重复这个动作,但每个形状只运行一次

//Move shape radmonly
func moveRandom (node: SKShapeNode) {

    move = SKAction.move(to: CGPoint(x: CGFloat.random(min: frame.minX, max: frame.maxX), y: CGFloat.random(min: frame.minY, max: frame.maxY)), duration: shapespeed)

    node.run(SKAction.repeatForever(move))
}

【问题讨论】:

  • 不要在 sprite-kit 中使用 Timer - 改用 SKAction。计时器不会遵守 sprite-kit 游戏循环,不会;如果场景暂停等,则不会停止等
  • SKAction.wait 代替计时器

标签: ios random sprite-kit action


【解决方案1】:

您的moveRandom 函数每个精灵只调用一次。

这是你告诉它做的事情:

  • 得到一个随机的 x,y 位置 --- 假设它有 120,200
  • 移动到 120,200 --- 并且永远重复移动到 120,200

因此,精灵尽职尽责地移动到那个随机位置,并继续移动到那个位置。它永远不会回到起点,也永远不会移动到新的位置。

如果您希望精灵继续移动到个随机位置,则需要在每次当前移动完成时创建一个新的随机位置。

【讨论】:

    猜你喜欢
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2014-10-09
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多