Contact Delegate missing - swift

I am trying to make a new level for my app. I am honestly just copy and pasting my code from the view controllers and gamescenes to other files that I changed the name of. Yes, I did change the names INSIDE the files too so It'll match the new level. However, my contact delegate won't work on my new level now. How can I fix this?
Cannot assign value of type 'Level1' to type 'SKPhysicsContactDelegate?'
What am I doing wrong? I just checked my main file that I had before creating a new level and it now has errors. I have't changed any of the coding.
import SpriteKit
import GameplayKit
class Level1: SKScene {
var ball = SKSpriteNode()
var danger1 = SKSpriteNode()
var danger2 = SKSpriteNode()
var goal = SKSpriteNode()
over
ride func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
ball = self.childNode(withName: "ball") as! SKSpriteNode
danger1 = self.childNode(withName: "danger1") as! SKSpriteNode
danger2 = self.childNode(withName: "danger2") as! SKSpriteNode
goal = self.childNode(withName: "goal") as! SKSpriteNode
let border = SKPhysicsBody(edgeLoopFrom: self.frame)
border.friction = 0
border.restitution = 0
danger1.physicsBody = SKPhysicsBody(rectangleOf: danger1.size)
danger1.physicsBody?.categoryBitMask = PhysicsCategories.dangerCategory
danger1.physicsBody?.isDynamic = false
danger2.physicsBody = SKPhysicsBody(rectangleOf: danger2.size)
danger2.physicsBody?.categoryBitMask = PhysicsCategories.dangerCategory
danger2.physicsBody?.isDynamic = false
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
ball.physicsBody?.categoryBitMask = PhysicsCategories.ballCategory
ball.physicsBody?.contactTestBitMask = PhysicsCategories.dangerCategory | PhysicsCategories.goalCategory
ball.physicsBody?.collisionBitMask = PhysicsCategories.none
ball.physicsBody?.isDynamic = true
ball.physicsBody!.affectedByGravity = false
goal.physicsBody = SKPhysicsBody(rectangleOf: goal.size)
goal.physicsBody?.categoryBitMask = PhysicsCategories.goalCategory
goal.physicsBody?.isDynamic = false
setupPhysics()
startGame()
}
func setupPhysics() {
physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
physicsWorld.contactDelegate = self
}
func startGame() {
ball.position = CGPoint(x: 0, y: 550)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
ball.position.x = location.x
ball.position.y = location.y
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
extension GameScene: SKPhysicsContactDelegate {
func didBegin(_ contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if contactMask == PhysicsCategories.ballCategory | PhysicsCategories.dangerCategory {
print("Contact")
} else if contactMask == PhysicsCategories.ballCategory | PhysicsCategories.goalCategory {
print("goal contact")
}
}
}

Instead of creating extension of GameScene, create extension of Level1
extension Level1: SKPhysicsContactDelegate

Related

Cannot detect collisions in spritekit

I am trying to build a game and my physics body contacts are not being detected. Can you pls help me? I have set the physicsWorldDelegate and categorybitmasks but cannot figure out what is wrong. The playground couldn't detect the collisions between the enemy and my avatar.
import SpriteKit
public class GameScene: SKScene, SKPhysicsContactDelegate {
let coronaSpeed: CGFloat = 80.0
let playerspeed: CGFloat = 155.0
var mask: SKSpriteNode?
var player: SKSpriteNode?
var mcorona: [SKSpriteNode] = []
var lastTouch: CGPoint? = nil
override public func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
// Animations
player = childNode(withName: "player") as? SKSpriteNode
mask = childNode(withName: "mask") as? SKSpriteNode
mask!.run(SKAction.repeatForever(
SKAction.sequence([
SKAction.moveBy(x: 0, y: 10, duration: 0.45),
SKAction.moveBy(x: 0, y: -10, duration: 0.45)
]
)))
for child in self.children {
if child.name == "corona" {
if let child = child as? SKSpriteNode {
mcorona.append(child)
}
}
}
// </> Animations
}
override public func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) { handleTouches(touches)
updatePlayer()
updateZombies()
}
override public func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) { handleTouches(touches)
}
override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { handleTouches(touches) }
fileprivate func handleTouches(_ touches: Set<UITouch>) { lastTouch = touches.first?.location(in: self) }
override public func didSimulatePhysics() {
if player != nil {
updatePlayer()
updateZombies()
}
}
fileprivate func shouldMove(currentPosition: CGPoint,
touchPosition: CGPoint) -> Bool {
guard let player = player else { return false }
return abs(currentPosition.x - touchPosition.x) > player.frame.width / 2 ||
abs(currentPosition.y - touchPosition.y) > player.frame.height / 2
}
fileprivate func updatePlayer() {
guard let player = player,
let touch = lastTouch
else { return }
let currentPosition = player.position
if shouldMove(currentPosition: currentPosition,
touchPosition: touch) {
updatePosition(for: player, to: touch, speed: playerspeed)
} else {
player.physicsBody?.isResting = true
}
}
func updateZombies() {
guard let player = player else { return }
let targetPosition = player.position
for corona in mcorona {
updatePosition(for: corona, to: targetPosition, speed: coronaSpeed)
}
}
func getDuration(pointA:CGPoint,pointB:CGPoint,speed:CGFloat)->TimeInterval{
let xDist = (pointB.x - pointA.x)
let yDist = (pointB.y - pointA.y)
let distance = sqrt((xDist * xDist) + (yDist * yDist));
let duration : TimeInterval = TimeInterval(distance/speed)
return duration
}
fileprivate func updatePosition(for sprite: SKSpriteNode, to target: CGPoint, speed: CGFloat) {
let currentPosition = sprite.position
let angle = CGFloat.pi + atan2(currentPosition.y - target.y, currentPosition.x - target.x)
let rotateAction = SKAction.rotate(toAngle: angle + (CGFloat.pi*0.5), duration: 0)
sprite.run(rotateAction)
//sprite.physicsBody?.isDynamic = true
let velocityX = speed * cos(angle)
let velocityY = speed * sin(angle)
let newVelocity = CGVector(dx: velocityX, dy: velocityY)
sprite.physicsBody?.velocity = newVelocity
sprite.physicsBody?.affectedByGravity = false
let moveToTouch = SKAction.move(to: CGPoint(x: target.x, y: target.y),duration: getDuration(pointA:currentPosition,pointB:target,speed:speed))
sprite.run(moveToTouch)
}
public func didBegin(_ contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// Check contact
if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask &&
secondBody.categoryBitMask == mcorona[0].physicsBody?.categoryBitMask {
// Player & corona
gameOver(false)
} else if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask &&
secondBody.categoryBitMask == mask?.physicsBody?.categoryBitMask {
// Player & mask
gameOver(true)
}
}
fileprivate func gameOver(_ didWin: Bool) {
let resultScene = MenuScene(size: size, didWin: didWin, levelToSend: 2)
let transition = SKTransition.flipVertical(withDuration: 1.0)
view?.presentScene(resultScene, transition: transition)
}
}
Here is a GIF to my problem-
https://giphy.com/gifs/U7nmXZgVxNcCuvJc7j

Sprite kit contact problems

I am making a game that requires you to dodge falling objects. I have tried to make it it so that when the falling object hits the player certain code runs. But it is not working please show me what is wrong with my code.
import SpriteKit
struct physicsCatagory {
static let person : UInt32 = 0x1 << 1
static let Ice : UInt32 = 0x1 << 2
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var person = SKSpriteNode(imageNamed: "Person")
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
person.position = CGPointMake(self.size.width/2, self.size.height/12)
person.setScale(0.4)
person.physicsBody = SKPhysicsBody(rectangleOfSize: person.size)
person.physicsBody?.affectedByGravity = false
person.physicsBody?.categoryBitMask = physicsCatagory.person
person.physicsBody?.contactTestBitMask = physicsCatagory.Ice
person.physicsBody?.dynamic = false
var IceTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("spawnFirstIce"), userInfo: nil, repeats: true)
self.addChild(person)
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == physicsCatagory.person && secondBody.categoryBitMask == physicsCatagory.Ice || firstBody.categoryBitMask == physicsCatagory.Ice && secondBody.categoryBitMask == physicsCatagory.person{
NSLog ("Test Test") }
}
}
func spawnFirstIce(){
let Ice = SKSpriteNode(imageNamed: "FirstIce")
Ice.setScale(0.5)
Ice.physicsBody = SKPhysicsBody(rectangleOfSize: Ice.size)
Ice.physicsBody?.categoryBitMask = physicsCatagory.Ice
Ice.physicsBody?.contactTestBitMask = physicsCatagory.person
Ice.physicsBody?.affectedByGravity = false
Ice.physicsBody?.dynamic = true
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 20
let SpawnPoint = UInt32(MaxValue - MinValue)
Ice.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
self.addChild(Ice)
let action = SKAction.moveToY(-85, duration: 3.0)
let actionDone = SKAction.removeFromParent()
Ice.runAction(SKAction.sequence([action,actionDone]))
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
person.position.x = location.x
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
person.position.x = location.x
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
You have defined didBeginContact inside of didMoveToView method :)
Place didBeginContact outside of that method and make it a member of GameScene class.

Spritekit - Swift/ Multiple collisions. I'm having trouble with putting the cell and the bacteria in the same function

I'm having trouble getting the bacteria and the cell below together inside the didBeginContact function. It seems like I have to separate them from each other for them to have different outcomes when they collide with objects instead of getting mixed up together.
let virusMask:UInt32 = 0x1 << 0
let cellMask:UInt32 = 0x1 << 1
let finishMask:UInt32 = 0x1 << 2
let wallMask:UInt32 = 0x1 << 3
class GameScene: SKScene, SKPhysicsContactDelegate {
var touchLocation: CGPoint = CGPointZero
var boneCannon: SKSpriteNode!
var smoke: SKEmitterNode!
var Finish: SKSpriteNode!
var bacteria: SKScene!
override func didMoveToView(view: SKView) {
boneCannon = self.childNodeWithName("boneCannon") as! SKSpriteNode
Finish = self.childNodeWithName("FinishLine") as! SKSpriteNode
let bacteria:SKSpriteNode = SKScene(fileNamed: "bacterioP")!.childNodeWithName("bacteriophage") as! SKSpriteNode
bacteria.removeFromParent()
self.addChild(bacteria)
bacteria.zPosition = 2
bacteria.position = CGPointMake(600, 600)
bacteria.runAction(SKAction.moveToY(1920, duration: 5))
bacteria.physicsBody?.collisionBitMask = wallMask|cellMask|virusMask
bacteria.physicsBody?.contactTestBitMask = bacteria.physicsBody!.collisionBitMask | virusMask
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.smoke = SKEmitterNode(fileNamed: "FireFlies")!
self.smoke.position = CGPointMake(600, 600)
self.smoke.zPosition = 1
self.addChild(smoke)
let a1 = SKAction.moveByX(850.0, y: 0.0, duration: 0.75)
let a2 = SKAction.moveByX(-850.0, y: 0.0, duration: 0.75)
let seq = SKAction.sequence([a1,a2])
self.Finish.runAction(SKAction.repeatActionForever(seq))
self.physicsWorld.contactDelegate = self
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
touchLocation = touches.first!.locationInNode(self)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
touchLocation = touches.first!.locationInNode(self)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let cell: SKSpriteNode = SKScene(fileNamed: "WhiteBloodCell")!.childNodeWithName("cell") as! SKSpriteNode
cell.removeFromParent()
self.addChild(cell)
cell.zPosition = 1
cell.position = boneCannon.position
let cellRadians = Float(boneCannon.zRotation)
let speed = CGFloat(20.0)
let vx: CGFloat=CGFloat(cosf(cellRadians)) * speed
let vy: CGFloat = CGFloat(sinf(cellRadians)) * speed
cell.physicsBody?.applyImpulse(CGVectorMake(vx, vy))
cell.physicsBody?.collisionBitMask = virusMask | cellMask | wallMask
cell.physicsBody?.contactTestBitMask = cell.physicsBody!.collisionBitMask | finishMask
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
let percent = touchLocation.x / size.width
let newAngle = percent * 180-180
boneCannon.zRotation = CGFloat(newAngle) * CGFloat(M_PI) / 180.0
}
func didBeginContact(contact: SKPhysicsContact) {
let cell = (contact.bodyA.collisionBitMask == cellMask) ? contact.bodyA: contact.bodyB
let other = (cell == contact.bodyA) ? contact.bodyB: contact.bodyA
if other.categoryBitMask == virusMask{
self.didHitVirus(other)
}
if other.categoryBitMask == finishMask{
let secondlevel:GameScene = GameScene(fileNamed: "SecondLevel")!
secondlevel.scaleMode = .AspectFill
self.view?.presentScene(secondlevel)
}
let bacteria = (contact.bodyA.collisionBitMask == virusMask) ? contact.bodyA: contact.bodyB
let object = (bacteria == contact.bodyA) ? contact.bodyB: contact.bodyA
if object.categoryBitMask == wallMask{
let fail:GameScene = GameScene(fileNamed: "ThirdLevel")!
fail.scaleMode = .AspectFill
self.view?.presentScene(fail)
}
func didHitVirus(virus:SKPhysicsBody){
let particleEffect: SKEmitterNode = SKEmitterNode(fileNamed: "ParticleScene")!
//if ((virus.node?.position) != nil) {}
particleEffect.position = CGPointMake(600, 600)
particleEffect.zPosition = 4;
self.addChild(particleEffect)
virus.node?.removeFromParent()
}
}

Adding multiple sprites to a screen

I am new to SpriteKit.
I started working on a game where I want a menu of sprites that I can select from. When I select a particular sprite I want to then be able to tap on the screen and produce copies of them.
Here is a copy of my code I have trying.
class GameScene: SKScene
{
override func didMoveToView(view: SKView)
{
let button1 = SKSpriteNode(imageNamed: "Monster1A.png")
button1.position = CGPointMake(500, 600)
button1.name = "Monster1A"
self.addChild(button1)
let button2 = SKSpriteNode(imageNamed: "Monster2A.png")
button2.position = CGPointMake(300, 600)
button2.name = "Monster2A"
self.addChild(button2)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
let touch = touches
let location = touch.first!.locationInNode(self)
let node = self.nodeAtPoint(location)
if (node.name == "Monster1A")
{
for touch in touches
{
let touchLocation = touch.locationInNode(self)
let sprite = SKSpriteNode(imageNamed:"Monster1A")
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.position = touchLocation
let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)
sprite.runAction(SKAction.repeatActionForever(action))
self.addChild(sprite)
}
}
if (node.name == "Monster2A")
{
for touch in touches
{
let touchLocation = touch.locationInNode(self)
let sprite = SKSpriteNode(imageNamed:"Monster2A")
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.position = touchLocation
let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)
sprite.runAction(SKAction.repeatActionForever(action))
self.addChild(sprite)
}
}
}
}
Try this :)
import SpriteKit
class GameScene: SKScene {
var isDuplicating: Bool = false
var selectedSpriteName: String!
override func didMoveToView(view: SKView) {
let button1 = SKSpriteNode(imageNamed:"Monster1A")
button1.position = CGPointMake(500, 600)
button1.name = "Monster1A"
self.addChild(button1)
let button2 = SKSpriteNode(imageNamed:"Monster2A")
button2.position = CGPointMake(300, 600)
button2.name = "Monster2A"
self.addChild(button2)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
let sprite = self.nodeAtPoint(location)
if sprite.name == "Monster1A" {
if self.isDuplicating == true {
// Deselect
self.isDuplicating = false
return
}
self.selectedSpriteName = "Monster1A"
self.isDuplicating = true
} else if sprite.name == "Monster2A" {
if self.isDuplicating == true {
// Deselect
self.isDuplicating = false
return
}
self.selectedSpriteName = "Monster2A"
self.isDuplicating = true
} else {
if self.isDuplicating == true {
let newsprite = SKSpriteNode(imageNamed: self.selectedSpriteName)
newsprite.position = location
addChild(newsprite)
}
}
}
}
}

Swift MultiTouch objects

This is my first game in Swift. I want to make a more advanced Breakout Game with SpriteKit like in this tutorial
I want to create 4 paddles and move them individually with 4 fingers, I tried to do that but when I move one paddle the second one moves too... What can I do ?
import SpriteKit
import AVFoundation
class GameScene: SKScene {
var fingerIsOnPaddle1 = false
var fingerIsOnPaddle2 = false
var fingerISOnPaddle3 = false
var fingerIsOnPaddle4 = false
let shurikenCategoryName = "shuriken"
let paddleCategoryName = "paddle"
let paddle2CategoryName = "paddle2"
let paddle3CategoryName = "paddle3"
let paddle4CategoryName = "paddle4"
let brickCategoryName = "brick"
let backgroundMusicPlayer = AVAudioPlayer()
override init(size: CGSize){
super.init(size: size)
let bgMusicURL = NSBundle.mainBundle().URLForResource("bgMusicMP3", withExtension: "mp3")
backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: bgMusicURL, error: nil)
backgroundMusicPlayer.numberOfLoops = -1
backgroundMusicPlayer.prepareToPlay()
backgroundMusicPlayer.play()
let backgroundImage = SKSpriteNode(imageNamed: "bg")
backgroundImage.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 3)
self.addChild(backgroundImage)
self.physicsWorld.gravity = CGVectorMake(0, 0)
let worldBorder = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody = worldBorder
self.physicsBody?.friction = 0
let shuriken = SKSpriteNode(imageNamed: "shuriken")
shuriken.name = shurikenCategoryName
shuriken.position = CGPointMake(self.frame.size.width/2, self.frame.size.height / 2)
self.addChild(shuriken)
shuriken.physicsBody = SKPhysicsBody(circleOfRadius: shuriken.frame.width / 2)
shuriken.physicsBody?.friction = 0
shuriken.physicsBody?.restitution = 1
shuriken.physicsBody?.linearDamping = 0
shuriken.physicsBody?.applyImpulse(CGVectorMake(2, 2))
shuriken.physicsBody?.allowsRotation = true
let paddle = SKSpriteNode(imageNamed: "paddle")
paddle.name = paddleCategoryName
paddle.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)/4)
self.addChild(paddle)
paddle.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.frame.size)
paddle.physicsBody?.friction = 0.4
paddle.physicsBody?.restitution = 0.1
paddle.physicsBody?.dynamic = false
//paddle.position = CGPointMake(CGRectGetMidX(self.frame)/2, CGRectGetMidY(self.frame)/4)
//self.addChild(paddle)
let paddle2 = SKSpriteNode(imageNamed: "paddle")
paddle2.name = paddle2CategoryName
paddle2.position = CGPointMake(CGRectGetMidX(self.frame)/5, CGRectGetMidY(self.frame))
paddle2.zRotation = CGFloat(M_PI_2)
self.addChild(paddle2)
paddle2.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.frame.size)
paddle2.physicsBody?.friction = 0.4
paddle2.physicsBody?.restitution = 0.1
paddle2.physicsBody?.dynamic = false
let paddle3 = SKSpriteNode(imageNamed: "paddle")
paddle3.name = paddle2CategoryName
paddle3.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)/4*7)
self.addChild(paddle3)
let paddle4 = SKSpriteNode(imageNamed: "paddle")
paddle4.name = paddle2CategoryName
paddle4.position = CGPointMake(CGRectGetMidX(self.frame)/5*9, CGRectGetMidY(self.frame))
paddle4.zRotation = CGFloat(M_PI_2)
self.addChild(paddle4)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as UITouch
let touchLocation = touch.locationInNode(self)
let body:SKPhysicsBody? = self.physicsWorld.bodyAtPoint(touchLocation)
if body?.node?.name == paddleCategoryName {
println("Paddle 1 touched")
fingerIsOnPaddle1 = true
}
if body?.node?.name == paddle2CategoryName{
println("Paddle 2 touched")
fingerIsOnPaddle2 = true
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if fingerIsOnPaddle1{
let touch = touches.anyObject() as UITouch
let touchLoc = touch.locationInNode(self)
let prevTouchLoc = touch.previousLocationInNode(self)
let paddle = self.childNodeWithName(paddleCategoryName) as SKSpriteNode
var newXPos = paddle.position.x + (touchLoc.x - prevTouchLoc.x)
paddle.position = CGPointMake(newXPos, paddle.position.y)
}
if fingerIsOnPaddle2{
let touch2 = touches.anyObject() as UITouch
let touchLoc2 = touch2.locationInNode(self)
let prevTouchLoc2 = touch2.previousLocationInNode(self)
let paddle2 = self.childNodeWithName(paddle2CategoryName) as SKSpriteNode
var newYPos = paddle2.position.y + (touchLoc2.y - prevTouchLoc2.y)
paddle2.position = CGPointMake(paddle2.position.y, newYPos)
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
}
required init?(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}
}
The crux of the issue here is in the touchesBegan function, which takes a Set of touches. It's a very common pattern to just take the first one, using anyObject(), but instead of doing that, you can loop over them, like this:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch in touches as? UITouch {
let touchLocation = touch.locationInNode(self)
let body:SKPhysicsBody? = self.physicsWorld.bodyAtPoint(touchLocation)
if body?.node?.name == paddleCategoryName {
println("Paddle 1 touched")
fingerIsOnPaddle1 = true
}
if body?.node?.name == paddle2CategoryName{
println("Paddle 2 touched")
fingerIsOnPaddle2 = true
}
}
}