Physics issue: ball is bouncing too high - swift

I am working on game where a ball bounces and hits a platform and then bounces right back. Ideally the ball should bounce to exactly the height that it started at. However, in my game the ball slowly keeps bouncing higher and higher. I've looked at the documentation and tried to change all of the physics properties but nothing seems to work.
Any suggestions?
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let ballCategory:UInt32 = 0x1 << 0;
let circleCategory:UInt32 = 0x1 << 1;
let platformCategory:UInt32 = 0x1 << 2;
var ball = SKShapeNode(circleOfRadius: 20.0)
var circle = SKShapeNode(circleOfRadius: 200.0)
var platform = SKShapeNode(rectOfSize: CGSizeMake(10, 1))
var circleColor = 2
var ballColor = 3
var scoreLabel: SKLabelNode!
var score = 0
override func didMoveToView(view: SKView) {
setUpLabels()
self.physicsWorld.contactDelegate = self
backgroundColor = (UIColor.whiteColor())
ball.fillColor = SKColor.redColor()
ball.strokeColor = SKColor.clearColor()
ball.position = CGPoint(x: self.size.width/2, y: self.size.height/2+60)
ball.physicsBody = SKPhysicsBody(circleOfRadius: 20)
ball.physicsBody?.categoryBitMask = ballCategory
ball.physicsBody?.collisionBitMask = platformCategory
ball.physicsBody?.contactTestBitMask = platformCategory
ball.physicsBody?.dynamic = true
ball.physicsBody?.restitution = 1.0
ball.physicsBody?.affectedByGravity = true
ball.physicsBody?.linearDamping = 0.0
ball.physicsBody?.friction = 0.0
self.addChild(ball)
circle.fillColor = SKColor.clearColor()
circle.strokeColor = SKColor.redColor()
circle.lineWidth = 5.0
circle.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
circle.physicsBody = SKPhysicsBody(edgeLoopFromPath: circle.path)
circle.physicsBody?.categoryBitMask = circleCategory
self.addChild(circle)
platform.fillColor = SKColor.clearColor()
platform.strokeColor = SKColor.clearColor()
platform.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(10, 1))
platform.position = CGPoint(x: self.size.width/2, y: circle.position.y/2)
platform.physicsBody?.categoryBitMask = platformCategory
platform.physicsBody?.dynamic = false
platform.physicsBody?.friction = 0.0
self.addChild(platform)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
circleColor += 1
if circleColor%3 == 1 {
circle.strokeColor = UIColor.redColor()
}
if circleColor%3 == 2 {
circle.strokeColor = UIColor.greenColor()
}
if circleColor%3 == 3 {
circle.strokeColor = UIColor.yellowColor()
}
if circleColor%3 == 4 {
circle.strokeColor = UIColor.greenColor()
}
if circleColor%3 == 5 {
circle.strokeColor = UIColor.blueColor()
}
if circleColor%3 == 0 {
circle.strokeColor = UIColor.yellowColor()
}
}
func didBeginContact(contact: SKPhysicsContact) {
if ball.fillColor == circle.strokeColor {
score += 1
scoreLabel.text = String("Score \(score)")
}
if ball.fillColor != circle.strokeColor {
let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
let scene = SecondScene(size: self.scene!.size)
scene.scaleMode = SKSceneScaleMode.AspectFill
self.scene!.view!.presentScene(scene, transition: transition)
}
ballColor = Int(arc4random_uniform(10))
if ballColor%3 == 1 {
ball.fillColor = UIColor.redColor()
}
if ballColor%3 == 2 {
ball.fillColor = UIColor.greenColor()
}
if ballColor%3 == 3 {
ball.fillColor = UIColor.yellowColor()
}
if ballColor%3 == 4 {
ball.fillColor = UIColor.greenColor()
}
if ballColor%3 == 5 {
ball.fillColor = UIColor.blueColor()
}
if ballColor%3 == 0 {
ball.fillColor = UIColor.yellowColor()
}
}
func setUpLabels () {
scoreLabel = SKLabelNode(fontNamed: "Arial")
scoreLabel.position = CGPoint(x: 60, y: self.size.height-30)
scoreLabel.text = String("Score \(score)")
scoreLabel.fontColor = UIColor.blackColor()
scoreLabel.fontSize = 25
self.addChild(scoreLabel)
}
override func update(currentTime: CFTimeInterval) {
}
}

Reduce restitution a little which take control of the bounciness of the physics body. Like this:
ball.physicsBody?.restitution = 0.9

In general numerical differential equation solvers that solve the ode for the next time step will introduce energy into the system, which is why you are seeing what you are seeing. You can mitigate this by introduce a damping force. In the case of SpriteKit this means changing the restitution or linear damping factors.
For more about the drift that occurs see: https://en.wikipedia.org/wiki/Energy_drift or http://www.cs.cmu.edu/~baraff/sigcourse/notesb.pdf.

Related

SpriteKit - didBegin contact is called 30 times instead of 1 time

I am making a little FlappyBird clone and have got everything working as it should until I changed the physics body of the bird to be exact to the texture. Now what it does is when it flies through the gap in the pipes it counts 30 points instead of just 1 point.
This only happens when I use the texture exact physics body which I need because the bird isn't round nor rectangular.
How would I go about making the collision so it only collides once with each gap node. I have tried setting the categoryBitBask to 0 after the contact but then all the gaps after don't add to the point count anymore at all.
Here is the full game code:
var score = 0
class GameScene: SKScene, SKPhysicsContactDelegate {
var bird = SKSpriteNode()
var bg = SKSpriteNode()
var ground = SKSpriteNode()
var scoreLabel = SKLabelNode(fontNamed: "Candice")
var gameOverLabel = SKLabelNode(fontNamed: "Candice")
var countbg = SKSpriteNode()
var timer = Timer()
enum ColliderType: UInt32 {
case Bird = 1
case Object = 2
case Gap = 4
}
var gameOver = false
let swooshSound = SKAction.playSoundFileNamed("sfx_swooshing.wav", waitForCompletion: false)
let pointSound = SKAction.playSoundFileNamed("sfx_point.wav", waitForCompletion: false)
let hitSound = SKAction.playSoundFileNamed("sfx_hit.wav", waitForCompletion: false)
#objc func makePipes() {
let movePipes = SKAction.move(by: CGVector(dx: -2 * self.frame.width, dy: 0), duration: TimeInterval(self.frame.width / 150))
let removePipes = SKAction.removeFromParent()
let moveAndRemovePipes = SKAction.sequence([movePipes, removePipes])
let gapHeight = bird.size.height * 2.8
let movementAmount = arc4random() % UInt32(self.frame.height) / 2
let pipeOffset = CGFloat(movementAmount) - self.frame.height / 4
let pipeTexture = SKTexture(imageNamed: "pipe1.png")
let pipe1 = SKSpriteNode(texture: pipeTexture)
pipe1.position = CGPoint(x: self.frame.midX + self.frame.width, y: self.frame.midY + pipeTexture.size().height / 2 + gapHeight / 2 + pipeOffset)
pipe1.zPosition = 2
pipe1.run(moveAndRemovePipes)
pipe1.physicsBody = SKPhysicsBody(rectangleOf: pipeTexture.size())
pipe1.physicsBody!.isDynamic = false
pipe1.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
pipe1.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
pipe1.physicsBody!.collisionBitMask = ColliderType.Object.rawValue
self.addChild(pipe1)
let pipe2Texture = SKTexture(imageNamed: "pipe2.png")
let pipe2 = SKSpriteNode(texture: pipe2Texture)
pipe2.position = CGPoint(x: self.frame.midX + self.frame.width, y: self.frame.midY - pipeTexture.size().height / 2 - gapHeight / 2 + pipeOffset)
pipe2.zPosition = 2
pipe2.run(moveAndRemovePipes)
pipe2.physicsBody = SKPhysicsBody(rectangleOf: pipe2Texture.size())
pipe2.physicsBody!.isDynamic = false
pipe2.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
pipe2.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
pipe2.physicsBody!.collisionBitMask = ColliderType.Object.rawValue
self.addChild(pipe2)
let gap = SKNode()
gap.position = CGPoint(x: self.frame.midX + self.frame.width, y: self.frame.midY + pipeOffset)
gap.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 1, height: gapHeight))
gap.physicsBody!.isDynamic = false
gap.run(moveAndRemovePipes)
gap.physicsBody!.contactTestBitMask = ColliderType.Bird.rawValue
gap.physicsBody!.categoryBitMask = ColliderType.Gap.rawValue
gap.physicsBody!.collisionBitMask = ColliderType.Gap.rawValue
self.addChild(gap)
}
func didBegin(_ contact: SKPhysicsContact) {
if gameOver == false {
if contact.bodyA.categoryBitMask == ColliderType.Gap.rawValue || contact.bodyB.categoryBitMask == ColliderType.Gap.rawValue {
score += 1
scoreLabel.text = String(format: "%05d", score)
run(pointSound)
} else {
self.speed = 0
run(hitSound)
gameOver = true
timer.invalidate()
bird.removeFromParent()
let changeSceneAction = SKAction.run(changeScene)
self.run(changeSceneAction)
}
}
}
//MARK: Change to Game Over Scene
func changeScene(){
let sceneToMoveTo = GameOverScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
setupGame()
}
func setupGame() {
timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.makePipes), userInfo: nil, repeats: true)
let groundTexture = SKTexture(imageNamed: "ground.png")
let moveGroundAnimation = SKAction.move(by: CGVector(dx: -groundTexture.size().width, dy: 0), duration: 7)
let shiftGroundAnimation = SKAction.move(by: CGVector(dx: groundTexture.size().width, dy: 0), duration: 0)
let moveGroundForever = SKAction.repeatForever(SKAction.sequence([moveGroundAnimation, shiftGroundAnimation]))
var i: CGFloat = 0
while i < 3 {
ground = SKSpriteNode(texture: groundTexture)
ground.position = CGPoint(x: self.size.width * i, y: self.size.height / 7.65)
ground.zPosition = 3
ground.run(moveGroundForever)
self.addChild(ground)
i += 1
}
let bottom = SKNode()
bottom.position = CGPoint(x: self.frame.midX, y: self.size.height / 7)
bottom.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.width, height: 1))
bottom.physicsBody!.isDynamic = false
bottom.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
bottom.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
bottom.physicsBody!.collisionBitMask = ColliderType.Object.rawValue
self.addChild(bottom)
let bgTexture = SKTexture(imageNamed: "bg.png")
bg = SKSpriteNode(texture: bgTexture)
bg.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
bg.size = self.frame.size
bg.zPosition = 1
self.addChild(bg)
let birdTexture = SKTexture(imageNamed: "flappy1.png")
let bird2Texture = SKTexture(imageNamed: "flappy2.png")
let bird3Texture = SKTexture(imageNamed: "flappy3.png")
let bird4Texture = SKTexture(imageNamed: "flappy4.png")
let bird5Texture = SKTexture(imageNamed: "flappy5.png")
let bird6Texture = SKTexture(imageNamed: "flappy6.png")
let animation = SKAction.animate(with: [birdTexture, bird2Texture, bird3Texture, bird4Texture, bird5Texture, bird6Texture], timePerFrame: 0.1)
let makeBirdFlap = SKAction.repeatForever(animation)
bird = SKSpriteNode(texture: birdTexture)
bird.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
bird.setScale(1)
bird.zPosition = 6
bird.run(makeBirdFlap)
self.addChild(bird)
bird.physicsBody = SKPhysicsBody.init(circleOfRadius: birdTexture.size().height / 2)
//bird.physicsBody = SKPhysicsBody(texture: birdTexture, size: birdTexture.size())
bird.physicsBody!.isDynamic = false
bird.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
bird.physicsBody!.categoryBitMask = ColliderType.Bird.rawValue
bird.physicsBody!.collisionBitMask = ColliderType.Bird.rawValue
let countbg = SKSpriteNode(imageNamed: "count_bg.png")
countbg.position = CGPoint(x: self.size.width / 4.8, y: self.size.height * 0.94)
countbg.setScale(0.8)
countbg.zPosition = 4
addChild(countbg)
scoreLabel.fontSize = 80
scoreLabel.text = String(format: "%05d", score)
scoreLabel.fontColor = SKColor(red: 218/255, green: 115/255, blue: 76/255, alpha: 1)
scoreLabel.position = CGPoint(x: self.size.width / 4, y: self.size.height * 0.94)
scoreLabel.zPosition = 5
addChild(scoreLabel)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if gameOver == false {
bird.physicsBody!.isDynamic = true
bird.physicsBody!.velocity = CGVector(dx: 0, dy: 0)
bird.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 280))
//run(swooshSound)
} else {
gameOver = false
score = 0
self.speed = 1
self.removeAllChildren()
setupGame()
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
If you would be using RxSwift, you would be able to easily get rid of those extra events easily by using debounce() or throttle() or distinctUntilChanged(). If you are willing to try this approach, give RxSpriteKit framework a go. Otherwise, store a timestamp of the last contact and ignore the following contacts until some time period elapses.

Swift Sprite Kit Physics Body: Ball slows down after applying an impulse

I am quite new to Swift and the Sprite Kit!
I tried to build a Pong game. Everything works well but the ball on which I am applying an impulse slows down really quickly!
I already turned off gravity, friction and damping and set the restitution of the ball and the scene to 1.
Here is my Code:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
    
let player = SKShapeNode(rectOf: CGSize(width: 200, height: 30))
let enemy = SKShapeNode(rectOf: CGSize(width: 200, height: 30))
let ball = SKShapeNode(circleOfRadius: 10)
override func didMove(to view: SKView) {
let border = SKPhysicsBody(edgeLoopFrom: self.frame)
border.restitution = 1
border.friction = 0
border.angularDamping = 0
border.linearDamping = 0
self.physicsBody = border
scene?.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
player.position = CGPoint(x:  0, y: -(scene?.size.height)! * 0.35)
player.fillColor = UIColor.white
player.strokeColor = UIColor.clear
self.addChild(player)
player.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 200, height: 30))
player.physicsBody?.friction = 0
player.physicsBody?.isDynamic = false
player.physicsBody?.angularDamping = 0
player.physicsBody?.linearDamping = 0
player.physicsBody?.restitution = 1
player.physicsBody?.affectedByGravity = false
enemy.position = CGPoint(x: 0, y: (scene?.size.height)! * 0.35)
enemy.fillColor = UIColor.white
enemy.strokeColor = UIColor.clear
self.addChild(enemy)
enemy.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 200, height: 30))
enemy.physicsBody?.friction = 0
enemy.physicsBody?.affectedByGravity = false
enemy.physicsBody?.linearDamping = 0
enemy.physicsBody?.angularDamping = 0
enemy.physicsBody?.isDynamic = false
ball.position = CGPoint(x: 0, y: 0)
ball.fillColor = UIColor.white
ball.strokeColor = UIColor.clear
ball.physicsBody?.angularDamping = 0
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.friction = 0
ball.physicsBody?.restitution = 1
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.affectedByGravity = true
ball.physicsBody = SKPhysicsBody(circleOfRadius: 10)
ball.physicsBody?.isDynamic = true
self.addChild(ball)
ball.physicsBody?.applyImpulse(CGVector(dx: -30, dy: -30))
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let pos = touch.location(in: self)
player.position.x = pos.x
}
}
override func update(_ currentTime: TimeInterval) {
if ball.position.y >  0{
enemy.position.x = ball.position.x
}
}
}
Thank you already!
Be sure to always create your physics body before you change its properties
ball.physicsBody?.angularDamping = 0
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.friction = 0
ball.physicsBody?.restitution = 1
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.affectedByGravity = true
ball.physicsBody = SKPhysicsBody(circleOfRadius: 10) <----Needs to be first
ball.physicsBody?.isDynamic = true
self.addChild(ball)
ball.physicsBody?.applyImpulse(CGVector(dx: -30, dy: -30))

How to change an SKSpriteNode image once a condition is met?

I'm trying to change the image of the SKSpriteNode in my shop scene when an upgrade is bought, I believe I'm on the right track with the variable, but I think I am doing it wrong. Thanks for the help in advanced, I've been trying for a while to figure it out with no luck.
// ShopScene.swift
// TheLastFlight
//
// Created by -Zachary Walensa- on 8/3/16.
// Copyright © 2016 -Zachary Walensa-. All rights reserved.
//
import Foundation
import SpriteKit
var coinUpgrade = defaults.integer(forKey: "coinUpgradeSaved")
var missileUpgrade = defaults.integer(forKey: "missileUpgradeSaved")
class ShopScene: SKScene {
var coinLabel = SKLabelNode(fontNamed: "The Bold Font")
var missileUpgrades = SKSpriteNode(imageNamed: "missile1")
override func didMove(to view: SKView) {
coinLabel.text = "Coins: \(coinNumber)"
coinLabel.fontSize = 100
coinLabel.fontColor = SKColor.black
coinLabel.zPosition = 1
coinLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1)
self.addChild(coinLabel)
let background = SKSpriteNode(imageNamed: "background")
background.size = self.size
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
let mainMenu = SKLabelNode(fontNamed: "The Bold Font")
mainMenu.text = "Main Menu"
mainMenu.fontSize = 100
mainMenu.fontColor = SKColor.darkGray
mainMenu.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.3)
mainMenu.zPosition = 1
mainMenu.name = "Main Menu"
self.addChild(mainMenu)
let Life = SKSpriteNode(imageNamed: "lifeButton")
Life.position = CGPoint(x: self.size.width*0.3, y: self.size.height*0.8)
Life.zPosition = 1
Life.name = "Life"
self.addChild(Life)
let coinUpgrades = SKSpriteNode(imageNamed: "coinUpgrade")
coinUpgrades.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.8)
coinUpgrades.zPosition = 1
coinUpgrades.name = "Coin"
self.addChild(coinUpgrades)
missileUpgrades.position = CGPoint(x: self.size.width*0.8, y: self.size.height*0.8)
missileUpgrades.zPosition = 1
missileUpgrades.name = "missile"
self.addChild(missileUpgrades)
/*let coinLabel = SKLabelNode(fontNamed: "The Bold Font")
coinLabel.text = "Coins: \(coinNumber)"
coinLabel.fontSize = 100
coinLabel.fontColor = SKColor.black
coinLabel.zPosition = 1
coinLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1)
self.addChild(coinLabel) */
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointOfTouch = touch.location(in: self)
let nodeITapped = atPoint(pointOfTouch)
if nodeITapped.name == "Main Menu" {
let sceneToMoveTo = MainMenuScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTrasition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTrasition)
}
if nodeITapped.name == "Life" {
if coinNumber >= 50 {
lifeNumber += 1
coinNumber -= 50
defaults.set(coinNumber, forKey: "coinNumberSaved")
defaults.set(lifeNumber, forKey: "lifeNumberSaved")
coinLabel.text = "Coins: \(coinNumber)"
}
}
if nodeITapped.name == "Coin" {
if coinNumber >= 600 {
coinNumber -= 600
defaults.set(coinNumber, forKey: "coinNumberSaved")
coinUpgrade = 1
defaults.set(coinUpgrade, forKey: "coinUpgradeSaved")
coinLabel.text = "Coins: \(coinNumber)"
}
}
if nodeITapped.name == "missile" {
if missileUpgrade == 2 {
missileUpgrade = 1
defaults.set(missileUpgrade, forKey: "missileUpgradeSaved")
if missileUpgrade == 1 {
missileUpgrade = 2
defaults.set(missileUpgrade, forKey: "missileUpgradeSaved")
}
}
else if missileUpgrade == 0 || coinNumber >= 1400 {
coinNumber -= 1400
defaults.set(coinNumber, forKey: "coinNumberSaved")
missileUpgrade = 2
defaults.set(missileUpgrade, forKey: "missileUpgradeSaved")
coinLabel.text = "Coins: \(coinNumber)"
missileUpgrades = SKSpriteNode(imageNamed: "missile")
}
}
}
}
}
There are a few ways to do this:
1) Create a Boolean variable that switches to true when the condition is met. Then in the update function, you can constantly check for if the condition is met:
var conditionIsMet = false
// Your code
//
//...
override func update(_ currentTime: CFTimeInterval) {
if conditionIsMet {
// Change your texture here
}
}
2) Where you get the trigger for when you want to change the texture, you can simply change the texture as part of the code/function that runs. That means that if you are trying to do this on a touch, you would add the code to change the texture there.
FYI: To change the texture just do something like this:
node.texture = SKTexture(imageNamed: "image name goes here")

Integer is not being updated

I am making a game that includes a high score label that comes up once the player dies, along with a restart button. Overall the high score, which is an integer, works fine but there is one problem. If you reach a new high score in that round you just finished you have to die again for it to show the new high score. Lets say I play the game while the high score is already 15 and I score 17 when the high score label comes up it still shows 15. After I restart the game and the high score comes up again it will now show 17. The high score is not updating when I want it to.
import SpriteKit
struct physicsCatagory {
static let person : UInt32 = 0x1 << 1
static let Ice : UInt32 = 0x1 << 2
static let IceTwo : UInt32 = 0x1 << 3
static let IceThree : UInt32 = 0x1 << 4
static let Score : UInt32 = 0x1 << 5
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var Highscore = Int()
var timeOfLastSpawn: CFTimeInterval = 0.0
var timePerSpawn: CFTimeInterval = 1.2
var scorenumber = Int()
var lifenumber = Int()
var SpeedNumber : Double = 0.5
var person = SKSpriteNode(imageNamed: "Person1")
let Score = SKSpriteNode()
var ScoreLable = SKLabelNode()
var Highscorelabel = SKLabelNode()
let BackGround = SKSpriteNode (imageNamed: "BackGround")
var restartButton = SKSpriteNode()
var Died = Bool()
func restartScene(){
self.removeAllChildren()
self.removeAllActions()
scorenumber = 0
lifenumber = 0
createScene()
random()
//spawnThirdIce()
Died = false
timeOfLastSpawn = 0.0
timePerSpawn = 1.2
}
func createScene(){
physicsWorld.contactDelegate = self
if (scorenumber > Highscore){
var Highscoredefault = NSUserDefaults.standardUserDefaults()
Highscoredefault.setValue(scorenumber, forKey: "HighScore")
}
var Highscoredefault = NSUserDefaults.standardUserDefaults()
if (Highscoredefault.valueForKey("HighScore") != nil){
Highscore = Highscoredefault.valueForKey("HighScore") as! NSInteger
}
else{
Highscore = 0
}
lifenumber = 0
SpeedNumber = 1
BackGround.size = CGSize(width: self.frame.width, height: self.frame.height)
BackGround.position = CGPointMake(self.size.width / 2, self.size.height / 2)
BackGround.zPosition = -5
self.addChild(BackGround)
Score.size = CGSize(width: 2563, height: 1)
Score.position = CGPoint(x: 320, y: -20)
Score.physicsBody = SKPhysicsBody(rectangleOfSize: Score.size)
Score.physicsBody?.affectedByGravity = false
Score.physicsBody?.dynamic = false
Score.physicsBody?.categoryBitMask = physicsCatagory.Score
Score.physicsBody?.collisionBitMask = 0
Score.physicsBody?.contactTestBitMask = physicsCatagory.IceThree
Score.color = SKColor.blueColor()
Score.zPosition = -5
self.addChild(Score)
person.zPosition = 1
person.position = CGPointMake(self.size.width/2, self.size.height/9.5)
person.setScale(0.6)
person.physicsBody = SKPhysicsBody (rectangleOfSize: CGSize(width: 1000, height: 50))
person.physicsBody?.affectedByGravity = false
person.physicsBody?.categoryBitMask = physicsCatagory.person
person.physicsBody?.contactTestBitMask = physicsCatagory.Ice
person.physicsBody?.collisionBitMask = physicsCatagory.Ice
person.physicsBody?.dynamic = false
person.physicsBody?.affectedByGravity = false
self.addChild(person)
ScoreLable = SKLabelNode()
ScoreLable.fontName = "Arial"
ScoreLable.position = CGPoint(x: self.frame.width / 2, y: 1700)
ScoreLable.text = "\(scorenumber)"
ScoreLable.fontColor = UIColor.yellowColor()
ScoreLable.fontSize = 150
self.addChild(ScoreLable)
Highscorelabel.fontName = "Arial"
Highscorelabel.position = CGPoint(x: self.frame.width / 2, y: 1400)
Highscorelabel.text = "HighScore: \(Highscore)"
Highscorelabel.fontSize = 150
Highscorelabel.fontColor = UIColor.yellowColor()
Highscorelabel.zPosition = -7
self.addChild(Highscorelabel)
}
func random() -> CGFloat{
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(min min: CGFloat, max: CGFloat) -> CGFloat{
return random() * (max - min) + min
}
var gameArea: CGRect
override init(size: CGSize) {
let maxAspectRatio: CGFloat = 16.0/9.0
let playableWidth = size.height / maxAspectRatio
let margin = (size.width - playableWidth) / 2
gameArea = CGRect(x: margin, y: 0, width: playableWidth, height: size.height)
super.init(size: size)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func didMoveToView(view: SKView) {
createScene()
}
func createButton(){
restartButton = SKSpriteNode(imageNamed: "Restart Button")
restartButton.position = CGPoint(x: 768, y: 1024)
restartButton.zPosition = 6
restartButton.setScale(2.3)
self.addChild(restartButton)
}
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == physicsCatagory.person && secondBody.categoryBitMask == physicsCatagory.IceThree || firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.person{
scorenumber++
if scorenumber == 20 {
timePerSpawn = 1.0
}
if scorenumber == 40{
timePerSpawn = 0.89
}
if scorenumber == 60{
timePerSpawn = 0.6
}
if scorenumber == 80{
timePerSpawn = 0.5
}
if scorenumber == 100{
timePerSpawn = 0.4
}
if scorenumber == 120{
timePerSpawn = 0.3
}
ScoreLable.text = "\(scorenumber)"
CollisionWithPerson(firstBody.node as! SKSpriteNode, Person: secondBody.node as! SKSpriteNode)
}
if firstBody.categoryBitMask == physicsCatagory.Score && secondBody.categoryBitMask == physicsCatagory.IceThree ||
firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.Score{
lifenumber++
if lifenumber == 1{
//person.texture
person.texture = SKTexture (imageNamed: "Flower#2")
}
if lifenumber == 2{
person.texture = SKTexture (imageNamed: "Flower#3")
}
if lifenumber == 3{
// self.addChild(Highscorelabel)
Highscorelabel.zPosition = 5
createButton()
person.zPosition = -6
person.texture = SKTexture (imageNamed: "Person1")
//person.removeFromParent()
Died = true
}
}
}
func CollisionWithPerson (Ice: SKSpriteNode, Person: SKSpriteNode){
Person.removeFromParent()
// if (scorenumber > Highscore){
// var Highscoredefault = NSUserDefaults.standardUserDefaults()
// Highscoredefault.setValue(scorenumber, forKey: "HighScore")
//}
}
func spawnThirdIce(){
if Died == true {
} else
{
var Ice = SKSpriteNode(imageNamed: "Ice")
Ice.zPosition = 2
Ice.setScale(1.5)
Ice.physicsBody = SKPhysicsBody(rectangleOfSize: Ice.size)
Ice.physicsBody?.categoryBitMask = physicsCatagory.IceThree
Ice.physicsBody?.contactTestBitMask = physicsCatagory.person | physicsCatagory.Score
Ice.physicsBody?.affectedByGravity = false
Ice.physicsBody?.dynamic = true
let randomXStart = random(min:CGRectGetMinX(gameArea), max: CGRectGetMaxX(gameArea))
let randomXend = random(min:CGRectGetMinX(gameArea),max: CGRectGetMaxX(gameArea))
let startPoint = CGPoint(x: randomXStart, y: self.size.height * 1.2)
let endpoint = CGPoint(x: randomXend, y: -self.size.height * 0.2)
Ice.position = startPoint
let moveEnemy = SKAction.moveTo(endpoint, duration: 2.0)
let deleteEnemy = SKAction.removeFromParent()
let enemySequence = SKAction.sequence([moveEnemy , deleteEnemy])
Ice.runAction(enemySequence)
self.addChild(Ice)
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches{
let location = touch.locationInNode(self)
if Died == true{
if restartButton.containsPoint(location){
restartScene()
}
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if Died == true {
}
else{
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let previousTouch = touch.previousLocationInNode(self)
let ammountDragged = location.x - previousTouch.x
person.position.x += ammountDragged
if person.position.x > CGRectGetMaxX(gameArea) - person.size.width/2{
person.position.x = CGRectGetMaxX(gameArea) - person.size.width/2
}
if person.position.x < CGRectGetMinX(gameArea) + person.size.width/2{
person.position.x = CGRectGetMinX(gameArea) + person.size.width/2
}
}
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if (currentTime - timeOfLastSpawn > timePerSpawn) {
spawnThirdIce()
self.timeOfLastSpawn = currentTime
}
}
}
Ok this is what I added to if life number = 3:
if lifenumber == 3{
if (scorenumber > Highscore){
var Highscoredefault = NSUserDefaults.standardUserDefaults()
Highscoredefault.setValue(scorenumber, forKey: "HighScore")
}
var Highscoredefault = NSUserDefaults.standardUserDefaults()
if (Highscoredefault.valueForKey("HighScore") != nil){
Highscore = Highscoredefault.valueForKey("HighScore") as! NSInteger
}
else{
Highscore = 0
}
self.addChild(Highscorelabel)
createButton()
person.zPosition = -6
person.texture = SKTexture (imageNamed: "Person1")
Died = true
}
It appears you only set the text property of Highscorelabel when you initialize it. If you want the Highscorelabel to be updated immediately upon death then you should update it at such time.
if lifenumber == 3 {
/* Check if new highscore, update HighscoreLabel */
I think you must add just this:
after scorenumber++ in func didBeginContact(contact: SKPhysicsContact)
//ADD THIS
if scorenumber > Highscore {
Highscore = scorenumber
Highscorelabel.text = "HighScore: \(Highscore)"
}

didBeginContact not working in Swift 2 + SpriteKit

I'm working on a game, and I'm using spritekit and Swift.
This is my code:
import SpriteKit
struct collision {
static let arrow:UInt32 = 0x1 << 1
static let runner:UInt32 = 0x1 << 2
static let target:UInt32 = 0x1 << 3
static let targetCenter:UInt32 = 0x1 << 4
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var person = SKSpriteNode()
var box = SKSpriteNode()
var screenSize:CGSize!
var gameScreenSize:CGSize!
var gameStarted:Bool = false
var moveAndRemove = SKAction()
var boxVelocity:NSTimeInterval = 5.5
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, -1.0)
self.physicsWorld.contactDelegate = self
screenSize = self.frame.size
gameScreenSize = view.frame.size
createPerson()
}
func createPerson() -> Void {
person.texture = SKTexture(imageNamed:"person")
person.setScale(1.0)
person.size = CGSize(width: 80, height: 80)
person.position = CGPoint(x: screenSize.width / 2, y: 150)
person.physicsBody = SKPhysicsBody(rectangleOfSize: person.size)
person.physicsBody?.affectedByGravity = false
person.physicsBody?.dynamic = false
self.addChild(person)
}
func createTarget() -> Void {
box = SKSpriteNode()
box.size = CGSize(width: 70, height: 100)
box.setScale(1.0)
box.position = CGPoint(x: (screenSize.width / 3) * 2, y: screenSize.height + box.size.height)
box.texture = SKTexture(imageNamed: "box")
box.physicsBody? = SKPhysicsBody(rectangleOfSize: box.size)
box.physicsBody?.categoryBitMask = collision.target
box.physicsBody?.collisionBitMask = collision.arrow
box.physicsBody?.contactTestBitMask = collision.targetCenter
box.physicsBody?.affectedByGravity = false
box.physicsBody?.dynamic = true
box.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(box)
let distance = CGFloat(self.frame.height - box.frame.height)
let moveTargets = SKAction.moveToY(-distance, duration: boxVelocity)
let removeTargets = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([moveTargets,removeTargets])
box.runAction(moveAndRemove)
}
func createBall() ->Void {
let ball = SKSpriteNode()
ball.size = CGSize(width: 20, height: 22)
ball.zPosition = 5
let moveToXY = CGPoint(x: self.size.width, y: self.size.height)
ball.texture = SKTexture(imageNamed: "ball")
ball.position = CGPointMake(person.position.x + ball.size.width, person.position.y + ball.size.height)
ball.physicsBody? = SKPhysicsBody(rectangleOfSize: ball.size)
ball.physicsBody?.categoryBitMask = collision.arrow
ball.physicsBody?.collisionBitMask = collision.target
ball.physicsBody?.affectedByGravity = false
ball.physicsBody?.dynamic = true
ball.physicsBody?.usesPreciseCollisionDetection = true
let action = SKAction.moveTo(moveToXY, duration: 1.5)
let delay = SKAction.waitForDuration(1.5)
ball.runAction(SKAction.sequence([action,delay]))
self.addChild(ball)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if gameStarted == false {
gameStarted = true
let spawn = SKAction.runBlock { () in
self.createTarget()
}
let delay = SKAction.waitForDuration(1.5)
let spawnDelay = SKAction.sequence([spawn, delay])
let spanDelayForever = SKAction.repeatActionForever(spawnDelay)
self.runAction(spanDelayForever)
} else {
createBall()
boxVelocity -= 0.1
}
}
func didBeginContact(contact: SKPhysicsContact) {
print("Detect")
}
func didEndContact(contact: SKPhysicsContact) {
print("end detect")
}
override func update(currentTime: CFTimeInterval) {
}
}
But when I run the game, the collision between objects does not. I'm trying to solve a while, but found nothing. Can someone help me?
Project files.
Try with these to modifications:
box.physicsBody = SKPhysicsBody(rectangleOfSize: box.size)
box.physicsBody?.categoryBitMask = collision.target
box.physicsBody?.collisionBitMask = collision.arrow
box.physicsBody?.contactTestBitMask = collision.targetCenter
and
ball.physicsBody = SKPhysicsBody(rectangleOfSize: ball.size)
ball.physicsBody?.categoryBitMask = collision.arrow
ball.physicsBody?.collisionBitMask = collision.target
ball.physicsBody?.contactTestBitMask = collision.target
Note the absence of "?" while you init the physicsBody and the new contactTestBitMask