2017-06-23 13 views
0

スプライト(四角形)をランダムに移動しようとしましたが、スプライトはすべて同じ位置に移動し、振動します。 スプライトを永久にランダムな位置に移動する最も良い方法は何ですか?画面上のランダムな位置で永久にスプライトを移動する

//Make random shape 
func makeShape() { 

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

     sprite = shape.copy() as! SKShapeNode 
     sprite.name = "Shpae \(shapesamount)" 

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

override func update(_ currentTime: TimeInterval) { 

    // Called before each frame is rendered 
    if istouching && !isOver { 
     let dt:CGFloat = 1.0/50.0 
     let distance = CGVector(dx: touchpoint.x - circle.position.x, dy: touchpoint.y - circle.position.y) 
     let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt) 
     self.circle.physicsBody!.velocity = velocity 
    } 

    if isOver == false { 
     for nodee in shapes.children { 
      moveRandom(node: nodee as! SKShapeNode) 
     } 
    } 
} 

}

形状は、それが振動している理由ですSKNode

答えて

0

あなたは常にあなたのノードに移動アクションを追加している、です。文字通り、フレームごとにすべての方向に動きます。

if isOver == false { 
     for nodee in shapes.children { 
      guard !nodee.hasActions else {continue} 
      moveRandom(node: nodee as! SKShapeNode) 
     } 
    } 

代わり

、任意のアクションが実行されている場合は、位置を変更しない場合は、確認するためにチェックを追加

関連する問題