Avoiding collision in spriteKit on iOS while maintaining contact - ios

I have 2 physicsBodies and don't want them to collide (ie they must pass through each other) but still want to be notified in case of contact, however when setting the collisionBitMask to 0, the contact event is not called anymore. What am I doing wrong ?
let stoneCategory : UInt32 = 0x1 << 1
let birdCategory : UInt32 = 0x1 << 2
physicsWorld.contactDelegate = self
stoneNode = SKSpriteNode(imageNamed: "Stone");
self.addChild(stoneNode)
stoneNode.physicsBody = SKPhysicsBody(rectangleOf: stoneNode.size);
stoneNode.physicsBody?.categoryBitMask = stoneCategory
stoneNode.physicsBody?.contactTestBitMask = birdCategory
stoneNode.physicsBody?.collisionBitMask = 0
birdNode = SKSpriteNode();
<...Set texture...>
self.addChild(birdNode)
birdNode.physicsBody = SKPhysicsBody(rectangleOf: birdNode.size);
birdNode.physicsBody?.categoryBitMask = birdCategory
birdNode.physicsBody?.contactTestBitMask = stoneCategory
birdNode.physicsBody?.collisionBitMask = 0
birdNode.physicsBody?.isDynamic = false;
func didBegin(_ contact: SKPhysicsContact) {
print("Contact between "+contact.bodyA.node!.name!+" and "+contact.bodyB.node!.name!); // --> Never called
}

You might have a different problem in your code elsewhere, because this is working for me. The bodies do not collide and the contact happens.
Sample playground code:
import PlaygroundSupport
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var stoneNode: SKShapeNode!
var birdNode: SKShapeNode!
override func didMove(to view: SKView) {
let stoneCategory : UInt32 = 0x1 << 1
let birdCategory : UInt32 = 0x1 << 2
physicsWorld.contactDelegate = self
stoneNode = SKShapeNode(circleOfRadius: 30)
stoneNode.name = "stone"
stoneNode.position = CGPoint(x: 0, y: 100)
addChild(stoneNode)
stoneNode.physicsBody = SKPhysicsBody(circleOfRadius: 30)
stoneNode.physicsBody?.categoryBitMask = stoneCategory
stoneNode.physicsBody?.contactTestBitMask = birdCategory
stoneNode.physicsBody?.collisionBitMask = 0
birdNode = SKShapeNode(circleOfRadius: 40)
birdNode.name = "bird"
birdNode.position = CGPoint(x: 0, y: -100)
addChild(birdNode)
birdNode.physicsBody = SKPhysicsBody(circleOfRadius: 40);
birdNode.physicsBody?.categoryBitMask = birdCategory
birdNode.physicsBody?.contactTestBitMask = stoneCategory
birdNode.physicsBody?.collisionBitMask = 0
birdNode.physicsBody?.isDynamic = false;
}
func didBegin(_ contact: SKPhysicsContact) {
print("Contact between "+contact.bodyA.node!.name!+" and "+contact.bodyB.node!.name!); // --> Never called
}
}
// Load the SKScene from 'GameScene.sks'
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 640, height: 480))
if let scene = GameScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
sceneView.presentScene(scene)
}
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView
And in the console, when the first body drops, being affected by gravity, it's printed:
Contact between stone and bird

Related

How to do i set up proper collision detection in SpriteKit, Swift 3

I had just recently set up collision detection in my game it is doesn't seem to be working. I am very new to spritekit and i would like to know what is happening. Below i will provide you the gamscene class, didMoveToView, a function that is spawning the balls in the view, and my Did begin contact function. Please note that i am working with just the red Ball and the red block right now so they are the only two colliding. Please tell me if theres anything else in the project that you need to see and is not provided below.
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var greenBlock = SKSpriteNode ()
var redBlock = SKSpriteNode()
var yellowBlock = SKSpriteNode()
var blueBlock = SKSpriteNode()
var greenBall = SKSpriteNode()
var redBall = SKSpriteNode()
var yellowBall = SKSpriteNode()
var blueBall = SKSpriteNode()
var ball = SKSpriteNode()
var ballSpeedTop = CGVector(dx: 0, dy: 5)
var ballSpeedBottom = CGVector(dx: 0, dy: -5)
This is where i define the numbers for the categoryBitMask
let redBallCategory: UInt32 = 0x1 << 0
let greenBallCategory: UInt32 = 0x1 << 1
let blueBallCategory: UInt32 = 0x1 << 2
let yellowBallCategory: UInt32 = 0x1 << 3
let redBlockCategory: UInt32 = 0x1 << 4
let yellowBlockCategory: UInt32 = 0x1 << 5
let greenBlockCategory: UInt32 = 0x1 << 6
let blueBlockCategory: UInt32 = 0x1 << 7
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
Where i am applying the physics bodies to the block.
redBlock = self.childNode(withName: "redBlock") as! SKSpriteNode
redBlock.physicsBody?.categoryBitMask = redBlockCategory
redBlock.physicsBody?.collisionBitMask = redBallCategory
redBlock.physicsBody?.contactTestBitMask = redBallCategory
yellowBlock = self.childNode(withName: "yellowBlock") as! SKSpriteNode
greenBlock = self.childNode(withName: "greenBlock") as! SKSpriteNode
blueBlock = self.childNode(withName: "blueBlock") as! SKSpriteNode
redBlock.physicsBody?.affectedByGravity = false
redBlock.physicsBody?.isDynamic = true
greenBlock.physicsBody?.affectedByGravity = false
greenBlock.physicsBody?.isDynamic = true
blueBlock.physicsBody?.affectedByGravity = false
blueBlock.physicsBody?.isDynamic = false
yellowBlock.physicsBody?.affectedByGravity = false
yellowBlock.physicsBody?.isDynamic = false
initializeGame()
print("hello")
}
func initializeGame (){
Timer.scheduledTimer(timeInterval: TimeInterval(randomBetweenNumbers(firstNum: 2, secondNum: 4)), target: self, selector: #selector(topBall), userInfo: nil, repeats: true)
Timer.scheduledTimer(timeInterval: TimeInterval(randomBetweenNumbers(firstNum: 2, secondNum: 4)), target: self, selector: #selector(bottomBall), userInfo: nil, repeats: true)
}
// top ball spawner
func topBall () -> SKSpriteNode {
let ball: SKSpriteNode?;
if Int(randomBetweenNumbers(firstNum: 0, secondNum: 2)) == 0 {
ball = SKSpriteNode(imageNamed: "redBall")
ball?.name = "redBall"
ball!.physicsBody?.categoryBitMask = redBallCategory
ball!.physicsBody?.collisionBitMask = redBlockCategory
ball!.physicsBody?.contactTestBitMask = redBlockCategory
} else {
ball = SKSpriteNode(imageNamed: "greenBall")
ball?.name = "greenBall"
}
ball!.size = CGSize(width: 30, height: 30)
ball!.anchorPoint = CGPoint(x: 0.5, y: 0.5)
ball!.position = CGPoint(x: 0, y: 0)
ball!.physicsBody = SKPhysicsBody(circleOfRadius: ball!.size.height / 2)
ball!.physicsBody?.affectedByGravity = false
ball!.physicsBody?.isDynamic = true
ball!.zPosition = 2
self.addChild(ball!)
ball!.physicsBody?.applyImpulse(ballSpeedTop)
return ball!
}
the contact function
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == redBallCategory && contact.bodyB.categoryBitMask == redBlockCategory){
print("touched red block")
} else if (contact.bodyB.categoryBitMask == redBallCategory && contact.bodyA.categoryBitMask == redBlockCategory){
print("touched a red block")
}
}
UPDATE::: i found where i went wrong but my problem is not technically solved yet. i had applied the properties of the redBall outside the if statement which meant that the redballs category bit mask, etc. is applied to both balls, i need a way to apply the redBall category bit mask to the redBall when it spawns and a different categorybitmask to the green ball when it spawns.
func topBall () -> SKSpriteNode {
let ball: SKSpriteNode?;
if Int(randomBetweenNumbers(firstNum: 0, secondNum: 2)) == 0 {
ball = SKSpriteNode(imageNamed: "redBall")
ball?.name = "redBall"
} else {
ball = SKSpriteNode(imageNamed: "greenBall")
ball?.name = "greenBall"
}
ball!.size = CGSize(width: 30, height: 30)
ball!.anchorPoint = CGPoint(x: 0.5, y: 0.5)
ball!.position = CGPoint(x: 0, y: 0)
ball!.physicsBody = SKPhysicsBody(circleOfRadius: ball!.size.height / 2)
ball!.physicsBody?.affectedByGravity = false
ball!.physicsBody?.isDynamic = true
ball!.physicsBody?.categoryBitMask = redBallCategory
ball!.physicsBody?.collisionBitMask = redBlockCategory
ball!.physicsBody?.contactTestBitMask = redBlockCategory
ball!.zPosition = 2
self.addChild(ball!)
ball!.physicsBody?.applyImpulse(ballSpeedTop)
return ball!
}
this is how i got it to work but the problem is that i want to apply individual categorybitmasks to the ball depending on wether it is green or red.
Use temporary variables to store the appropriate category, collision and contactTest bitmasks in the same block of code where you set the spawned ball's image and name, and then use these temporary variables to set the actual values in the physicsbody:
var categoryBitMask: UInt32
var collisonBitMask: UInt32
var contactTestBitMask: Uint32
if Int(randomBetweenNumbers(firstNum: 0, secondNum: 2)) == 0 {
ball = SKSpriteNode(imageNamed: "redBall")
ball?.name = "redBall"
categoryBitMask = redBallCategory
collisionBitMask = redBlockCategory
contactTestBitMask = redBlockCategory
} else {
ball = SKSpriteNode(imageNamed: "greenBall")
ball?.name = "greenBall"
categoryBitMask = greenBallCategory
collisionBitMask = greenBlockCategory
contactTestBitMask = greenBlockCategory
}
ball!.size = CGSize(width: 30, height: 30)
ball!.anchorPoint = CGPoint(x: 0.5, y: 0.5)
ball!.position = CGPoint(x: 0, y: 0)
ball!.physicsBody = SKPhysicsBody(circleOfRadius: ball!.size.height / 2)
ball!.physicsBody?.affectedByGravity = false
ball!.physicsBody?.isDynamic = true
ball!.physicsBody?.categoryBitMask = categoryBitMask
ball!.physicsBody?.collisionBitMask = collisionBitMask
ball!.physicsBody?.contactTestBitMask = contactTestBitMask

Struggling with collision detection in Swift/SpriteKit

I'm having a play with game programming in Swift/Xcode and am struggling getting collision to work.
I'm using Xcode 8 Beta 3 (because I couldn't use Xcode 7 on MacOS Sierra) so its using the new Swift 3.0 Syntax. The game has a little puppy (defined in a SK Scene file) that jumps and collects thrown items from the right. The thrown item is created in a function and removed when a new item is thrown.
I need contact with the thrown item comes into contact with the puppy. I've got physics turned on and the item bounces off the puppy but no contact is registered.
Heres my code. Appologies for the mess of code but its very first attempt at Swift and everything just thrown in for now. I can't see what I'm doing wrong :(
UPDATE: As cocoajoe suggested, I've tried setting the masks to 1 and have simplified the code to remove references to the gamescene.sks file and created everything programmatically but its still not working. I've updated the code below to reflect the latest version of the code...
import SpriteKit
import AVFoundation
class GameScene: SKScene, SKPhysicsContactDelegate {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder) is not used in this app")
}
override init(size: CGSize) {
super.init(size: size)
anchorPoint = CGPoint(x: 0.0, y: 0.0)
}
var isJumping = false
var gamePaused = false
var gameRunning = false
var playerScore = 0
var lastSpawnTime = 0
var lastTick = 0
var spawnDelay = 4
let PuppyCategory : UInt32 = 0x1 << 0
let ThrowableCategory : UInt32 = 0x1 << 1
let GroundCategory : UInt32 = 0x1 << 2
override func didMove(to view: SKView) {
physicsWorld.gravity = CGVector(dx: 0.0, dy: -9.8)
physicsWorld.contactDelegate = self
let background = SKSpriteNode(imageNamed: "bg")
background.anchorPoint = CGPoint(x: 0.0, y:0.0)
background.size = size
addChild(background)
// Set up Ground Object
let Ground = SKSpriteNode()
Ground.name = "Ground"
Ground.size = CGSize(width:frame.width, height: frame.height / 10)
Ground.position = CGPoint(x: frame.midX, y: Ground.frame.height / 2 )
Ground.zPosition = -20
Ground.color = SKColor.white()
// Set up the Physics
Ground.physicsBody = SKPhysicsBody(rectangleOf: Ground.size)
Ground.physicsBody!.categoryBitMask = GroundCategory
Ground.physicsBody!.contactTestBitMask = PuppyCategory | ThrowableCategory
Ground.physicsBody!.collisionBitMask = PuppyCategory | ThrowableCategory
Ground.physicsBody?.affectedByGravity = false
Ground.physicsBody?.allowsRotation = false
Ground.physicsBody?.isDynamic = false
Ground.physicsBody?.mass = 1.99999
// Initialise the Node
addChild(Ground)
// Create a Puppy
let Puppy = SKSpriteNode(imageNamed: "puppy1")
Puppy.name = "Puppy"
Puppy.position = CGPoint(x: frame.width / 10, y: frame.height / 2)
Puppy.size = CGSize(width: frame.width / 7, height: frame.height / 5)
Puppy.zPosition = 3
// Set up the Physics
Puppy.physicsBody = SKPhysicsBody(rectangleOf: Puppy.size)
Puppy.physicsBody!.categoryBitMask = PuppyCategory
Puppy.physicsBody!.contactTestBitMask = ThrowableCategory | GroundCategory
Puppy.physicsBody!.collisionBitMask = ThrowableCategory | GroundCategory
Puppy.physicsBody?.affectedByGravity = true
Puppy.physicsBody?.allowsRotation = false
Puppy.physicsBody?.isDynamic = true
Puppy.physicsBody?.mass = 1.99999
// Set up the Textures/Animation Frames
var TextureAtlas = SKTextureAtlas()
var TextureArray = [SKTexture]()
TextureAtlas = SKTextureAtlas(named: "puppy")
for i in 1...TextureAtlas.textureNames.count {
let Name = "puppy\(i).png"
TextureArray.append(SKTexture(imageNamed: Name))
}
Puppy.run(SKAction.repeatForever(SKAction.animate(with:TextureArray, timePerFrame: 0.1)))
// Add the Sprite
addChild(Puppy)
let gameMessage = SKSpriteNode(imageNamed: "TapToPlay")
gameMessage.name = "GameMessage"
gameMessage.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
gameMessage.zPosition = 30
gameMessage.setScale(0.0)
addChild(gameMessage)
let gameLogo = SKSpriteNode(imageNamed: "pjlogo")
gameLogo.name = "GameLogo"
gameLogo.size = CGSize(width: frame.width / 3, height: frame.height / 5)
gameLogo.position = CGPoint(x: frame.midX, y: (frame.maxY / 100) * 80)
gameLogo.zPosition = 30
addChild(gameLogo)
let scale = SKAction.scale(to: 1.0, duration: 1)
scene?.childNode(withName: "GameMessage")!.run(scale)
if let musicURL = Bundle.main.urlForResource("bgmusic", withExtension: "mp3") {
var backgroundMusic: SKAudioNode!
backgroundMusic = SKAudioNode(url: musicURL)
addChild(backgroundMusic)
}
startNewGame()
}
func puppyJump() {
if gameRunning {
let Puppy = childNode(withName: "Puppy")
if !isJumping {
Puppy?.physicsBody?.applyImpulse(CGVector(dx: 0,dy: 1400))
isJumping = true
}
}
}
func generateThrowable() {
print("new enemy")
let oldItem = childNode(withName: "throwableItem")
oldItem?.removeFromParent()
let throwableItem = SKSpriteNode(imageNamed: "puppy1")
throwableItem.name = "throwableItem"
throwableItem.position = CGPoint(x: frame.maxX, y: frame.midY)
throwableItem.zPosition = 3
throwableItem.setScale(0.1)
throwableItem.physicsBody = SKPhysicsBody(rectangleOf: throwableItem.size)
throwableItem.physicsBody!.categoryBitMask = ThrowableCategory
throwableItem.physicsBody!.contactTestBitMask = PuppyCategory | GroundCategory
throwableItem.physicsBody!.collisionBitMask = PuppyCategory | GroundCategory
throwableItem.physicsBody?.friction = 0.1
throwableItem.physicsBody?.restitution = 0.1
throwableItem.physicsBody?.mass = 0.01
throwableItem.physicsBody?.affectedByGravity = true
throwableItem.physicsBody?.allowsRotation = true
throwableItem.physicsBody?.isDynamic = true
addChild(throwableItem)
throwableItem.physicsBody?.applyImpulse(CGVector(dx: -7,dy: 4))
let throwableTrail = SKEmitterNode(fileNamed: "particleTrail.sks")
throwableTrail?.name = "throwableTrail"
throwableTrail?.targetNode = scene
throwableItem.addChild(throwableTrail!)
}
func startNewGame() {
let logo = childNode(withName: "GameLogo")
let gameMessage = childNode(withName: "GameMessage")
logo?.removeFromParent()
gameMessage?.removeFromParent()
let gameScore = SKLabelNode(fontNamed: "Arial")
gameScore.name = "GameScore"
gameScore.text = "Score:0"
gameScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.right
gameScore.fontSize = 20
gameScore.position = CGPoint(x: frame.maxX - 20, y: frame.maxY - 26)
gameScore.zPosition = 30
addChild(gameScore)
let pauseButton = SKLabelNode(fontNamed: "Arial")
pauseButton.name = "pauseButton"
pauseButton.text = "PAUSE"
pauseButton.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.left
pauseButton.fontSize = 20
pauseButton.position = CGPoint(x: 20, y: frame.maxY - 26)
pauseButton.zPosition = 30
addChild(pauseButton)
gameRunning = true
}
func didBeginContact(contact: SKPhysicsContact) {
print("contact")
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
// 2
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// 3
if firstBody.categoryBitMask == PuppyCategory && secondBody.categoryBitMask == GroundCategory {
print("Hit bottom. First contact has been made.")
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
if gameRunning {
let gameScore = childNode(withName: "GameScore") as? SKLabelNode
gameScore?.text = "Score:\(playerScore)"
let currentTick:Int = Int(ceil(currentTime))
if lastTick < currentTick {
lastTick = currentTick
}
if (lastSpawnTime + spawnDelay) < currentTick {
// Time to Spawn new Bad Guy
generateThrowable()
lastSpawnTime = currentTick
}
}
}
}
I haven't checked other parts. (Your code still needs plenty of resources...)
But this line:
func didBeginContact(contact: SKPhysicsContact) {
needs to be changed as:
func didBegin(_ contact: SKPhysicsContact) {
in Swift 3. (Which is suggested by Xcode, with just typing didBegin.)
Please try.

Contact method for a frame

I am trying to implement a contact method to know when my player block has touched the yellow frame. Below is the code I have so far. When i drag the player block to the yellow frames nothing happens.
import SpriteKit
import iAd
struct BitMask {
static let player: UInt32 = 0x1 << 0
static let obstacle: UInt32 = 0x1 << 1
static let frame: UInt32 = 0x1 << 2
}
class GameScene: SKScene, SKPhysicsContactDelegate {
let player = SKShapeNode(rectOfSize: CGSize(width: 30, height: 30))
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
// setup scene's physics body (setup the walls)
physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)
// get current screen size
let screenSize: CGRect = UIScreen.mainScreen().bounds
print("screenWidth: \(screenSize.width) screenHeight: \(screenSize.height)")
// setup play scene
let playScreen = SKSpriteNode(color: .clearColor(), size: CGSize(width: 370, height: 370))
// y position adjustment
let yFrame = 65
playScreen.position = CGPoint(x: frame.midX, y: frame.midY - CGFloat(yFrame))
// create the rectangle which will represent physics body.
let rect = CGRect(origin: CGPoint(x: -playScreen.size.width/2, y: -playScreen.size.height/2), size: playScreen.size)
// apply physics conditions to the play scene
playScreen.physicsBody = SKPhysicsBody(edgeLoopFromRect: rect)
playScreen.physicsBody?.friction = 0
// add play scene to the frame
addChild(playScreen)
let playerScreenFrame = SKShapeNode(rectOfSize: CGSize(width: playScreen.size.width, height: playScreen.size.height))
playerScreenFrame.position = CGPoint(x: frame.midX, y: frame.midY - CGFloat(yFrame))
playerScreenFrame.fillColor = .clearColor()
playerScreenFrame.strokeColor = UIColor(rgba: "#E9BD00")
playerScreenFrame.lineWidth = 3;
addChild(playerScreenFrame)
let bottomRect = CGRectMake(frame.midX, frame.midY - CGFloat(yFrame), playScreen.size.width, playScreen.size.height)
let bottom = SKNode()
bottom.physicsBody = SKPhysicsBody(edgeLoopFromRect: bottomRect)
addChild(bottom)
bottom.physicsBody!.categoryBitMask = BitMask.frame
bottom.physicsBody!.contactTestBitMask = BitMask.player
bottom.physicsBody!.collisionBitMask = BitMask.player
// set up player block
player.name = "player"
player.fillColor = UIColor(rgba: "#E9BD00")
player.strokeColor = .blackColor()
player.position = CGPoint(x:frame.size.width/2, y: frame.size.height/2 - CGFloat(yFrame))
player.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 30, height: 30))
player.physicsBody!.dynamic = false
player.physicsBody!.friction = 0
player.physicsBody!.affectedByGravity = false
player.physicsBody!.allowsRotation = false
addChild(player)
player.physicsBody!.categoryBitMask = BitMask.player
player.physicsBody!.contactTestBitMask = BitMask.obstacle | BitMask.frame
player.physicsBody!.collisionBitMask = BitMask.obstacle | BitMask.frame
// set gravity to 0
physicsWorld.gravity = CGVectorMake(0,0)
// set the scene as the delegate to be notified when two bodies collide
physicsWorld.contactDelegate = self
}
my contact method is as follows:
func didBeginContact(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
}
if (firstBody.categoryBitMask == BitMask.player) && (secondBody.categoryBitMask == BitMask.obstacle) {
// do your thing
print("Player contacted with obstacle")
self.view?.paused = true
}
if (firstBody.categoryBitMask == BitMask.player) && (secondBody.categoryBitMask == BitMask.frame) {
// do your thing
print("Player contacted with frame")
self.view?.paused = true
}
}
Currently you are trying to check for contact between two static bodies, but SpriteKit will not generate any contact events unless at least one body is dynamic.
In your case, the reason for this is related to the fact that edge-based physics bodies are static by default, and your player is static as well, means no contacts.
To fix this, you should set dynamic property on player's physics body to true, like this:
player.physicsBody!.dynamic = true

How to save a high score with SpriteKit?

Im starting to learn how to code and i have no clue how to save the score to stay as a high score. any help will be appreciated! i already tried and tried but i can't manage to get it right. at this point i don't know what to do.
class Game: SKScene, SKPhysicsContactDelegate {
let Ball = SKSpriteNode(imageNamed: "Red.png")
var QuitOption = SKLabelNode()
var ScoreLabel = SKLabelNode()
var timesecond = Int(60)
var locked = false
var loseOption = SKLabelNode()
var scorePoints = SKLabelNode()
var score = Int()
var highScore = SKLabelNode()
let whiteBall = SKSpriteNode(imageNamed: "fingerPointingDown.png")
struct PhysicsCategory {
static let Ball: UInt32 = 0b1
static let whiteBall: UInt32 = 0b10
}
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.whiteColor() // background for the display
self.physicsWorld.gravity = CGVectorMake(0, -9.8)
self.physicsWorld.contactDelegate = self
let SceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
SceneBody.friction = 0
self.physicsBody = SceneBody
scorePoints = SKLabelNode(fontNamed: "Noteworthy-Light")
scorePoints.text = "0"
scorePoints.fontColor = SKColor.blackColor()
scorePoints.fontSize = 35
scorePoints.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*1 - 120)
scorePoints.name = "Points"
scorePoints.hidden = true
addChild(scorePoints)
Ball.size = CGSize(width: 82, height: 82)
Ball.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.1 - 60)
Ball.physicsBody = SKPhysicsBody(circleOfRadius: 41)
Ball.physicsBody?.affectedByGravity = true
Ball.physicsBody?.density = 10
Ball.physicsBody?.restitution = 0.1
Ball.physicsBody?.linearDamping = 0
Ball.name = "Ball"
Ball.physicsBody?.usesPreciseCollisionDetection = true
Ball.physicsBody?.categoryBitMask = PhysicsCategory.Ball
Ball.physicsBody?.contactTestBitMask = PhysicsCategory.whiteBall
Ball.physicsBody?.collisionBitMask = PhysicsCategory.whiteBall
self.addChild(Ball)
QuitOption.text = "Quit"
QuitOption.fontName = "Noteworthy-Light"
QuitOption.fontColor = SKColor.purpleColor()
QuitOption.fontSize = 35
QuitOption.position = CGPoint(x: self.frame.size.width/2 - 160, y: self.frame.size.height*1 - 110)
QuitOption.name = "Quit"
addChild(QuitOption)
ScoreLabel = SKLabelNode(fontNamed: "Noteworthy-Light")
ScoreLabel.fontColor = SKColor.redColor()
ScoreLabel.fontSize = 35 // The + will move it to the right side and - to the left side for more accuracy.
ScoreLabel.position = CGPoint(x: self.frame.size.width/2 + 160, y: self.frame.size.height/1 - 115) // position of ScoreLabelNode
ScoreLabel.name = "Score+"
ScoreLabel.hidden = false
self.addChild(ScoreLabel)
whiteBall.size = CGSize(width: 55, height: 55)
whiteBall.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.8 - 30)
whiteBall.name = "whiteBall"
whiteBall.physicsBody = SKPhysicsBody(circleOfRadius: 25)
whiteBall.physicsBody?.dynamic = false
whiteBall.physicsBody?.restitution = 0.1
whiteBall.physicsBody?.usesPreciseCollisionDetection = true
whiteBall.physicsBody?.categoryBitMask = PhysicsCategory.whiteBall
whiteBall.physicsBody?.contactTestBitMask = PhysicsCategory.Ball
whiteBall.physicsBody?.collisionBitMask = PhysicsCategory.Ball
self.addChild(whiteBall)
}
// Making the ball jump after user touches ball
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.first as! UITouch
var location = touch.locationInNode(self)
var node = self.nodeAtPoint(location)
if (node.name == "Quit"){
let myScene = GameScene(size: self.size)
myScene.scaleMode = scaleMode
let reveal = SKTransition.fadeWithDuration(1)
self.view?.presentScene(myScene, transition: reveal)
}
if (node.name == "Ball"){
for touch: AnyObject in touches {
Ball.physicsBody?.allowsRotation = true
Ball.physicsBody?.velocity = CGVectorMake(0, 0)
Ball.physicsBody?.applyImpulse(CGVectorMake(0, 450))
}
}
if(!self.locked){
self.locked = true
var actionrun = SKAction.waitForDuration(0.5)
var actionwait = SKAction.runBlock({
self.timesecond--
if self.timesecond == 60 {self.timesecond = 0}
self.ScoreLabel.text = "\(self.timesecond)"
if (self.timesecond == 0){
let myScene = WT(size: self.size)
myScene.scaleMode = self.scaleMode
let reveal = SKTransition.fadeWithDuration(1)
self.view?.presentScene(myScene, transition: reveal)
}
})
let loopAction = SKAction.repeatAction(SKAction.sequence([actionwait, actionrun]), count: 60)
ScoreLabel.runAction(loopAction, withKey: "scoreAction")
}
}
func didBeginContact(contact: SKPhysicsContact) {
let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collision == PhysicsCategory.Ball | PhysicsCategory.whiteBall {
score++
scorePoints.text = "Score: \(score)"
scorePoints.hidden = false
}
}
}
The simplest way is to use NSUserDefaults.
NSUserDefaults.standardUserDefaults().setInteger(highScore, forKey: "high_score")
NSUserDefaults.synchronize() // must be used to save
You can then load the score when needed
let highScore: Int = NSUserDefaults.standardUserDefaults().integerForKey("high_score")
You can access NSUserDefaults from anywhere, saving and loading as needed.
If you have more global player state, create an object for it and you could serialize the object by making it conform to NSCoding protocol.
You would need to implement the encode and decode functions, that save aand load your object's properties.

Scene transition in spritekit not transitioning

I am in sprite kit making a game in swift. (I am new to swift) And I would like to transition a scene when a node is pressed. I looked it up and the proper way to transition is
let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
let scene = GameScene(size: self.scene.size)
scene.scaleMode = SKSceneScaleMode.AspectFill
self.scene.view.presentScene(scene, transition: transition)
But when I am programming it doesn't like the self.scene.size and puts a question mark on scene. Here is my code
let playView = PlayScene(size: self.scene?.size)
And I don't know how to fix this. Any suggestions? (Or it might be just a simple error in my part because I am new to swift.
Here is more of my code
In the GameScene.swift File
class GameScene: SKScene {
var playButton = SKSpriteNode(imageNamed: "Play")
var levelsButton = SKSpriteNode(imageNamed: "Levels")
var optionsButton = SKSpriteNode(imageNamed: "Options")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let myLabel = SKLabelNode(fontNamed:"Helvetica-Light")
myLabel.text = "Mini Golf";
myLabel.fontSize = 30;
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:525);
println(size.height)
println(size.width)
let backgroundNode = SKSpriteNode(imageNamed: "Background")
backgroundNode.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame));
playButton.position = CGPoint(x: CGRectGetMidX(self.frame), y: 360)
levelsButton.position = CGPoint(x: CGRectGetMidX(self.frame), y: 295)
optionsButton.position = CGPoint(x: CGRectGetMidX(self.frame), y: 230)
self.addChild(backgroundNode)
self.addChild(playButton)
self.addChild(levelsButton)
self.addChild(optionsButton)
self.addChild(myLabel)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
super.touchesBegan(touches, withEvent: event)
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
//let touchedNode = nodeAtPoint(Location)
//touchedNode.postiion =location
let transitionToPlayScene = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
if CGRectContainsPoint(playButton.frame, location) {
let playView = PlayScene(size: self.scene?.size)
}
}
}
This is the code in the PlayScene.swift file
import Foundation
import UIKit
import SpriteKit
private let WALL_CATEGORY: UInt32 = 0x1 << 0
private let HOLE_CATEGORY: UInt32 = 0x1 << 1
private let GOLF_CLUB_CATEGORY: UInt32 = 0x1 << 2
private let BALL_CATEGORY: UInt32 = 0x1 << 4
private let DECORATION_BORDER_CATEGORY: UInt32 = 0x1 << 5
class PlayScene: SKScene, SKPhysicsContactDelegate {
var ballNode = SKSpriteNode(imageNamed: "Ball")
var flag = SKSpriteNode(imageNamed: "Flag")
var club = SKSpriteNode(imageNamed: "Golf Club")
var tee = SKSpriteNode(imageNamed: "Tee")
var hole = SKSpriteNode(imageNamed: "Hole")
var decoBorder1 = SKSpriteNode(imageNamed: "Border 1")
var decoBorder2 = SKSpriteNode(imageNamed: "Border 2")
override func didMoveToView(view: SKView) {
//Setting up the view's physics
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame);
self.physicsBody = borderBody
self.physicsBody?.friction = 0.0
self.physicsBody?.categoryBitMask = WALL_CATEGORY
self.physicsWorld.gravity = CGVectorMake(0.0 ,0.0);
self.physicsWorld.contactDelegate = self
//Setting the ball's properties
ballNode.position = CGPoint(x: CGRectGetMidX(self.frame), y: 100);
ballNode.physicsBody = SKPhysicsBody(rectangleOfSize: ballNode.frame.size);
ballNode.physicsBody?.categoryBitMask = BALL_CATEGORY
ballNode.physicsBody?.mass = 50.0
ballNode.physicsBody?.dynamic = true
ballNode.physicsBody?.allowsRotation = true
self.addChild(ballNode)
//Setting the club's properties
club.position = CGPoint(x: CGRectGetMidX(self.frame)-20, y: 75);
club.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size);
club.physicsBody?.categoryBitMask = GOLF_CLUB_CATEGORY
club.physicsBody?.mass = 150.0
club.physicsBody?.dynamic = true
club.physicsBody?.allowsRotation = false
self.addChild(club)
//Setting the Tee's properties
tee.position = CGPoint(x: CGRectGetMidX(self.frame), y: 70);
//self.addChild(tee)
//Setting the decoration border nodes
decoBorder1.position = CGPoint(x: CGRectGetMinX(self.frame), y: 500);
decoBorder2.position = CGPoint(x: CGRectGetMaxX(self.frame), y:500);
decoBorder1.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size);
decoBorder2.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size);
decoBorder1.physicsBody?.categoryBitMask = DECORATION_BORDER_CATEGORY
decoBorder1.physicsBody?.dynamic = false;
decoBorder1.physicsBody?.allowsRotation = false
decoBorder2.physicsBody?.categoryBitMask = DECORATION_BORDER_CATEGORY
decoBorder2.physicsBody?.dynamic = false;
decoBorder2.physicsBody?.allowsRotation = false
//self.addChild(decoBorder1)
//self.addChild(decoBorder2))
//Setting the Hole properties
hole.position = CGPoint(x: CGRectGetMidX(self.frame), y: 550);
hole.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size);
hole.physicsBody?.categoryBitMask = HOLE_CATEGORY
hole.physicsBody?.dynamic = false
hole.physicsBody?.allowsRotation = false
//self.addChild(hole)
}
The question mark comes from optional chaining because your scene property must have been declared like this SKScene?. This isn't a problem though, it just means that if your scene is nil, it won't get the size property.
The real problem is probably this line:
self.scene.view.presentScene(scene, transition: transition)
Reason being, your scene probably doesn't have a view before it's presented.
You probably wanted something like this:
(self.view as SKView).presentScene(scene, transition: transition)
You'll notice I said probably a lot. I'm only speculating on the problem because I don't see enough code, but I appreciate the effort to try and narrow down your problem. You should post more though because it may be somewhere else.
Edit
Try replacing this:
let transitionToPlayScene = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
if CGRectContainsPoint(playButton.frame, location) {
let playView = PlayScene(size: self.scene?.size)
}
With this
if CGRectContainsPoint(playButton.frame, location) {
let transitionToPlayScene = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
let nextScene = PlayScene(size: self.size)
self.view!.presentScene(nextScene, transition: transitionToPlayScene)
}
What ended up working for me was this:
let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
let scene = GameScene(size: (self.scene as SKScene!).size)
scene.scaleMode = SKSceneScaleMode.AspectFill
(self.view as SKView!).presentScene(scene, transition: transition)

Resources