How do I detect if an SKSpriteNode has been touched

First set the name property of the SKSpriteNode to a string. pineapple.name = “pineapple” pineapple.userInteractionEnabled = false then in touchesBegan function in the Scene override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { let touch:UITouch = touches.anyObject()! as UITouch let positionInScene = touch.locationInNode(self) let touchedNode = self.nodeAtPoint(positionInScene) if let name = touchedNode.name { if name == … Read more

What’s the best way to handle multiple SKScenes?

Having a Single View Controller and Multiple Scenes You could use single view controller (which is actually a default state of SpriteKit game template) and have multiple scenes. So you will have GameViewController and LandingScene, GameScene and possible some other scenes, like LevelSelect scene or something like that. In GameViewController, you initialize your scene for … Read more

how to throw SKSpriteNode?

Here is a quick example I wrote of moving a sprite with touch by simulating its velocity in the game-loop as opposed to setting the position directly. This makes the sprite more dynamic (i.e. you can “throw” it, and let it interact with other physics bodies as your drag the sprite). No angle calculations are … Read more

How to Detect collision in Swift, Sprite kit

Define unique categories, ensure your class is a SKPhysicsContactDelegate and make yourself the physics contact delegate: //Physics categories let enemyCategory: UInt32 = 1 << 1 let bulletCategory: UInt32 = 1 << 2 class GameScene: SKScene, SKPhysicsContactDelegate { physicsWorld.contactDelegate = self Assign the categories (usually in didMove(to view:) : enemy.physicsBody.catgeoryBitMask = enemyCategory bullet.physicsBody.catgeoryBitMask = bulletCategory (Make … Read more

Setting up buttons in SKScene

you could use a SKSpriteNode as your button, and then when the user touches, check if that was the node touched. Use the SKSpriteNode’s name property to identify the node: //fire button – (SKSpriteNode *)fireButtonNode { SKSpriteNode *fireNode = [SKSpriteNode spriteNodeWithImageNamed:@”fireButton.png”]; fireNode.position = CGPointMake(fireButtonX,fireButtonY); fireNode.name = @”fireButtonNode”;//how the node is identified later fireNode.zPosition = 1.0; … Read more