2017-01-07 9 views
0

これまでのところ、私のアプリは中央に大きなボールを、中央に小さなボールを持っています。私は画面上の任意の場所をタップすることができたいと思いますし、小さなボールはその方向に撃つ。私は人々がベクトルの作成について言うと聞いてきましたが、私はこれらを迅速に動かすことができないようです。私は初心者ですので、愚かな質問については申し訳ありません!ここでタップの方向に弾丸を発射する

は私のコードです:

var mainBall = SKSpriteNode(imageNamed: "Ball") 

override func didMove(to view: SKView) { 


    mainBall.size = CGSize(width: 300, height: 300) 
    mainBall.position = CGPoint(x: frame.width/2, y: frame.height/2) 

    self.addChild(mainBall) 

} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

    if let touch = touches.first { 
     let position = touch.location(in: self) 
     print(position.x) 
     print(position.y) 
    } 

    for touch in (touches) { 
     touch.location(in: self) 

     let smallBall = SKSpriteNode(imageNamed: "Ball") 
     smallBall.position = mainBall.position 
     smallBall.size = CGSize(width: 100, height: 100) 
     smallBall.physicsBody = SKPhysicsBody(circleOfRadius: smallBall.size.width/2) 
     smallBall.physicsBody?.affectedByGravity = false 

     self.addChild(smallBall) 

    } 
} 
+0

あなたがしたい場合があります(HTTPS [チュートリアルの顔をしています]。 raywenderlich.com/145318/spritekit-swift-3-tutorial-beginners)。 – shallowThought

答えて

0

あなたはSKSprideNode Sをアニメーション化するアクションを使用することができます:// WWW:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    guard let touch = touches.first else { 
     return   
    } 

    let newPosition = touch.location(in: self) 
    ... 
    //assuming self.smallBall exists and is visible already: 
    [self.smallBall runAction:[SKAction moveTo:newPosition duration:1.0]]; 
} 
+0

私はあなたのコードを追加しましたが、それは2に頼まれましたが、今私はこのエラーを受け取ります... [リンク](http://prntscr.com/dspjq5)。ごめんなさい!何をしているかが自分もわからない! –

+0

申し訳ありません。私は誤ってObj-C構文に切り替えました。更新された答え。 – shallowThought

関連する問題