Using 3D touch in Sprite Kit to measure pressure

442 views Asked by At

In my main app I'm trying to use 3D touch to activate a bullet fireing, but the results seem to be very inconsistant and it didn't always give my the write results. Also my iPhone 6S was crashing when I used this code, This is the code I used for a test.

 for touch in touches {
        let location = touch.locationInNode(self)

        var force =  touch.force * 10
        var maxForce = touch.maximumPossibleForce

        print ("The force is")
        print (maxForce*4)
        print (force*10000000)

        if force != 0{


            myLabel.text = "Force"
            myLabel.fontSize = 45
            myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
            self.addChild(myLabel)
            myLabel.zPosition = 100000


        }

        else {

           myLabel.removeFromParent()
        }
1

There are 1 answers

0
Knight0fDragon On

I am a going to guess it is crashing because you add label to the parent multiple times (Your if condition is broken, no matter what function you call, (touchesBegan,touchesMoved,touchesEnded) only one of those conditions will always be true, in touchesBegan and touchesMoved, your force will always be > 0, in your touchEnded, your force should be 0. (I am going to assume this code is your touchMoved) Instead, add your label in the your viewDidLoad code, and use the .hidden field to show/hide your label on your moves. (Make sure on touchesEnded to hide it)