-3

Referring to my last question:

Spritekit: passing from UIButtons to buttons as SKSpriteNode

I’m working on a SpriteKit game: the user can tap and swipe on the screen to move sprites inside the scene and I added gesture recognizers in my ViewController for that. Then I create a HUD to keep 4 buttons programmatically made with which the user could add other sprites to the scene. I want my buttons fade and scale a little when pressed and then turn back to the original state, but it seems that they conflict with viewController’s gesture recognizers: buttons fade and scale down, but they stay in that state, don’t go back to normal state. What can I do? This is the Button class:

import SpriteKit
protocol ButtonDelegate: NSObjectProtocol {
    func buttonClicked(sender: Button)
}
class Button: SKSpriteNode {
    weak var delegate: ButtonDelegate!
    var buttonTexture = SKTexture()
    init(name: String) {
        buttonTexture = SKTexture(imageNamed: name)
        super.init(texture: buttonTexture, color: .clear, size: buttonTexture.size())
        self.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
var touchEndedCallback: (() -> Void)?
weak var currentTouch: UITouch?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if isUserInteractionEnabled {
        setScale(0.9)
        self.alpha = 0.5
        if let currentTouch = touches.first {
            let touchLocation = currentTouch.location(in: self)
            for node in self.nodes(at: touchLocation) {
                delegate?.buttonClicked(sender: self)
            }
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    setScale(1.0)
    self.alpha = 1.0
    touchEndedCallback?()
    print("tapped!")
}
}

This is the code I used in View Controller:

class ViewController: UIViewController, UIGestureRecognizerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        let skView = view as! SKView
        skView.isMultipleTouchEnabled = false
        skView.presentScene(scene)
    @IBAction func didTap(_ sender: UITapGestureRecognizer) {
        game.myCode
    }
    @IBAction func didPan(_ sender: UIPanGestureRecognizer) {
        let currentPoint = sender.translation(in: self.view)
        if let originalPoint = panPointReference {
            if abs(currentPoint.x - originalPoint.x) > (SquareSize * 0.9) {
                if sender.velocity(in: self.view).x > CGFloat(0) {
                    //game.myCode
                    panPointReference = currentPoint
                } else {
                    //game.myCode
                    panPointReference = currentPoint
                }
            }
        } else if sender.state == .began {
            panPointReference = currentPoint
        }
    }
    @IBAction func didSwipe(_ sender: UISwipeGestureRecognizer) {
        //game.myCode
    }
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive shouldReceiveTouch: UITouch) -> Bool {
        if UITouch .isKind(of: Button.self) {
            return false
        }
        return true
    }
    func buttonClicked(sender: Button) {
        //myCode
    }
}
DaniChi
  • 301
  • 3
  • 12

2 Answers2

1

SOLVED IT!
Ok, it was simpler than I think.
(I took a break).
- I deleted UIgestures and added them programmatically;
- in my GameScene’s touchesEnded method I created a conditional to check which one of my node has been touched.
- in View Controller I add the
gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:) method, to avoid conflicts with gesture recognizers attached to different view (my nodes are in two different views):

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    if gestureRecognizer.view != otherGestureRecognizer.view {
        return false
    }
    return true
}


I hope this will help…

DaniChi
  • 301
  • 3
  • 12
0

I don't think they are interfering with the inbuilt gesture recognizers of the View Controller as you are not adding any gestures of your own, you are just overriding touchesBegin and touchesEnded.

Does "tapped" get printed out?

It is possible that touchesEnd() is not getting called, try implementing touchesCancelled() as well and see if that gets called.

Brett
  • 1,647
  • 16
  • 34
  • No, touchesEnd() is not getting called, "tapped" is not printed... but before to add gesture recognizers to GameViewController, everything worked perfectly. After I added them my buttons stopped to work. If I push a button, it fades and scale, but stays in that state, doesn’t go back to normal state. – DaniChi Oct 02 '18 at 11:23
  • I don't see any gesture recognisers in the code you posted. – Brett Oct 03 '18 at 00:42
  • Yesterday I changed and added the code: pan, tap and gesture recognizers are applied to the view, so the user can move sprites inside of it. But after I added them my buttons stopped to work. I really don't know how to solve the problem. – DaniChi Oct 03 '18 at 06:47