2017-01-24 4 views
3

ユーザーが最初に触れる隅とユーザーが持ち上げるもう一方の隅に四角形を追加します。また、ユーザーが指をドラッグしている間に矩形が表示されるようにしたいと思います。すぐにスプライトキットを使用して角の四角形をタッチします。コードは実行されますが、矩形は表示されません

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch in touches{ 
     let position1 = touch.location(in: self) 
     var x1 = position1.x 
     var y1 = position1.y 


     func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     for touch in touches{ 
     let position2 = touch.location(in: self) 
     var x2 = position2.x 
     var y2 = position2.y 

     var originX = min(x1,x2) 
     var originY = min(y1,y2) 
     var cornerX = max(x1,x2) 
     var cornerY = max(y1,y2) 
     var rect_width = cornerX - originX 
     var rect_height = cornerY - originY 

     var rect_con = CGRect(x: originX, y:originY, width: rect_width, height: rect_height) 

     var box = SKShapeNode(rect: rect_con) 
    } 
} 

答えて

2

まずアップ、あなたは長方形の後だけなら、あなたはこれで、ちょうどSKSpriteNodeでこれをやって、そしてそれをスケーリング方がいいでしょう:

https://developer.apple.com/reference/spritekit/skspritenode/1645445-scale

第二に、ためにSKShapeNodeは、CAShapeLayerをVERYプリミティブ描画ツールに半分(またはそれ以下)完全に変換したものです。それは、ダイナミックな描画では、まったくうまくいきません。 touchesMovedのxおよびyにアンカータッチポイントでSKSpriteNode長方形のポイント、およびスケールを設定


。負の値は、デカルト座標系の原点から操作されているため、少し変換する必要があります。

0

あなたのtouchesEnded関数はtouchesStart関数内から宣言されており、そのためにクラススコープからは見えないので、呼び出されることはありません。このようになります(テストされていません)。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
for touch in touches{ 
    let position1 = touch.location(in: self) 
    var x1 = position1.x 
    var y1 = position1.y 
    } 
} 

func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch in touches{ 
    let position2 = touch.location(in: self) 
    var x2 = position2.x 
    var y2 = position2.y 

    var originX = min(x1,x2) 
    var originY = min(y1,y2) 
    var cornerX = max(x1,x2) 
    var cornerY = max(y1,y2) 
    var rect_width = cornerX - originX 
    var rect_height = cornerY - originY 

    var rect_con = CGRect(x: originX, y:originY, width: rect_width, height: rect_height) 

    var box = SKShapeNode(rect: rect_con) 
} 
} 
関連する問題