3

したがって、私はSwiftおよびiOS開発には非常に慣れていませんが、プログラミングにまったく新しいわけではなく、いくつかのチュートリアルを行ったばかりですが、基本的に私のコードは次のようになります:

import SpriteKit


class GameScene: SKScene {


override func didMoveToView(view: SKView) {
    let circle = SKShapeNode(circleOfRadius: 30.0)
    circle.position = CGPointMake(100, 200)
    addChild(circle)
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    for touch in touches {
        let location = touch.locationInNode(self)
        let touchedNode = nodeAtPoint(location)
        touchedNode.position = location;
    }
}

}

これをビルドすると、ドラッグすると円が認識されて移動しますが、約 30 ピクセルしかないため、もう一度タッチして移動する必要があります。ここで何が間違っていますか?

4

2 に答える 2

3

私は間違っているかもしれませんが、あなたの指はノードの制限を離れてバックグラウンドに入ると思います。私はそれを次のように設定します:

// First make the shape available throughout the code and make sure you have a place to save the touch event for later use.
var circle: SKShapeNode!
var circleTouch: UITouch?

// Create the Shape
override func didMoveToView(view: SKView) {
    circle = SKShapeNode(circleOfRadius: 30.0)
    circle.position = CGPointMake(100, 200)
    addChild(circle)
}

// When a finger is placed on the screen, check if it's in the circle, if it is, keep that touch even in memory so we can reference it later because other fingers could touch other things in the scene.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch in touches {
        if nodeAtPoint(touch.locationInNode(self)) == circle {
            circleTouch = touch as? UITouch
        }
    }
}

// Check if the touch event that is moving is the one that anchored the circle to our finger, if yes, move the circle to the position of the finger.
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    for touch in touches {
        if circleTouch != nil {
            if touch as UITouch == circleTouch! {
                let location = touch.locationInNode(self)
                circle.position = location
            }
        }
    }
}

// Clean up the touch event when the finger anchoring the circle is raised from the screen.
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    for touch in touches {
        if circleTouch != nil {
            if touch as UITouch == circleTouch! {
                circleTouch = nil
            }
        }
    }
}

if letそこにステートメントを使用することもできますがnil、わかりやすくするために代わりにチェックしました。

于 2015-01-27T18:43:57.370 に答える
1

とくりくさん、どうもありがとうございます。私たちは完全には正しくありませんが、最終的な答えにたどり着きました。

import SpriteKit

class GameScene: SKScene {
var circleTouch: UITouch?

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    let circle = SKShapeNode(circleOfRadius: 40.0)
    circle.fillColor = UIColor.blackColor()
    circle.position = CGPoint(x: size.width * 0.5, y: size.height * 0.2)
    circle.name = "userCircle"
    addChild(circle)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch in touches {
        if nodeAtPoint(touch.locationInNode(self)).name == "userCircle" {
            circleTouch = touch as? UITouch
        }
    }
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    for touch in touches {
        if circleTouch != nil {
            if touch as UITouch == circleTouch! {
                let location = touch.locationInNode(self)
                let touchedNode = nodeAtPoint(location)
                touchedNode.position = location
            }
        }
    }
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    for touch in touches {
        if circleTouch != nil {
            if touch as UITouch == circleTouch! {
                circleTouch = nil
            }
        }
    }
}
于 2015-01-28T05:27:08.620 に答える