Well it was a combination little hacks in touchesBegan/ touchesMoved and update func,First you need to catch on which touch occurred, get it's name (in my case I made nodes which had alpha of 0, but become visible upon moving over them i.e alpha 1). In touchesBegan, touchesMoved as follow guard let touch = touches.first else {return}
let location = touch.location(in: self)
lastTouchPoint = location
let positionInScene = touch.location(in: self)
let touchedNode = self.atPoint(positionInScene)
if let name = touchedNode.name
{
if name == "vortex"
{
touching = false
self.view!.isUserInteractionEnabled = false
print("Touched on the interacted node")
}else{
self.view!.isUserInteractionEnabled = true
touching = true
}
}
}Second use a BOOL touching to track user interactions, on the screen by using getting a tap recogniser setup, as follow,
func setupTapDetection() {
let t = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
view?.addGestureRecognizer(t)
}
@objc func tapped(_ tap: UITapGestureRecognizer) {
touching = true
}Finally in update put checks as follow,guard isGameOver == false else { return }
self.view!.isUserInteractionEnabled = true
if(touching ?? true){
if let lastTouchPosition = lastTouchPoint {
//this usually gives a large value (related to screen size of the device) so /100 to normalize it
let diff = CGPoint(x: lastTouchPosition.x - player.position.x, y: lastTouchPosition.y - player.position.y)
physicsWorld.gravity = CGVector(dx: diff.x/100, dy: diff.y/100)
}
}
}
Post
Replies
Boosts
Views
Activity
Set the contactTestBitMask properties of both objects. then detect when they collide in the didBegin func and then detect when they stop colliding in the didEnd funcplayer.physicsBody?.contactTestBitMask = collisionTypes.bullet.rawValue
bullet.physicsBody?.contactTestBitMask = collisionTypes.player.rawValue
func didBegin(_ contact: SKPhysicsContact) {
//detect your collision and do what you need to do
//maybe set a bool to true if you have multiple collision types
}
func didEnd(_ contact: SKPhysicsContact) {
//check that the above collison is the one that ended
//run appropriate code for end collision
}
let me clarify,node 1 is static and node 2 is dynamic, I want to know when node 2 is completely out of node 1's boundaries.