Reputation: 119
I'm running a very simple code and I'm trying to detect when a long press has ended and then execute a function but I can't seem to figure out how to do so. I have looked at lots of resources online but I haven't been able to figure it out. I'm very new to coding in Swift so if the answer is simple I apologize in advance. Here's all the code:
import SpriteKit
class GameScene: SKScene {
let char = SKSpriteNode(imageNamed: "flying character")
override func didMove(to view: SKView) {
setScene()
}
@objc func press() {
physicsWorld.gravity = CGVector(dx: 0.1, dy: 1.5)
if UILongPressGestureRecognizer.State.changed == .ended {
print ("the gesture has ended")
}
}
func setScene() {
backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 221/255, alpha: 1.0)
char.size = CGSize(width: frame.size.width/5, height: frame.size.width/5)
char.run(SKAction.rotate(byAngle: .pi*11/6, duration: 0.00001))
char.position = CGPoint(x: frame.midX - 100, y: frame.midY + 150)
addChild(char)
char.physicsBody = SKPhysicsBody(circleOfRadius: char.size.width/2)
char.physicsBody?.categoryBitMask = PhysicsCategories.charCategory
physicsWorld.gravity = CGVector(dx: 0.1, dy: -1.5)
char.physicsBody?.affectedByGravity = true
view?.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(press)))
}
}
as you can see I'm just trying to execute the print command when the long press has ended.
Upvotes: 0
Views: 79
Reputation: 46
First of all, a very important thing you might notice is that your problem is not related to SpriteKit itself! You are using a UILongPressGestureRecognizer, a class from UIKit! Apple Docs
Also, note that you are applying the gesture to the whole view. As I assume that this is a game with a character that flies, I imagine that this behaviour is the intended one.
I imagine that the problem you are facing is that you are not working with the UILongPressGestureRecognizer assigned to your view! Instead, you are accessing UILongPressGestureRecognizer class properties/methods. For more details you can browse this section of the Swift documentation
A quick solution to you problem is to change your press function from
@objc func press() {
physicsWorld.gravity = CGVector(dx: 0.1, dy: 1.5)
if UILongPressGestureRecognizer.State.changed == .ended {
print ("the gesture has ended")
}
}
to
@objc func press(_ gestureRecognizer: UILongPressGestureRecognizer) {
physicsWorld.gravity = CGVector(dx: 0.1, dy: 1.5)
if gestureRecognizer.state == .ended {
print ("the gesture has ended")
}
}
and your setScene function from
view?.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(press)))
to
view?.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(press:)))
SpriteKit handles touch events in a quite raw way, but it might give you a bit more control on how you want to respond to these events. As KnightOfDragon said, it's quite like reinventing the wheel.
If you want to take a look on how SpriteKit does it, see this example. It's quite short, but I hope it helps :)
Upvotes: 3