2017-03-04 6 views
1

私は、ドラゴンがランダムに動き回って、ヒーローがそれを避けなければならないゲームを作ろうとしていました。私はドラゴンをランダムな場所に出現させて1〜2回動かせるが、それはポイントからポイントまで連続的に移動させ、さらに移動させると問題が発生する。私は、乱数のすべてを生成する前にアクションが完了するのを待っているわけではないからかもしれないと思う。私は、forループからの唯一の最後のアクションが実行されるために起こっているラベルは、少なくとも20までの乱数を生成していることを自分自身に証明するために含めて次のコード...スウィフト3を使用してスプライトをランダムに移動する

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) 
    } 
} 

} 

答えて

0

を試してみました。アクションのキューを作成する必要があります。配列に配列を追加して、次のようなシーケンスで実行する必要があります。

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) 

    } 
} 
+0

これは、私が欲しかったとおりに動作します。ありがとう! – Dominick

+0

@Dominickこの回答が問題の解決に役立つ場合は、それを受け入れることを検討してください。 – Whirlwind

関連する問題