2016-05-29 4 views
0

私は迅速なプロジェクトで次のスプライトキットシーンを持っています。 SKNodeを画面全体に移動します。問題は、円を上向きと下向きの両方に移動させたいが、下向きまたは横向きにしか移動しないことです。 yMoveの値をチェックして、y軸にランダムに正の変化が生じていることを確認しました。これらの正の値を生成していますが、オブジェクトはまだ水平方向と垂直方向にのみ移動します。スプライトキットオブジェクトを上に移動するオブジェクトを取得する

class GameScene: SKScene { 


override func didMoveToView(view: SKView) { 
    backgroundColor = SKColor.whiteColor() 

    runAction(SKAction.repeatActionForever(
     SKAction.sequence([ 
      SKAction.runBlock(addKugel), 
      SKAction.waitForDuration(1.0) 
      ]) 
     )) 


} 

func addKugel() { 

    // Create sprite 
    let kugel = SKShapeNode(circleOfRadius: 100) 
    kugel.strokeColor = SKColor.blackColor() 
    kugel.glowWidth = 1.0 

    // Determine where to spawn the monster along the Y axis 
    let actualY = random(min: 200/2, max: size.height - 200/2) 

    // Position the monster slightly off-screen along the right edge, 
    // and along a random position along the Y axis as calculated above 
    kugel.position = CGPoint(x: size.width + 200/2, y: actualY) 

    // Add the monster to the scene 
    addChild(kugel) 

    // Determine speed of the monster 
    let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0)) 

    //randomly create coefficients for movement 
    let xVector = random(min: CGFloat(0.0), max: CGFloat(1.0)) 
    var yVector = random(min: CGFloat(0.0), max: CGFloat(1.0)) 

    //overly complicated way to make a number negative 50 percent of the time 
    var probNegative = random(min: CGFloat(0.0), max: CGFloat(1.0)) 
    if (probNegative > 0.5){ 
     yVector = 0.0 - yVector 
    } 
    debugPrint(yVector) 
    // Create the actions 
    let yMove = (200/2)*yVector 
    debugPrint(yMove) 
    let actionMove = SKAction.moveTo(CGPoint(x: -200/2*xVector, y: yMove), duration: NSTimeInterval(actualDuration)) 
    let actionMoveDone = SKAction.removeFromParent() 
    kugel.runAction(SKAction.sequence([actionMove, actionMoveDone])) 

} 
} 

答えて

0

だから私は非常に間違っていたmoveTo何をしていた。すべてmoveToは、スプライトを特定のCGPointに移動します。 0size.heightの間の値をランダムに選択するのではなく、私のスプライトサイズでy座標をスケーリングしていました。 上記のスニペットの簡略化されたバージョンです:

func addKugel() { 
    let KUGELWIDTH = 50.0 

    let kugel = SKShapeNode(circleOfRadius: CGFloat(KUGELWIDTH)/2) 
    kugel.strokeColor = SKColor.blackColor() 
    kugel.glowWidth = 1.0 

    let actualY = random(min: CGFloat(KUGELWIDTH)/2, max: (size.height - CGFloat(KUGELWIDTH)/2)) 
    debugPrint("actualY: \(actualY)") 

    kugel.position = CGPoint(x: size.width + CGFloat(KUGELWIDTH/2), y: actualY) 

    addChild(kugel) 

    let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0)) 

    let y = random(min: CGFloat(0.0), max: CGFloat(1.0)) 
    let yPos = CGFloat(y)*size.height 

    let actionMove = SKAction.moveTo(CGPoint(x: 0.00, y: yPos), duration: NSTimeInterval(actualDuration)) 
    let actionMoveDone = SKAction.removeFromParent() 
    kugel.runAction(SKAction.sequence([actionMove, actionMoveDone])) 

} 
関連する問題