简体   繁体   中英

How can I detect swipes on Spritekit Nodes?

So I am currently working on a 2D endless runner written in Swift3 and using Spritekit for my nodes and scenes. I recently implemented some code to detect swipes in general, they will be below. So my question is: how can I detect a swipe action and check the direction of that swipe on a Spritekit Node? I realize that this has been asked before, but I cannot find a working solution since everything I come across seems to be for Swift and Swift2, not Swift3.

Here's my swipe detection code:

override func viewDidLoad() {
  super.viewDidLoad()
  let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.left
    self.view.addGestureRecognizer(swipeLeft)

    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right
    self.view.addGestureRecognizer(swipeRight)

    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeUp.direction = UISwipeGestureRecognizerDirection.up
    self.view.addGestureRecognizer(swipeUp)

    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeDown.direction = UISwipeGestureRecognizerDirection.down
    self.view.addGestureRecognizer(swipeDown)
}

I have tested these with a function:

func respondToSwipeGesture(gesture: UIGestureRecognizer) {
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.right:
                print("Swiped right")
            case UISwipeGestureRecognizerDirection.down:
                print("Swiped down")
            case UISwipeGestureRecognizerDirection.left:
                print("Swiped left")
            case UISwipeGestureRecognizerDirection.up:
                print("Swiped up")
            default:
                break
            }
        }
}

The swipes seem to work all right, as the print statements have been displaying the correct outputs in response to my swipes.

I have also added a delegate to avoid possible SIGABRT errors:

class GameViewController: UIViewController, UIGestureRecognizerDelegate

Please let me know if you'd like more information!

Edit: Added code where I initialized my puzzle pieces

//The class
class ChoosePiecesClass: SKSpriteNode{
    func moveWithCamera() {
        self.position.x += 5;
}


//initialize a piece
private var RandomPiece1: ChoosePiecesClass?;


override func didMove(to view: SKView) {
    RandomPiece1 = childNode(withName: "RandomPiece1") as? ChoosePiecesClass;
    RandomPiece1?.position.x = 195;
    RandomPiece1?.position.y = -251;
}

override func update(_ currentTime: TimeInterval) {
    RandomPiece1?.moveWithCamera();
}

Edited to provide the error statement from console, upon clicking Start Game and triggering the fatalError code, causing the game to crash.

fatal error: init(coder:) has not been implemented: file /Users/ardemis/Documents/PuzzleRunner/PuzzleRunner 3 V3/PuzzleRunner/ChoosePieces.swift, line 33

It also displays the following

EXEC_BAT_INSTRUCTION(code = EXEC_1386_INVOP, subcode = 0x0)

on the same line as the fatalError declaration.

I wouldn't use the UIGestures if I was trying to track specific SpriteNodes. You can easily track your nodes in TouchesBegan and figure out which way a swipe direction occurs. This example has three sprites on the screen on will print/log whatever direction one of them gets swiped and will ignore all other swipes.

EDIT > I just created a subclass for my Box object (sorry I didn't use your naming, but it has the same functions). There are probably many ways of doing this, I chose to use create a protocol on the subclass and make the scene conform to the protocol. I moved all of the touch/swipe functionality into the sub class, and when it is done detecting the swipe it just calls the delegate and says which object has been swiped.

protocol BoxDelegate: NSObjectProtocol {
    func boxSwiped(box: Box)
}

class Box: SKSpriteNode {

    weak var boxDelegate: BoxDelegate!
    private var moveAmtX: CGFloat = 0
    private var moveAmtY: CGFloat = 0
    private let minimum_detect_distance: CGFloat = 100
    private var initialPosition: CGPoint = CGPoint.zero
    private var initialTouch: CGPoint = CGPoint.zero
    private var resettingSlider = false

    override init(texture: SKTexture?, color: UIColor, size: CGSize) {

        super.init(texture: texture, color: color, size: size)

        self.isUserInteractionEnabled = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func moveWithCamera() {
        self.position.x += 5
    }

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

        if let touch = touches.first as UITouch! {

            initialTouch = touch.location(in: self.scene!.view)
            moveAmtY = 0
            moveAmtX = 0
            initialPosition = self.position
        }
    }

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

        if let touch = touches.first as UITouch! {

            let movingPoint: CGPoint = touch.location(in: self.scene!.view)

            moveAmtX = movingPoint.x - initialTouch.x
            moveAmtY = movingPoint.y - initialTouch.y
        }
    }

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

        var direction = ""
        if fabs(moveAmtX) > minimum_detect_distance {

            //must be moving side to side
            if moveAmtX < 0 {
                direction = "left"
            }
            else {
                direction = "right"
            }
        }
        else if fabs(moveAmtY) > minimum_detect_distance {

            //must be moving up and down
            if moveAmtY < 0 {
                direction = "up"
            }
            else {
                direction = "down"
            }
        }

        print("object \(self.name!) swiped " + direction)

        self.boxDelegate.boxSwiped(box: self)
    }
}

In GameScene.sks

Make sure to make GameScene conform to BoxDelegate by adding the protocol after the declaration

class GameScene: SKScene, BoxDelegate {

    var box = Box()
    var box2 = Box()
    var box3 = Box()

    override func didMove(to view: SKView) {

        box = Box(color: .white, size: CGSize(width: 200, height: 200))
        box.zPosition = 1
        box.position = CGPoint(x: 0 - 900, y: 0)
        box.name = "white box"
        box.boxDelegate = self
        addChild(box)

        box2 = Box(color: .blue, size: CGSize(width: 200, height: 200))
        box2.zPosition = 1
        box2.name = "blue box"
        box2.position = CGPoint(x: 0 - 600, y: 0)
        box2.boxDelegate = self
        addChild(box2)

        box3 = Box(color: .red, size: CGSize(width: 200, height: 200))
        box3.zPosition = 1
        box3.name = "red box"
        box3.position = CGPoint(x: -300, y: 0)
        box3.boxDelegate = self
        addChild(box3)
    }

    func boxSwiped(box: Box) {
        currentObject = box
        print("currentObject \(currentObject)")
    }

    override func update(_ currentTime: TimeInterval) {
        box.moveWithCamera()
        box2.moveWithCamera()
        box3.moveWithCamera()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM