Reputation: 11
This is how I made my nodes to move when you tap on the left and the right side of the screen but how can I make them separate when one finger in on the left side and the other on the right side ?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let location = (touches.first)?.location(in: self.view)
if (location?.x)! < (self.view?.bounds.size.width)!/2 {
player1.run(SKAction.moveTo(x: 0 + player1.size.width / 2, duration: 0.1))
player2.run(SKAction.moveTo(x: 0 + player1.size.width + player2.size.width / 2, duration: 0.1))
} else {
player1.run(SKAction.moveTo(x: self.frame.width - player1.size.width - player2.size.width / 2, duration: 0.1))
player2.run(SKAction.moveTo(x: self.frame.width - player1.size.width / 2, duration: 0.1))
}
}
Upvotes: 0
Views: 305
Reputation: 35412
To enable the multiple touch on your game , you should use : view.isMultipleTouchEnabled = true
. Here you can find the API references
Then, if you present your scene from a SKS file, usually the anchorPoint
setted is (0.5,0.5)
so to work with my example below be sure to add this line in your GameViewController
:
scene.anchorPoint = CGPoint.zero
before the line view.presentScene(scene)
to have your anchorPoint
setted to 0 for your current scene.
You should also think to "protect" your action from multiple calls especially when you launch these actions from the touch event delegate methods. This can be possible with action(forKey:
where you check if an action is running or not.
I have made a more detailed example to show you how it can be possible to obtain the multitouch for your actions in Sprite-kit:
import SpriteKit
class GameScene: SKScene {
private var player1 : SKSpriteNode!
private var player2 : SKSpriteNode!
override func didMove(to view: SKView) {
self.player1 = SKSpriteNode.init(color: .red, size: CGSize(width:50,height:50))
self.player2 = SKSpriteNode.init(color: .yellow, size: CGSize(width:50,height:50))
addChild(player1)
addChild(player2)
self.player1.position=CGPoint(x:self.frame.width/2,y:(self.frame.height/2)+100)
self.player2.position=CGPoint(x:self.frame.width/2,y:(self.frame.height/2)-100)
if let view = self.view {
view.isMultipleTouchEnabled = true
}
}
func rightActions() {
if (player1.action(forKey: "right") == nil) {
player1.run(SKAction.moveTo(x: 0 + player1.size.width / 2, duration: 0.1),withKey:"right")
}
if (player2.action(forKey: "right") == nil) {
player2.run(SKAction.moveTo(x: 0 + player1.size.width + player2.size.width / 2, duration: 0.1),withKey:"right")
}
}
func leftActions() {
if (player1.action(forKey: "left") == nil) {
player1.run(SKAction.moveTo(x: self.frame.width - player1.size.width - player2.size.width / 2, duration:0.1) ,withKey:"left")
}
if (player2.action(forKey: "left") == nil) {
player2.run(SKAction.moveTo(x: self.frame.width - player1.size.width / 2, duration: 0.1),withKey:"left")
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let touch = t as UITouch
let location = touch.location(in: self)
let touchedNode = self.atPoint(location)
let tapCount = touch.tapCount
print("tapCount: \(tapCount) - touchedNode:\(touchedNode) - location:\(location)")
location.x < (self.size.width/2) ? rightActions() : leftActions()
}
}
}
Upvotes: 2