Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Swift 3随机移动精灵_Swift_Sprite Kit - Fatal编程技术网

使用Swift 3随机移动精灵

使用Swift 3随机移动精灵,swift,sprite-kit,Swift,Sprite Kit,我试着做一个游戏,让龙随机移动,英雄必须避开它。我可以让龙出现在一个随机的位置,移动一两次,但是让它不断地从一个点移动到另一个点,然后再移动一些给我带来了麻烦。我想这可能是因为我没有在生成所有随机数之前等待操作完成。我尝试了以下代码,包括标签,以证明自己的随机数正在生成,至少有20 import SpriteKit import GameplayKit class GameScene: SKScene { private let greenDragonNode = GreenDragonSp

我试着做一个游戏,让龙随机移动,英雄必须避开它。我可以让龙出现在一个随机的位置,移动一两次,但是让它不断地从一个点移动到另一个点,然后再移动一些给我带来了麻烦。我想这可能是因为我没有在生成所有随机数之前等待操作完成。我尝试了以下代码,包括标签,以证明自己的随机数正在生成,至少有20

import SpriteKit
import GameplayKit

class GameScene: SKScene {

private let greenDragonNode = GreenDragonSprite.newInstance()
private var lastUpdateTime : TimeInterval = 0
private var i = 0

override func sceneDidLoad() {

    self.lastUpdateTime = 0

    let xOrigin = CGFloat(arc4random()).truncatingRemainder(dividingBy: size.width)
    let yOrigin = CGFloat(arc4random()).truncatingRemainder(dividingBy: size.height)
    let dragonOrigin = CGPoint(x: xOrigin, y: yOrigin)

    greenDragonNode.position = dragonOrigin
    greenDragonNode.setScale(0.1)

    addChild(greenDragonNode)

    }

override func didMove(to view: SKView) {

    for i in 1...20 {
    let xDestination = CGFloat(arc4random()).truncatingRemainder(dividingBy: size.width)
    let yDestination = CGFloat(arc4random()).truncatingRemainder(dividingBy: size.height)
    let dragonDestination = CGPoint(x: xDestination, y: yDestination)

    let xDestination2 = CGFloat(arc4random()).truncatingRemainder(dividingBy: size.width)
    let yDestination2 = CGFloat(arc4random()).truncatingRemainder(dividingBy: size.height)
    let dragonDestination2 = CGPoint(x: xDestination2, y: yDestination2)

    let dragonNodeTravel = SKAction.move(to:dragonDestination, duration: 3.0)
    let dragonNodeReturn = SKAction.move(to: dragonDestination2, duration: 3.0)
    greenDragonNode.run(SKAction.sequence([dragonNodeTravel, dragonNodeReturn]))

//    i += 1
    let label = SKLabelNode(text: "\(i)")
    label.position  = dragonDestination2
    addChild(label)
    }
}

}

这是因为只执行for循环中的最后一个操作。您必须创建一个操作队列。因此,您应该将它们附加到数组中,并按顺序运行它们,如下所示:

import SpriteKit

import GameplayKit

class GameScene: SKScene {

    private let greenDragonNode = SKSpriteNode(color: .green, size: CGSize(width: 20, height: 20))

    func getRandomPoint(withinRect rect:CGRect)->CGPoint{

        let x = CGFloat(arc4random()).truncatingRemainder(dividingBy: rect.size.width)
        let y = CGFloat(arc4random()).truncatingRemainder(dividingBy: rect.size.height)

        return CGPoint(x: x, y: y)
    }

    override func didMove(to view: SKView) {

         greenDragonNode.position = self.getRandomPoint(withinRect: frame)

         addChild(greenDragonNode)

        var actions:[SKAction] = []

        for i in 1...20 {

            let destination = getRandomPoint(withinRect: frame)

            let move = SKAction.move(to:destination, duration: 1.0)

            actions.append(move)

            let label = SKLabelNode(text: "\(i)")
            label.position  = destination
            addChild(label)
        }

        let sequence = SKAction.sequence(actions)
        greenDragonNode.run(sequence)

    }
}

这是因为只执行for循环中的最后一个操作。您必须创建一个操作队列。因此,您应该将它们附加到数组中,并按顺序运行它们,如下所示:

import SpriteKit

import GameplayKit

class GameScene: SKScene {

    private let greenDragonNode = SKSpriteNode(color: .green, size: CGSize(width: 20, height: 20))

    func getRandomPoint(withinRect rect:CGRect)->CGPoint{

        let x = CGFloat(arc4random()).truncatingRemainder(dividingBy: rect.size.width)
        let y = CGFloat(arc4random()).truncatingRemainder(dividingBy: rect.size.height)

        return CGPoint(x: x, y: y)
    }

    override func didMove(to view: SKView) {

         greenDragonNode.position = self.getRandomPoint(withinRect: frame)

         addChild(greenDragonNode)

        var actions:[SKAction] = []

        for i in 1...20 {

            let destination = getRandomPoint(withinRect: frame)

            let move = SKAction.move(to:destination, duration: 1.0)

            actions.append(move)

            let label = SKLabelNode(text: "\(i)")
            label.position  = destination
            addChild(label)
        }

        let sequence = SKAction.sequence(actions)
        greenDragonNode.run(sequence)

    }
}

Dominick,如果这个答案帮助你解决问题,请考虑接受它。@ Dominick,如果这个答案帮助你解决问题,请考虑接受它。