2017-01-30 9 views
1

私はSwiftのSKNodesについて学び、問題に遭遇しました。アプリケーションは、画面に触れて、ノードがタッチされた位置に表示されるように、非常に単純なものになっていました。私は理由は分かりませんが、長方形は画面上の別の場所に表示され続けますが、sn.positiontouch.location(in: view)と同じです。どうしましたか?SKShpapeNodeの位置とタッチの位置の差

import SpriteKit 
import GameplayKit 

class GameScene: SKScene { 

    private var spinnyNode : SKShapeNode? 
    private var touchPoint : CGPoint! 

    override func didMove(to view: SKView) { 

     let size = CGSize(width: 50, height: 50) 

     spinnyNode = SKShapeNode(rectOf: size, cornerRadius: CGFloat(0.6)) //init 

     if let spinnyNode = self.spinnyNode{ 

      spinnyNode.lineWidth = 3 

      let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(M_PI), duration: 1)) 
      let fade = SKAction.fadeOut(withDuration: 0.5) 
      let remove = SKAction.removeFromParent() 

      spinnyNode.run(rotate) 
      spinnyNode.run(SKAction.sequence([fade, remove])) 

     } 
    } 

    func touchDown(point pos : CGPoint){ 
     if let sn = self.spinnyNode?.copy() as! SKShapeNode? { 

      sn.position = CGPoint(x: touchPoint.x , y: touchPoint.y) 
      print("touchDown x:\(touchPoint.x), y:\(touchPoint.y)") 

      self.addChild(sn) 
     } 
    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     guard touches.count == 1 else{ 
      return 
     } 
     if let touch = touches.first{ 
      touchPoint = touch.location(in: view) 

      touchDown(point: touchPoint) 
      print("touchesBegan -> x: \(touchPoint.x) y: \(touchPoint.y)") 
     } 
    } 

答えて

0

この

import SpriteKit 
import GameplayKit 

class GameScene: SKScene { 

//use whichever image for your node 
var ballNode = SKSpriteNode(imageNamed: "ball") 

var fingerLocation = CGPoint() 

override func didMove(to view: SKView) { 

    ballNode.size = CGSize(width: 100, height: 100) 
    ballNode.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.5) 

} 


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


    if let location = touches.first?.location(in: self){ 

     if let copy = ballNode.copy() as? SKSpriteNode { 
      copy.position = location 
      addChild(copy) 
     } 
    } 
} 

} 

私はランダムなスポットをクリックするたびのようなものをやってみ、ボールが表示されます。お役に立てれば。

+0

私はこれを試してみると、私の触れた場所とskspritenodeの位置は別の場所に設定されています。例えば。タッチポイントが(10、-20)のとき、skスプライトは異なる位置に配置されます。そのskスプライトノードをクリックするとその位置が異なります。どのようにそれをふるいにかける。私の必要性は、私がsigleの位置skspriteノードをクリックすると、その点を見つけることです – Araf