Change image when collision Detected in swift? - ios

i am making a simple game as a project and need to change my 'bird image' when it collides with another 'image'
Now i have the collision detection all set up i just need to change the texture of my 'bird image' to my new image
any tips on how to do this? I'm new to programming thought it would be easy but evidently not...
i have tried changing
var birdTexture = SKTexture(imageNamed: "bird_img_1.png") // global variable
to
birdTexture = SKTexture(imageNamed: "bird_img_dead.png") // this is inside collision detection
GameScene Class:-
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var bg = SKSpriteNode() // Create Background
var bird = SKSpriteNode() // Create User Bird
var scoreLabel = SKLabelNode() // Create Score Label
var score: Int = 0 // Create Score Integer
var enemyBird = SKSpriteNode() // Create Enemy Bird
var birdGroup:UInt32 = 1 // Bird Collision Group
var objectGroup:UInt32 = 2 // Enemy Collision Group
var scoreGroup:UInt32 = 3 // Score Collision Group
var gameOver = 0 // Game Over function
var movingObjects = SKNode() // ??
var scoreTimer = NSTimer() // Create Score Timer
var gameOverLabel = SKLabelNode() // Game over label
var labelHolder = SKSpriteNode() // Holds Label - Game Over
var enemySpawnTimer = NSTimer() // Starts Enemy Spawner
var Menu = 0
/* Put Bird in and animate */
var birdTexture = SKTexture(imageNamed: "bird_img_1.png")
var birdTexture2 = SKTexture(imageNamed: "bird_img_2.png")
var birdTexture3 = SKTexture(imageNamed: "bird_img_3.png")
var birdTexture4 = SKTexture(imageNamed: "bird_img_4.png")
var birdDeadTexture = SKTexture(imageNamed: "bird_img_dead.png")
/* ------------------------------------ Main Setup ------------------------------------------ */
override func didMoveToView(view: SKView) {
/* Setup your scene here */
println("Moved to Game Scene")
movingObjects.speed = 0
/* Background Image setup */
backgroundImage()
/* Set up deletates and physics and timer */
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -5)
self.addChild(movingObjects)
self.addChild(labelHolder)
/* Load Score Text */
scoreLabel.fontName = "Helvetica"
scoreLabel.fontSize = 60
scoreLabel.text = "0"
scoreLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)+self.frame.size.height*0.3)
self.addChild(scoreLabel)
bird = SKSpriteNode(texture: birdTexture)
bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
// Make bird smaller
bird.size.height = bird.size.width/12
bird.size.width = bird.size.width/12
// Animate Bird
var animation = SKAction.animateWithTextures([birdTexture, birdTexture2, birdTexture3, birdTexture4], timePerFrame: 0.08)
var makeBirdFlap = SKAction.repeatActionForever(animation)
bird.runAction(makeBirdFlap)
// Load Physics
// birdPhysics()
// Run bird animation
bird.zPosition = 10
self.addChild(bird)
/* Introduce ground and top into the scene */
// Create Ground
var ground = SKSpriteNode()
ground.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height/100 * 16)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 1))
ground.physicsBody?.dynamic = false
ground.physicsBody?.categoryBitMask = objectGroup
// Add Ground to scene
self.addChild(ground)
// Create Top
var top = SKSpriteNode()
top.position = CGPointMake(0, self.frame.size.height)
top.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 1))
top.physicsBody?.dynamic = false
top.physicsBody?.categoryBitMask = objectGroup
// Add Top to scene
self.addChild(top)
}
/* ------------------------------------ Spawning Enemy Birds ------------------------------------------ */
/* Create and Spawn enemy birds */
func enemySpawn() {
let height = self.frame.size.height // screen height as variable
let width = self.frame.size.width // screen width as variable
var bottomBarHeight = self.frame.size.height*0.16 // bottom bar height
var spawningPoint = height - bottomBarHeight // screen height - 16%
var randSpawn = arc4random_uniform(UInt32(spawningPoint)) // random number between 0 and 645
var finalSpawn = CGFloat(randSpawn) + bottomBarHeight // add 16% (bottombarheight to the random number
/*
CHECK VALUES OF SPAWN
println("\(height) is the total screen height")
println("\(bottomBarHeight) is the pixels below the ground")
println("\(spawningPoint) is the random range")
println("\(randSpawn) is a random number")
println("\(finalSpawn) - Spawn Location")
println(enemyBird.position)
*/
if (enemyBird.position.y > 730) {
enemyBird.position.y = 720
}
if (enemyBird.position.y < 130) {
enemyBird.position.y = 135
}
// println(Int(enemyBird.position.y))
// Create enemy bird
var enemyBirdTexture = SKTexture(imageNamed: "enemy_img_2.png")
enemyBird = SKSpriteNode(texture: enemyBirdTexture)
enemyBird.position = CGPoint(x: CGRectGetMidX(self.frame)*2.5, y: finalSpawn)
enemyBird.size.height = enemyBird.size.height/12
enemyBird.size.width = enemyBird.size.width/12
enemyBird.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(enemyBird.size.width, enemyBird.size.height))
enemyBird.physicsBody?.dynamic = false
enemyBird.physicsBody?.categoryBitMask = objectGroup
// Move enemy birds
var moveEnemyBird = SKAction.moveByX(-self.frame.size.width * 2, y: 0, duration: NSTimeInterval(self.frame.size.width / 100)) //4.14 seconds on screen
var removeEnemyBird = SKAction.removeFromParent()
var moveAndRemoveEnemyBird = SKAction.sequence([moveEnemyBird, removeEnemyBird])
// Add enemy to scene and move enemy
enemyBird.runAction(moveEnemyBird)
movingObjects.addChild(enemyBird)
}
/* ------------------------------------ Other Parts ------------------------------------------ */
func backgroundImage() {
/* Put Background Image In */
// Create Background Texture
var bgTexture = SKTexture(imageNamed: "bg.png")
// Link bg variable to texture
bg = SKSpriteNode(texture: bgTexture)
// Position thr bg image
bg.position = CGPoint(x: CGRectGetMidX(self.frame), y: self.frame.height/2)
bg.size.height = self.frame.height
// Move background image left
var moveBg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
var replaceBg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
var moveBgForever = SKAction.repeatActionForever(SKAction.sequence([moveBg, replaceBg]))
// Keep world never ending
for var i:CGFloat = 0; i < 3; i++ {
// Position Background
bg = SKSpriteNode(texture: bgTexture)
bg.position = CGPoint(x: bgTexture.size().width/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
// Stretch background full height of screen
bg.size.height = self.frame.height
// Run the action to move BG
bg.runAction(moveBgForever)
self.addChild(bg)
}
}
func startTimers() {
/* Create Score Timer */
//scoreTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector:Selector("scoreCount"), userInfo: nil, repeats: true)
enemySpawnTimer = NSTimer.scheduledTimerWithTimeInterval(0.75, target: self, selector: Selector("enemySpawn"), userInfo: nil, repeats: true)
}
func birdPhysics() {
/* Give bird physics */
// Give bird physics
bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height/3)
bird.physicsBody?.mass = 0.2
bird.physicsBody?.dynamic = true
bird.physicsBody?.allowsRotation = false
bird.physicsBody?.categoryBitMask = birdGroup
bird.physicsBody?.collisionBitMask = objectGroup
bird.physicsBody?.contactTestBitMask = objectGroup
}
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == objectGroup || contact.bodyB.categoryBitMask == objectGroup {
scoreTimer.invalidate()
enemySpawnTimer.invalidate()
bird = SKSpriteNode(texture: birdDeadTexture)
if gameOver == 0 {
movingObjects.speed = 0
gameOver = 1
movingObjects.removeAllChildren()// Remove all enemies
gameOverLabel.fontName = "Helvetica"
gameOverLabel.fontSize = 25
gameOverLabel.text = "Tap to retry!"
gameOverLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)*1.5)
labelHolder.addChild(gameOverLabel)
gameOverLabel.zPosition = 9
}
}
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if (Menu == 0) {
movingObjects.speed = 1
birdPhysics()
startTimers()
}
if (gameOver == 0) { // Runs if game is not over
bird.physicsBody?.velocity = CGVectorMake(0, 0)
bird.physicsBody?.applyImpulse(CGVectorMake(0, 80))
Menu = 1 // Number on right is jump height
} else { // Runs if game is over
score = 0 // Score int is 0
scoreLabel.text = "0" // Score Label is 0
bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)) // Position Bird in center
bird.physicsBody?.velocity = CGVectorMake(0,0) // Cannot make bird jump
labelHolder.removeAllChildren() // Removes all labels
gameOver = 0 // Sets game over to 0 so game will run
movingObjects.speed = 1
startTimers()
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}

You can just change the texture of the SKSpriteNode like,
sprite.texture = newTexture
Load the textures before the start of the game.
var birdAliveTexture = SKTexture(imageNamed: "bird_img_1.png")
var birdDeadTexture = SKTexture(imageNamed: "bird_img_dead.png")
var bird : SKSpriteNode!
When the sprite is loaded, give a name to the birdSprite.
bird = SKSpriteNode(texture: birdAliveTexture)
bird.name = "bird"
In didBeginContact function
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == objectGroup || contact.bodyB.categoryBitMask == objectGroup {
scoreTimer.invalidate()
enemySpawnTimer.invalidate()
bird.removeAllActions() //changed
bird.texture = birdDeadTexture // changed
if gameOver == 0 {
movingObjects.speed = 0
gameOver = 1
movingObjects.removeAllChildren()// Remove all enemies
gameOverLabel.fontName = "Helvetica"
gameOverLabel.fontSize = 25
gameOverLabel.text = "Tap to retry!"
gameOverLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)*1.5)
labelHolder.addChild(gameOverLabel)
gameOverLabel.zPosition = 9
}
}
}

Related

Detect collision between sprites

I have a problem in my code.
I'm creating a Game and I need to have a sprite that can appear multiple times at the same time, to do so I created a class so I can do addChild(obstacle) multiple times and it will spawn one SKSpriteNode exactly similar to another.
My problem is that I want to check collision between my player and the obstacle but because it's from the same SKSpriteNode the computer can't know of which obstacle I'm talking about.
Here's how I created the player and the obstacle:
import SpriteKit
class Obstacle: SKSpriteNode {
init() {
let obstacleTexture = SKTexture(imageNamed: "obstacle")
super.init(texture: obstacleTexture, color: UIColor.clearColor(), size: CGSize(width: 80, height: 80))
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class GameScene: SKScene {
var player:SKSpriteNode!
override func didMoveToView(view: SKView) {
//player setup
let playerTexture = SKTexture(imageNamed: "player")
player = SKSpriteNode(texture: playerTexture)
player.position = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.2)
}
//how I spawn an obstacle
func spawnObstacle() {
let obstacle = Obstacle()
//obstacle position setup
obstacle.position.x = CGFloat(arc4random()) % self.frame.size.width
obstacle.position.y = self.frame.size.height + 200
//random spin action setup
var rotateObstacle = SKAction.rotateByAngle(CGFloat(M_PI), duration: Double((drand48() + 1) * 0.75))
if random() % 2 == 0 {
rotateObstacle = SKAction.rotateByAngle(CGFloat(M_PI), duration: Double((drand48() + 1) * 0.75))
}else{
rotateObstacle = SKAction.rotateByAngle(-CGFloat(M_PI), duration: Double((drand48() + 1) * 0.75))
}
let rotateObstacleForever = SKAction.repeatActionForever(rotateObstacle)
//random move action setup
let moveObstacle = SKAction.moveTo(CGPointMake(CGFloat(arc4random()) % self.frame.size.width, -200), duration: Double((drand48() + 1) * 1.5))
//running the actions
obstacle.runAction(rotateObstacleForever)
obstacle.runAction(moveObstacle)
addChild(obstacle)
}
}
}
How to detect when the player collide with any obstacle?
To detect collisions you could use the SpriteKit physics.
Here you have 3 elements involved to hypothetical collisions:
The boundaries (or the field where live your player and where you
have your obstacles)
The player
The obstacles
Advice
Set this parameter to your debug phases to see physics objects boundaries :
skView.showsPhysics = true
An example of code (warning:- This code would be only a point to start to realize your physics, I don't know the rest of your project so your job will be to correct as you believe it's better for your objects):
enum CollisionTypes: UInt32 {
case Field = 1
case Player = 2
case Obstacle = 4
}
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, 0) // set your gravity value
self.physicsWorld.contactDelegate = self
let fieldBody = SKPhysicsBody.init(edgeLoopFromRect: self.frame)
self.physicsBody = fieldBody
self.physicsBody!.affectedByGravity = false
self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.dynamic = true
self.physicsBody!.mass = 0.8
self.physicsBody!.friction = 0
self.physicsBody!.linearDamping = 0
self.physicsBody!.angularDamping = 0
self.physicsBody!.restitution = 0
self.physicsBody!.categoryBitMask = CollisionTypes.Field.rawValue
self.physicsBody!.contactTestBitMask = CollisionTypes.Player.rawValue
// Prepare the player
player.physicsBody = SKPhysicsBody(circleOfRadius: player.frame.width/2)
player.physicsBody!.affectedByGravity = false
player.physicsBody!.restitution = 0.0
player.physicsBody!.linearDamping = 0
player.physicsBody!.friction = 0.3
player.physicsBody!.dynamic = true
player.physicsBody!.mass = 0.2
player.physicsBody!.allowsRotation = false
player.physicsBody!.categoryBitMask = CollisionTypes.Player.rawValue
player.physicsBody!.contactTestBitMask = CollisionTypes.Field.rawValue | CollisionTypes.Obstacles.rawValue
player.physicsBody!.collisionBitMask = CollisionTypes.Field.rawValue | CollisionTypes.Obstacles.rawValue
//Prepare the obstacles (you must do it in your obstacle class)
obstacle.physicsBody = SKPhysicsBody(circleOfRadius: obstacle.frame.width/2)
obstacle.physicsBody!.affectedByGravity = false
obstacle.physicsBody!.restitution = 0.0
obstacle.physicsBody!.linearDamping = 0
obstacle.physicsBody!.friction = 0.3
obstacle.physicsBody!.dynamic = true
obstacle.physicsBody!.mass = 0.8
obstacle.physicsBody!.allowsRotation = true
obstacle.physicsBody!.categoryBitMask = CollisionTypes.Obstacle.rawValue
obstacle.physicsBody!.contactTestBitMask = CollisionTypes.Player.rawValue
obstacle.physicsBody!.collisionBitMask = CollisionTypes.Player.rawValue
}
func didBeginContact(contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == CollisionTypes.Player.rawValue &&
contact.bodyB.categoryBitMask == CollisionTypes.Obstacle.rawValue) {
print("contact between Player and Obstacle")
}
if (contact.bodyA.categoryBitMask == CollisionTypes.Player.rawValue &&
contact.bodyB.categoryBitMask == CollisionTypes.Field.rawValue) {
print("contact between Player and Field")
}
}
}

Sprite Kit Game Scene changes when it is not supposed to

In a game that I am currently building a person is supposed to catch balls that are falling from the sky. If the ball goes off the screen it means he didn't catch the ball, and so the scene is supposed to change to a game over scene. The problem is that even if the ball doesn't go below the screen the screen will change. But the screen will change to a blank screen so instead of the GameOverScene().
Here is the code for the GameScene()...
//
// GameScene.swift
// catch balls
//
// Created by Ankith Udupa on 8/10/15.
// Copyright (c) 2015 UdupaLabs. All rights reserved.
//
import SpriteKit
var score = 0
var lossFlag = false
class GameScene: SKScene, SKPhysicsContactDelegate {
var person = SKSpriteNode(imageNamed: "guyLeft_1.png")
var left = true
let kScoreHudName = "scoreHud"
struct PhysicsCategory {
static let None : UInt32 = 0
static let All : UInt32 = UInt32.max
static let Ball : UInt32 = 0b1
static let Person: UInt32 = 0b10
}
override func didMoveToView(view: SKView) {
var content = false
//set up screen
setUpScreen()
//set up the physics
physicsWorld.gravity = CGVectorMake(0, 0)
physicsWorld.contactDelegate = self
//add ball
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(addBall),
SKAction.waitForDuration(1.0)
])
))
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
left = !left
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if ((person.position.x > person.size.width/2) && (person.position.x < size.width-(person.size.width/2))){
if left {
var leftMove = SKAction.moveByX(5, y: 0, duration: 0.1)
person.runAction(leftMove)
}
if !left { // or use an if-else construct
var rightMove = SKAction.moveByX(-5, y: 0, duration: 0.1)
person.runAction(rightMove)
}
}
}
//random number gen functions
func random() -> CGFloat {
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(#min: CGFloat, max: CGFloat) -> CGFloat {
return random() * (max - min) + min
}
//add ball function
func addBall(){
//create ball sprite
var ball = SKSpriteNode(imageNamed: "ball.png")
//create physics for ball
ball.physicsBody = SKPhysicsBody(rectangleOfSize: ball.size) // 1
ball.physicsBody?.dynamic = true // 2
ball.physicsBody?.categoryBitMask = PhysicsCategory.Ball // 3
ball.physicsBody?.contactTestBitMask = PhysicsCategory.Person // 4
ball.physicsBody?.collisionBitMask = PhysicsCategory.None // 5
//generate random postion along x axis for ball to spawn
let actualX = random(min:ball.size.width/2+1, max: size.width - ball.size.width/2-1)
//set balls positon
ball.position = CGPoint(x: actualX, y: size.height - ball.size.width/2)
//add ball to scene
addChild(ball)
//determine speed of ball
let actualDuration = random(min: CGFloat(3.0), max: CGFloat(5.0))
//create movement actions and run them
let actionMove = SKAction.moveTo(CGPoint(x:actualX, y: -ball.size.width/2), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
let Loss = SKAction.runBlock() {
let reveal = SKTransition.crossFadeWithDuration(0.1)
let gameOverScene = GameOverScene()
self.view?.presentScene(GameOverScene(), transition: reveal)
}
ball.runAction(SKAction.sequence([actionMove, Loss, actionMoveDone]))
}
//setUpScreen
func setUpScreen(){
self.backgroundColor = SKColor.whiteColor()
var ground = SKShapeNode(rectOfSize: CGSizeMake(self.frame.size.width, self.frame.size.height * 0.2))
ground.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height * 0.1)
ground.fillColor = SKColor.blueColor()
self.addChild(ground)
person.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height * 0.2)
setUpPersonPhysics()
self.addChild(person)
}
//set up person physics
func setUpPersonPhysics(){
person.physicsBody = SKPhysicsBody(rectangleOfSize: person.size)
person.physicsBody?.dynamic = true
person.physicsBody?.categoryBitMask = PhysicsCategory.Person
person.physicsBody?.contactTestBitMask = PhysicsCategory.Ball
person.physicsBody?.collisionBitMask = PhysicsCategory.None
person.physicsBody?.usesPreciseCollisionDetection = true
}
func didBeginContact(contact: SKPhysicsContact) {
// 1
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
}
// 2
if ((firstBody.categoryBitMask & PhysicsCategory.Ball != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.Person != 0)) {
personDidCollideWithBall(secondBody.node as! SKSpriteNode, ball: firstBody.node as! SKSpriteNode)
}
}
//called when person collides with ball
func personDidCollideWithBall(person:SKSpriteNode, ball:SKSpriteNode) {
println("hit")
ball.removeFromParent()
score++
}
}
and here is the code for the gameOverScene()...
//
// gameOverScene.swift
// catch babies
//
// Created by Ankith Udupa on 8/12/15.
// Copyright (c) 2015 UdupaLabs. All rights reserved.
//
import Foundation
import SpriteKit
class GameOverScene: SKScene {
var message = "Game Over"
override func didMoveToView(view: SKView) {
self.backgroundColor = SKColor.whiteColor()
setUpTextOutPut()
}
func setUpTextOutPut(){
let gameOverLabel = SKLabelNode(fontNamed: "Superclarendon-Black")
gameOverLabel.text = message
gameOverLabel.fontSize = 40
gameOverLabel.fontColor = SKColor.orangeColor()
gameOverLabel.position = CGPoint(x: size.width/2, y: size.height/2)
addChild(gameOverLabel)
let scoreLabel = SKLabelNode(fontNamed: "Superclarendon-Black")
scoreLabel.text = "\(score)"
scoreLabel.fontSize = 40
scoreLabel.fontColor = SKColor.orangeColor()
scoreLabel.position = CGPoint(x: size.width/2, y: size.height/2-50)
addChild(scoreLabel)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
}
}
The error is with your addBall method,
//add ball function
func addBall(){
//create ball sprite
var ball = SKSpriteNode(imageNamed: "ball.png")
//create physics for ball
ball.physicsBody = SKPhysicsBody(rectangleOfSize: ball.size) // 1
ball.physicsBody?.dynamic = true // 2
ball.physicsBody?.categoryBitMask = PhysicsCategory.Ball // 3
ball.physicsBody?.contactTestBitMask = PhysicsCategory.Person // 4
ball.physicsBody?.collisionBitMask = PhysicsCategory.None // 5
//generate random postion along x axis for ball to spawn
let actualX = random(min:ball.size.width/2+1, max: size.width - ball.size.width/2-1)
//set balls positon
ball.position = CGPoint(x: actualX, y: size.height - ball.size.width/2)
//add ball to scene
addChild(ball)
//determine speed of ball
let actualDuration = random(min: CGFloat(3.0), max: CGFloat(5.0))
//create movement actions and run them
let actionMove = SKAction.moveTo(CGPoint(x:actualX, y: -ball.size.width/2), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
let Loss = SKAction.runBlock() {
let reveal = SKTransition.crossFadeWithDuration(0.1)
let gameOverScene = GameOverScene()
self.view?.presentScene(GameOverScene(), transition: reveal)
}
ball.runAction(SKAction.sequence([actionMove, Loss, actionMoveDone]))
}
If you look at the method properly, you ask to run the sequence to the sprite and inside runBlock, you move to another scene. Do you need to check if the ball is outside bounds inside this block and only then present your game over scene ?
Should it be something like this,
let Loss = SKAction.runBlock() {
if ball.position.x > self.size.width + ball.frame.size.width * 0.5 || ball.position.y < ball.frame.size.height * 0.5 {
let reveal = SKTransition.crossFadeWithDuration(0.1)
let gameOverScene = GameOverScene()
self.view?.presentScene(GameOverScene(), transition: reveal)
}
}

Scoring Method for Swift & Spawning objects

I cannot seem to work out how to get my scoring to work...
the concept of the game is birds will come in on the right hand side, and when they reach the left hand side i want my score to increment by 1.
In Obj_c i would have done something like
if (bird.enter.x < 0) {
score++
}
but in swift sprite kit I'm not to sure how to do it...
Another problem I'm having is that i have to use a timer to get my "Enemy Birds" to spawn (the birds going from right to left)
(every 1 second the timer runs the spawn enemy function) but in Obj_c i would have spawned say 3/4 at different x co-ordinates and like the previous code when the bird was < 0 px i would CGPointMake() back over to the other side of the screen so they are on a constant loop
Any ideas of doing this in swift? My code is as follows: its in bit of a weird layout:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var bg = SKSpriteNode() // Create Background
var bird = SKSpriteNode() // Create User Bird
var scoreLabel = SKLabelNode() // Create Score Label
var score: Int = 0 // Create Score Integer
var enemyBird = SKSpriteNode() // Create Enemy Bird
var birdGroup:UInt32 = 1 // Bird Collision Group
var objectGroup:UInt32 = 2 // Enemy Collision Group
var scoreGroup:UInt32 = 3 // Score Collision Group
var gameOver = 0 // Game Over function
var movingObjects = SKNode() // ??
var gameOverLabel = SKLabelNode() // Game over label
var labelHolder = SKSpriteNode() // Holds Label - Game Over
var Menu = 0
/* Put Bird in and animate */
var birdTexture = SKTexture(imageNamed: "bird_img_1.png")
var birdTexture2 = SKTexture(imageNamed: "bird_img_2.png")
var birdTexture3 = SKTexture(imageNamed: "bird_img_3.png")
var birdTexture4 = SKTexture(imageNamed: "bird_img_4.png")
var birdDeadTexture = SKTexture(imageNamed: "bird_img_dead.png")
var finalSpawn: CGFloat = 0.0
/* ------------------------------------ Main Setup ------------------------------------------ */
override func didMoveToView(view: SKView) {
/* Setup your scene here */
println("Moved to Game Scene")
movingObjects.speed = 0
/* Call Functions */
backgroundImage()
enemySpawn()
/* Set up deletates and physics and timer */
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -5)
self.addChild(movingObjects)
self.addChild(labelHolder)
/* Load Score Text */
scoreLabel.fontName = "Helvetica"
scoreLabel.fontSize = 60
scoreLabel.text = "0"
scoreLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)+self.frame.size.height*0.3)
self.addChild(scoreLabel)
enemyBird.position = CGPointMake(self.frame.width+200, 500)
movingObjects.addChild(enemyBird)
bird = SKSpriteNode(texture: birdTexture)
bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
// Make bird smaller
bird.size.height = bird.size.height/12
bird.size.width = bird.size.width/11
// Animate Bird
var animation = SKAction.animateWithTextures([birdTexture, birdTexture2, birdTexture3, birdTexture4], timePerFrame: 0.08)
var makeBirdFlap = SKAction.repeatActionForever(animation)
bird.runAction(makeBirdFlap)
// Load Physics
// birdPhysics()
// Run bird animation
bird.zPosition = 10
self.addChild(bird)
/* Introduce ground and top into the scene */
// Create Ground
var ground = SKSpriteNode()
ground.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height/100 * 16)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 1))
ground.physicsBody?.dynamic = false
ground.physicsBody?.categoryBitMask = objectGroup
// Add Ground to scene
self.addChild(ground)
// Create Top
var top = SKSpriteNode()
top.position = CGPointMake(0, self.frame.size.height)
top.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 1))
top.physicsBody?.dynamic = false
top.physicsBody?.categoryBitMask = objectGroup
// Add Top to scene
self.addChild(top)
}
/* ------------------------------------ Spawning Enemy Birds ------------------------------------------ */
/* Create and Spawn enemy birds */
func enemySpawn() {
let height = self.frame.size.height // screen height as variable
let width = self.frame.size.width // screen width as variable
var bottomBarHeight = self.frame.size.height*0.16 // bottom bar height
var spawningPoint = height - bottomBarHeight // screen height - 16%
var randSpawn = arc4random_uniform(UInt32(spawningPoint)) // random number between 0 and 645
finalSpawn = CGFloat(randSpawn) + bottomBarHeight // add 16% (bottombarheight to the random number
println(Int(finalSpawn))
if (enemyBird.position.y > frame.size.height) {
enemyBird.position.y = frame.size.height - 100
}
if (enemyBird.position.y < self.frame.size.width * 0.16) {
enemyBird.position.y = self.frame.size.width + 0.18
}
// Create enemy bird
var enemyBirdTexture = SKTexture(imageNamed: "enemy_img_2.png")
// enemyBird.position = CGPointMake(self.frame.width+200, 500)
enemyBird = SKSpriteNode(texture: enemyBirdTexture)
enemyBird.size.height = enemyBird.size.height/12
enemyBird.size.width = enemyBird.size.width/11
enemyBird.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(enemyBird.size.width/1.1, enemyBird.size.height/1.1))
enemyBird.physicsBody?.dynamic = false
enemyBird.physicsBody?.categoryBitMask = objectGroup
// BIRD LOOP - ONLY RUNNING ONCE???
let startPoint = CGPointMake(self.frame.width+200, finalSpawn)
let endPoint = CGPointMake(0, finalSpawn)
let moveToEndAction = SKAction.moveTo(endPoint, duration: 2)
let resetTostartAction = SKAction.moveTo(startPoint, duration: 0)
let moveToEndThenStartAgain = SKAction.repeatActionForever(SKAction.sequence([moveToEndAction,resetTostartAction]))
enemyBird.runAction(SKAction.repeatActionForever(moveToEndThenStartAgain))
}
/* ------------------------------------ Other Parts ------------------------------------------ */
func backgroundImage() {
/* Put Background Image In */
// Create Background Texture
var bgTexture = SKTexture(imageNamed: "bg.png")
// Link bg variable to texture
bg = SKSpriteNode(texture: bgTexture)
// Position thr bg image
bg.position = CGPoint(x: CGRectGetMidX(self.frame), y: self.frame.height/2)
bg.size.height = self.frame.height
// Move background image left
var moveBg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
var replaceBg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
var moveBgForever = SKAction.repeatActionForever(SKAction.sequence([moveBg, replaceBg]))
// Keep world never ending
for var i:CGFloat = 0; i < 3; i++ {
// Position Background
bg = SKSpriteNode(texture: bgTexture)
bg.position = CGPoint(x: bgTexture.size().width/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
// Stretch background full height of screen
bg.size.height = self.frame.height
// Run the action to move BG
bg.runAction(moveBgForever)
self.addChild(bg)
}
}
/* ------------------------------------ Create Bird Physics ------------------------------------------ */
func birdPhysics() {
/* Give bird physics */
// Give bird physics
bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height/3)
bird.physicsBody?.mass = 0.2
bird.physicsBody?.dynamic = true
bird.physicsBody?.allowsRotation = false
bird.physicsBody?.categoryBitMask = birdGroup
bird.physicsBody?.collisionBitMask = objectGroup
bird.physicsBody?.contactTestBitMask = objectGroup
}
/* ------------------------------------ If contact is found ------------------------------------------ */
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == objectGroup || contact.bodyB.categoryBitMask == objectGroup {
bird.removeAllActions()
bird.texture = birdDeadTexture
let action = SKAction.rotateByAngle(CGFloat(-M_PI), duration:1)
bird.runAction(SKAction.repeatActionForever(action))
if gameOver == 0 {
movingObjects.speed = 0
gameOver = 1
movingObjects.removeAllChildren()// Remove all enemies
gameOverLabel.fontName = "Helvetica"
gameOverLabel.fontSize = 25
gameOverLabel.text = "Tap to retry!"
gameOverLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)*1.5)
labelHolder.addChild(gameOverLabel)
gameOverLabel.zPosition = 9
}
}
}
/* ------------------------------------ When user touches the screen ------------------------------------------ */
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if (Menu == 0) {
movingObjects.speed = 1
birdPhysics()
let action = SKAction.rotateByAngle(CGFloat(-M_PI), duration:1)
bird.runAction(SKAction.repeatActionForever(action))
}
if (gameOver == 0) { // Runs if game is not over
bird.physicsBody?.velocity = CGVectorMake(0, 0)
bird.physicsBody?.applyImpulse(CGVectorMake(0, 80))
Menu = 1 // Number on right is jump height
} else { // Runs if game is over
score = 0 // Score int is 0
scoreLabel.text = "0" // Score Label is 0
bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)) // Position Bird in center
bird.physicsBody?.velocity = CGVectorMake(0,0) // Cannot make bird jump
labelHolder.removeAllChildren() // Removes all labels
gameOver = 0 // Sets game over to 0 so game will run
movingObjects.speed = 1
bird.texture = birdTexture
// Animate Bird
var animation = SKAction.animateWithTextures([birdTexture, birdTexture2, birdTexture3, birdTexture4], timePerFrame: 0.08)
var makeBirdFlap = SKAction.repeatActionForever(animation)
bird.runAction(makeBirdFlap)
let height = self.frame.size.height
let width = self.frame.size.width
var gameScene: GameScene = GameScene(size: CGSizeMake(width, height))
var spriteView: SKView = self.view as SKView!
var trans :SKTransition = SKTransition.crossFadeWithDuration(0.5)
spriteView.presentScene(gameScene, transition: trans)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if (enemyBird.position.x == 0) {
score = score + 1
scoreLabel.text = "\(score)"
}
if (enemyBird.position.y > frame.size.height) {
enemyBird.position.y = frame.size.height - enemyBird.size.height
}
} }
You can increase the score like
if (bird.position.x < 0)
{
score++
}
And to loop the animation of enemy birds, you can make a looping SKAction from one end to other end. For example like this
let startPoint = CGPointMake(0, 200)
let endPoint = CGPointMake(self.frame.width, 200)
let moveToEndAction = SKAction.moveTo(endPoint, duration: 2)
let resetTostartAction = SKAction.moveTo(startPoint, duration: 0)
let moveToEndThenStartAgain = SKAction.repeatActionForever(SKAction.sequence([moveToEndAction,resetTostartAction]))
node.runAction(SKAction.repeatActionForever(moveToEndThenStartAgain))
To start at a random height each time, you can use
let maxHeight:UInt32 = 400
let startPoint = CGPointMake(0, CGFloat(arc4random() % maxHeight))
let moveToEndAction = SKAction.moveByX(self.frame.width, y: 0, duration: 1.0)
let resetTostartAction = SKAction.runBlock { () -> Void in
let randomHeight = CGFloat(arc4random() % maxHeight)
self.playButton.position = CGPointMake(startPoint.x,randomHeight)
}
let moveToEndThenStartAgain = SKAction.repeatActionForever(SKAction.sequence([moveToEndAction,resetTostartAction]))
enemyBird.runAction(SKAction.repeatActionForever(moveToEndThenStartAgain))
You can change maxHeight as required.

Display Current Score in Game using Sprite Kit and Swift

I have been working on a Flappy Bird type game for a couple of days now in Sprite Kit. I am new to programming so I have been trying to teach myself to make the game through various online videos and tutorials. I have come to the point where I need to create a scoring system that will display the current score on the screen in real time. I have looked and looked for a good tutorial on this but have had no luck finding one. I have a score label set up already in the code below. I have included below my entire scene. All I need to know is how to update the score label text each time the bird flies through a pipe. Any advice or suggestions would be greatly appreciated, thank you.
//
// ArcheryScene.swift
// FlappyBird (swift)
//
// Created by Brandon Ballard on 1/6/15.
// Copyright (c) 2015 Brandon Ballard. All rights reserved.
//
import UIKit
import SpriteKit
class ArcheryScene: SKScene, SKPhysicsContactDelegate {
var bird = SKSpriteNode()
var pipeUpTexture = SKTexture()
var pipeDownTexture = SKTexture()
var pipesMoveAndRemove = SKAction()
var impulse = 1
var count = 0
var scoreLabel = SKLabelNode()
let scoreLabelName = "scoreLabel"
let pipeGap = 150.0
enum ColliderType:UInt32 {
case BIRD = 1
case PIPE = 2
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
backgroundColor = SKColor.cyanColor()
//physics
self.physicsWorld.gravity = CGVectorMake(0.0, -15.0);
self.physicsWorld.contactDelegate = self
//Bird
var birdTexture = SKTexture(imageNamed:"Bird")
birdTexture.filteringMode = SKTextureFilteringMode.Nearest
bird = SKSpriteNode(texture: birdTexture)
bird.setScale(0.6)
bird.position = CGPoint(x: self.frame.width * 0.35 + 20, y: self.frame.size.height * 0.95)
bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height / 2.0)
bird.physicsBody?.dynamic = true
bird.physicsBody?.allowsRotation = true
bird.physicsBody?.affectedByGravity = true
bird.physicsBody!.collisionBitMask = ColliderType.BIRD.rawValue
bird.physicsBody!.contactTestBitMask = ColliderType.PIPE.rawValue
self.addChild(bird)
//Ground
var groundTexture = SKTexture(imageNamed: "Ground")
var sprite = SKSpriteNode(texture: groundTexture)
sprite.setScale(2.0)
sprite.position = CGPointMake(self.size.width / 2, sprite.size.height / 2.0)
self.addChild(sprite)
var ground = SKNode()
ground.position = CGPointMake(0, groundTexture.size().height + 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0))
ground.physicsBody?.dynamic = false
self.addChild(ground)
//Score Label
scoreLabel = SKLabelNode(fontNamed: "ScoreLabel")
scoreLabel.name = scoreLabelName
scoreLabel.fontSize = 125
scoreLabel.fontColor = SKColor.whiteColor()
scoreLabel.text = "\(count)"
println(size.height)
scoreLabel.position = CGPointMake(frame.size.width / 2, frame.size.height / 14)
self.addChild(scoreLabel)
//Pipes
//Create the Pipes
pipeUpTexture = SKTexture(imageNamed: "PipeUp")
pipeDownTexture = SKTexture(imageNamed: "PipeDown")
//Movement of Pipes
let distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeUpTexture.size().width)
let movePipes = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))
let removePipes = SKAction.removeFromParent()
pipesMoveAndRemove = SKAction.sequence([movePipes,removePipes])
//Spawn Pipes
let spawn = SKAction.runBlock({() in self.spawnPipes()})
let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
let spawnThenDelay = SKAction.sequence([spawn,delay])
let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
self.runAction(spawnThenDelayForever)
}
func spawnPipes() {
let pipePair = SKNode()
pipePair.position = CGPointMake(self.frame.size.width + pipeUpTexture.size().width * 2, 0)
pipePair.zPosition = -10
let height = UInt32(self.frame.size.height / 4)
let y = arc4random() % height + height
var pipeDown = SKSpriteNode(texture: pipeDownTexture)
pipeDown.setScale(2.0)////////
pipeDown.position = CGPointMake(3.0, CGFloat(y) + pipeDown.size.height + CGFloat(pipeGap) )
pipeDown.physicsBody = SKPhysicsBody(rectangleOfSize: pipeDown.size)
pipeDown.physicsBody?.dynamic = false
pipeDown.physicsBody!.affectedByGravity = false
pipeDown.physicsBody!.collisionBitMask = ColliderType.PIPE.rawValue
pipePair.addChild(pipeDown)
var pipeUp = SKSpriteNode(texture: pipeUpTexture)
pipeUp.setScale(2.0)
pipeUp.position = CGPointMake(0.0, CGFloat(y))
pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size )
pipeUp.physicsBody?.dynamic = false
pipeUp.physicsBody!.affectedByGravity = false
pipeUp.physicsBody!.collisionBitMask = ColliderType.PIPE.rawValue
pipePair.addChild(pipeUp)
pipePair.runAction(pipesMoveAndRemove)
self.addChild(pipePair)
}
func didBeginContact(contact: SKPhysicsContactDelegate) {
impulse = 0
let fadeOut = SKAction.sequence([SKAction.waitForDuration(3.0)])
let welcomeReturn = SKAction.runBlock({
let Transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
let welcomeScene = GameScene(fileNamed: "GameScene")
welcomeScene.scaleMode = .AspectFill
self.scene!.view?.presentScene(welcomeScene, transition: Transition)
})
let sequence = SKAction.sequence([fadeOut, welcomeReturn])
self.runAction(sequence)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if bird.position.y > (self.frame.size.height * 0.999){
impulse = 0
}
if impulse == 1 {
bird.physicsBody?.velocity = CGVectorMake( 0, 0 )
bird.physicsBody?.applyImpulse(CGVectorMake(0,25))
}
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
Just set up an invisible sprite between your pipes. when your bird passes through it, you can detect that and increase your score.
you can make an invisible sprite like this
let scoreSprite = SKSpriteNode(color: SKColor.clearColor(), size: theSize)
add a physics body to that, and youre good to go.

My Sprites Are Scrunched in Sprite Kit using Swift

I am creating my first game with Sprite Kit. It is a Flappy Bird type game. The image on the left is what the game looked like earlier today. I ended up creating an entirely new project. I copied the code for the previous project and pasted it into the current one. I placed the same exact images as the previous project into the current one as well. There are no compiling errors and everything works the same except that the game now looks like the image on the right. As you can see the images widths are smaller. Any advice would be greatly appreciated, if you would like me to provide all of the code in this question let me know, thank you.
This is all of the code for the Archery Scene:
//
// ArcheryScene.swift
// FlappyBird (swift)
//
// Created by Brandon Ballard on 1/6/15.
// Copyright (c) 2015 Brandon Ballard. All rights reserved.
//
import UIKit
import SpriteKit
class ArcheryScene: SKScene, SKPhysicsContactDelegate {
var bird = SKSpriteNode()
var pipeUpTexture = SKTexture()
var pipeDownTexture = SKTexture()
var pipesMoveAndRemove = SKAction()
var score = 0
let pipeGap = 150.0
enum ColliderType:UInt32 {
case BIRD = 1
case PIPE = 2
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
backgroundColor = SKColor.cyanColor()
//physics
self.physicsWorld.gravity = CGVectorMake(0.0, -15.0);
self.physicsWorld.contactDelegate = self
//Bird
var birdTexture = SKTexture(imageNamed:"Bird")
birdTexture.filteringMode = SKTextureFilteringMode.Nearest
bird = SKSpriteNode(texture: birdTexture)
bird.setScale(0.6)
bird.position = CGPoint(x: self.frame.width * 0.35 + 20, y: self.frame.size.height * 0.95)
bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height / 2.0)
bird.physicsBody?.dynamic = true
bird.physicsBody?.allowsRotation = true
bird.physicsBody?.affectedByGravity = true
bird.physicsBody!.collisionBitMask = ColliderType.BIRD.rawValue
bird.physicsBody!.contactTestBitMask = ColliderType.PIPE.rawValue
self.addChild(bird)
//Ground
var groundTexture = SKTexture(imageNamed: "Ground")
var sprite = SKSpriteNode(texture: groundTexture)
sprite.setScale(2.0)
sprite.position = CGPointMake(self.size.width / 2, sprite.size.height / 2.0)
self.addChild(sprite)
var ground = SKNode()
ground.position = CGPointMake(0, groundTexture.size().height + 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0))
ground.physicsBody?.dynamic = false
self.addChild(ground)
//Pipes
//Create the Pipes
pipeUpTexture = SKTexture(imageNamed: "PipeUp")
pipeDownTexture = SKTexture(imageNamed: "PipeDown")
//Movement of Pipes
let distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeUpTexture.size().width)
let movePipes = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))
let removePipes = SKAction.removeFromParent()
pipesMoveAndRemove = SKAction.sequence([movePipes,removePipes])
//Spawn Pipes
let spawn = SKAction.runBlock({() in self.spawnPipes()})
let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
let spawnThenDelay = SKAction.sequence([spawn,delay])
let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
self.runAction(spawnThenDelayForever)
}
func spawnPipes() {
let pipePair = SKNode()
pipePair.position = CGPointMake(self.frame.size.width + pipeUpTexture.size().width * 2, 0)
pipePair.zPosition = -10
let height = UInt32(self.frame.size.height / 4)
let y = arc4random() % height + height
var pipeDown = SKSpriteNode(texture: pipeDownTexture)
pipeDown.setScale(2.0)////////
pipeDown.position = CGPointMake(3.0, CGFloat(y) + pipeDown.size.height + CGFloat(pipeGap) )
pipeDown.physicsBody = SKPhysicsBody(rectangleOfSize: pipeDown.size)
pipeDown.physicsBody?.dynamic = false
pipeDown.physicsBody!.affectedByGravity = false
pipeDown.physicsBody!.collisionBitMask = ColliderType.PIPE.rawValue
pipePair.addChild(pipeDown)
var pipeUp = SKSpriteNode(texture: pipeUpTexture)
pipeUp.setScale(2.0)
pipeUp.position = CGPointMake(0.0, CGFloat(y))
pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size )
pipeUp.physicsBody?.dynamic = false
pipeUp.physicsBody!.affectedByGravity = false
pipeUp.physicsBody!.collisionBitMask = ColliderType.PIPE.rawValue
pipePair.addChild(pipeUp)
pipePair.runAction(pipesMoveAndRemove)
self.addChild(pipePair)
}
func didBeginContact(contact: SKPhysicsContactDelegate) {
scene?.view?.paused = true
//gameOver()
}
func createScoreNode() -> SKLabelNode {
let scoreNode = SKLabelNode(fontNamed: "Brandon Ballard")
scoreNode.name = "scoreNode"
let newScore = "\(score)"
scoreNode.text = newScore
scoreNode.fontSize = 125
scoreNode.fontColor = SKColor.cyanColor()
scoreNode.position = CGPointMake(CGRectGetMidX(self.frame), 58)
self.addChild(scoreNode)
return scoreNode
}
func gameOver() {
let scoreNode = self.createScoreNode()
self.addChild(scoreNode)
let fadeOut = SKAction.sequence([SKAction.waitForDuration(3.0), SKAction.fadeOutWithDuration(3.0)])
let welcomeReturn = SKAction.runBlock({
let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
let welcomeScene = GameScene(fileNamed: "GameScene")
self.scene!.view?.presentScene(welcomeScene, transition: transition)
})
let sequence = SKAction.sequence([fadeOut, welcomeReturn])
self.runAction(sequence)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
bird.physicsBody?.velocity = CGVectorMake( 0, 0 )
bird.physicsBody?.applyImpulse(CGVectorMake(0,25))
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
All I needed to do to fix my problem was add a simple line of code to my GameScene file. Here is the code I had in the GameScene file before I solved the problem that presents the ArcheryScene:
welcomeNode?.runAction(fadeAway, completion: {
let doors = SKTransition.pushWithDirection(SKTransitionDirection.Down, duration: 1.0)
let archeryScene = ArcheryScene(fileNamed: "ArcheryScene")
self.view?.presentScene(archeryScene, transition: doors)
})
All I had to do was add this line of code after I created "archeryScene":
archeryScene.scaleMode = .AspectFill
This made the images "fit to screen size". The exact description of ".AspectFill" used by Apple is:
"The scaling factor of each dimension is calculated and the larger of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire area of the view is filled but may cause parts of the scene to be cropped."

Resources