SpriteKit: SKShapeNodes overlapping - ios

I just created a very basic scene in SpriteKit and first problem just appeared.
When two SKShapeNode get in contact, the player node is overlapping the ground node, I can't see why.
Here is my code and a screenshot:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var contentCreated = false
var player: SKShapeNode?
override func didMoveToView(view: SKView) {
if !contentCreated {
createSceneContents()
contentCreated = true
}
}
func createSceneContents() {
self.backgroundColor = SKColor(white: 1.0, alpha: 1.0)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsWorld.gravity = CGVectorMake(0, -10)
self.physicsWorld.contactDelegate = self
let groundCategory: UInt32 = 0x1 << 0
let playerCategory: UInt32 = 0x1 << 0
let ground = SKShapeNode(rect: CGRectMake(0, 0, self.frame.width, 50))
ground.fillColor = UIColor.blueColor()
ground.lineWidth = 0
ground.position = CGPoint(x: 0, y: 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.frame.size)
ground.physicsBody?.dynamic = false
ground.physicsBody?.categoryBitMask = groundCategory
ground.physicsBody?.collisionBitMask = groundCategory
ground.physicsBody?.contactTestBitMask = groundCategory
self.addChild(ground)
player = SKShapeNode(rect: CGRectMake(0, 0, 30, 30))
player?.fillColor = UIColor.blackColor()
player?.lineWidth = 0
player?.position = CGPoint(x: 20, y: 400)
player?.physicsBody = SKPhysicsBody(rectangleOfSize: player!.frame.size)
player?.physicsBody?.mass = 1.0
player?.physicsBody?.categoryBitMask = playerCategory
player?.physicsBody?.collisionBitMask = playerCategory
player?.physicsBody?.contactTestBitMask = playerCategory
self.addChild(player!)
}
}
Thanks.

You might want to have a different bitmask for your ground and player :
let groundCategory: UInt32 = 0x1
let playerCategory: UInt32 = 0x1 << 1
Then you might also want to set the collision / contact bitmask differently :
ground.physicsBody?.categoryBitMask = groundCategory
ground.physicsBody?.collisionBitMask = playerCategory
ground.physicsBody?.contactTestBitMask = playerCategory
player?.physicsBody?.categoryBitMask = playerCategory
player?.physicsBody?.collisionBitMask = groundCategory
player?.physicsBody?.contactTestBitMask = groundCategory
Even so, you'll have something like you already have. However if you turn on the showPhysics (as suggested by #Skoua) :
You can see that the physics body is attached to the bottom left anchor point.
Here is how to fix that :
func createSceneContents() {
self.backgroundColor = SKColor(white: 1.0, alpha: 1.0)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsWorld.gravity = CGVectorMake(0, -10)
self.physicsWorld.contactDelegate = self
let groundCategory: UInt32 = 0x1
let playerCategory: UInt32 = 0x1 << 1
let ground = SKShapeNode(rectOfSize: CGSize(width: self.frame.width, height: 50)) // HERE
ground.fillColor = UIColor.blueColor()
ground.lineWidth = 0
ground.position = CGPoint(x: ground.frame.size.width/2, y: ground.frame.size.height/2) // HERE
ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.frame.size)
ground.physicsBody?.dynamic = false
ground.physicsBody?.categoryBitMask = groundCategory
ground.physicsBody?.collisionBitMask = playerCategory
ground.physicsBody?.contactTestBitMask = playerCategory
self.addChild(ground)
player = SKShapeNode(rectOfSize: CGSize(width: 30, height: 30)) // HERE
player?.fillColor = UIColor.blackColor()
player?.lineWidth = 0
player?.position = CGPoint(x: 20, y: 400)
player?.physicsBody = SKPhysicsBody(rectangleOfSize: player!.frame.size)
player?.physicsBody?.mass = 1.0
player?.physicsBody?.categoryBitMask = playerCategory
player?.physicsBody?.collisionBitMask = groundCategory
player?.physicsBody?.contactTestBitMask = groundCategory
self.addChild(player!)
}
Don't forget, when placing your node, that the default anchor point is the bottom left of the scene.

Related

Physics body leaving circle player

My issue is similar to this questions (physics body not inline with shape?) except setting the center doesn't help. I've played around with it a bunch but I can't get the physics body to stay wrapped about the circle.
First issue in the first video.
Similar/new problem in second video
Here's my code for set up:
let sprite = SKSpriteNode(imageNamed: "smileyEmoji")
sprite.size = CGSize(width: 45, height: 45)
sprite.name = "player"
sprite.zPosition = 3
player.addChild(sprite)
player.position = CGPoint(x:85, y:140)
player.physicsBody?.isDynamic = false
player.physicsBody?.allowsRotation = true
player.physicsBody?.restitution = 0.7
player.physicsBody?.friction = 0.0
player.physicsBody?.angularDamping = 0.0
player.physicsBody?.linearDamping = 0.0
player.physicsBody?.usesPreciseCollisionDetection = true
player.physicsBody?.categoryBitMask = PhysicsCategory.CollisionCategoryPlayer
player.physicsBody?.collisionBitMask = PhysicsCategory.CollisionCategoryPlatform | PhysicsCategory.CollisionCategoryPoint
player.physicsBody?.contactTestBitMask = PhysicsCategory.CollisionCategoryPoint | PhysicsCategory.CollisionCategoryDevil
After springing:
player.physicsBody = SKPhysicsBody(circleOfRadius: (self.player.childNode(withName: "player") as! SKSpriteNode).size.width/2, center: CGPoint(x: 0, y: 0.0))
player.physicsBody?.isDynamic = true

How do I make an object bounce off the edges in swift

I am trying to make my 'Enemy' bounce off all the boundaries. For example, when the Enemy collides with the left, right, top and bottom of the screen, it should change direction.
This is my current code:
import SpriteKit
import GameplayKit
struct Physics {
static let Enemy: UInt32 = 0x1 << 1
let BorderCategory : UInt32 = 0x1 << 2
let BottomCategory : UInt32 = 0x1 << 3
let BallCategory : UInt32 = 0x1 << 4
}
class GameScene: SKScene {
var Enemy = SKSpriteNode()
var gameStarted = Bool()
var gameState = "running"
var destX : CGFloat = 0.0
var destY : CGFloat = 0.0
var score = 0
override func didMove(to view: SKView) {
let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)
borderBody.friction = 0
self.physicsBody = borderBody
let screenSize: CGRect = UIScreen.main().bounds // get the screen size
let screenWidth = screenSize.width //get the width
let screenHeight = screenSize.height //get the height
Enemy = SKSpriteNode(imageNamed: "red2")
Enemy.size = CGSize(width: 60, height: 70)
Enemy.position = (CGPoint(x: self.frame.width / 6 - Enemy.frame.width, y: self.frame.height / 10))
Enemy.physicsBody = SKPhysicsBody(circleOfRadius: Enemy.frame.height / 2)
Enemy.physicsBody?.categoryBitMask = Physics.Enemy
//Enemy.physicsBody?.categoryBitMask = Physics.Ground | Physics.wall
//Enemy.physicsBody?.contactTestBitMask = Physics.Ground | Physics.wall
Enemy.physicsBody?.affectedByGravity = false
Enemy.physicsBody?.isDynamic = true
self.addChild(Enemy)
Enemy.physicsBody?.velocity = CGVector(dx: 50, dy: 50)
if (Enemy.position.x == screenWidth) {
Enemy.physicsBody?.velocity = CGVector(dx: -50, dy: 0) // change direction at edge DOESN'T WORK
}
if(Enemy.position.y == screenHeight){
Enemy.physicsBody?.velocity = CGVector(dx: 0, dy: -50) //change direction at edge DOESN'T WORK
}
}
OK, based on your code I've written a working bouncing sprite. It uses the physics-engine to achieve this, rather than manually changing the direction (which I guess is how you should go about doing this).
override func didMove(to view: SKView) {
let borderBody = SKPhysicsBody(edgeLoopFrom: frame)
borderBody.friction = 0
borderBody.categoryBitMask = Physics.wall
physicsBody = borderBody
let enemy = enemySprite(size: CGSize(width: 60, height: 70), position: CGPoint(x: frame.size.width/2, y: frame.size.height/2))
addChild(enemy)
let force = SKAction.applyForce(CGVector(dx: 300, dy: 300) , duration: 0.1)
enemy.run(force)
}
func enemySprite(size: CGSize, position: CGPoint) -> SKSpriteNode {
let enemy = SKSpriteNode(color: SKColor.red(), size: CGSize(width: 60, height: 80))
enemy.position = CGPoint(x: frame.width/2, y: frame.height/2)
enemy.physicsBody = SKPhysicsBody(circleOfRadius: enemy.frame.height / 2) // A bit silly with circle, but you seem to have an image for this use
enemy.physicsBody?.categoryBitMask = Physics.enemy
enemy.physicsBody?.restitution = 1
enemy.physicsBody?.friction = 0
enemy.physicsBody?.collisionBitMask = Physics.wall
enemy.physicsBody?.affectedByGravity = false
enemy.physicsBody?.angularDamping = 0
enemy.physicsBody?.linearDamping = 0
return enemy
}
A question to consider is also how you set up the scene? Is the scene-size the same as the view-size? Otherwise the scene's physicsBody will not make the ball bounce where you expect it to...
It won't (in most cases) because of your conditions - using equal to operators instead of greater/lower than.
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
// Create enemy with physics body attached and add it to the scene
let enemy = SKSpriteNode(color: SKColor.greenColor(), size: CGSize(width: 20, height: 20))
enemy.name = "enemy"
enemy.position = CGPoint(x: size.width / 2, y: size.height / 2)
enemy.physicsBody = SKPhysicsBody(rectangleOfSize: enemy.size)
enemy.physicsBody!.affectedByGravity = false
enemy.physicsBody!.linearDamping = 0
enemy.physicsBody!.mass = 0.1
enemy.physicsBody!.velocity = CGVector(dx: 150, dy: 0)
addChild(enemy)
}
override func didSimulatePhysics() {
let enemy = childNodeWithName("enemy")!
if (enemy.position.x > size.width && enemy.physicsBody!.velocity.dx > 0)
|| (enemy.position.x < 0 && enemy.physicsBody!.velocity.dx < 0) {
enemy.physicsBody!.velocity.dx *= -1
}
}
}

Sprite not being affected by physics

I want a SKSpriteNode to keep moving through the screen and bounce with the edges forever. So I set the physics of the edges:
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
borderBody.friction = 0
self.physicsBody = borderBody
physicsWorld.gravity = CGVectorMake(0, 0)
And then the physics of the sprite:
ball.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
ball.size = CGSizeMake(30, 30)
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.restitution = 1.0
ball.physicsBody?.linearDamping = 0.0
ball.physicsBody?.friction = 0.0
ball.physicsBody?.applyImpulse(CGVectorMake(10, -10))
self.addChild(ball)
The problem is that the sprite is not being affected in anyway by the physics and stays on the center of the screen. What did I do wrong?
you need to add the ball to your scene before you apply the impulse to it.
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
borderBody.friction = 0
self.physicsBody = borderBody
physicsWorld.gravity = CGVectorMake(0, 0)
let ball = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(30, 30))
ball.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.restitution = 1.0
ball.physicsBody?.linearDamping = 0.0
ball.physicsBody?.friction = 0.0
self.addChild(ball)
ball.physicsBody?.applyImpulse(CGVectorMake(10, -10))
}
}

Building my first game in XCODE 7(swift 2)-CGPOINTMAKE?

I want to build a game similar to snooker, and at the beginning I already have some problems. I want to build four walls first (that will be the size of the screen-self.frame), and the first one I made like this:
let ground = SKNode()
ground.position = CGPointMake ( 0, 0)
ground.physicsBody = SKPhysicsBody( rectangleOfSize:CGSizeMake(self.frame.size.width * 3, 1))
ground.physicsBody!.dynamic = false
self.addChild(ground)
However, I don't known how to manipulate the values of CGPointMake to do the left/right/up walls. I first imagined that the (0,0) point was the left down corner, but it seems to not be like that. Can someone please help me with this or just explain how this works? (since in https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CGGeometry/index.html#//apple_ref/c/func/CGPointMake they dont explain very much =/)
Here is a basic example of how you can restrict the ball from leaving the screen:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let BallCategory : UInt32 = 0x1 << 1
let WallCategory : UInt32 = 0x1 << 2
let ball = SKShapeNode(circleOfRadius: 40)
override func didMoveToView(view: SKView) {
/* Setup your scene here */
physicsWorld.contactDelegate = self
setupWalls()
setupBall()
}
func setupWalls(){
physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)
physicsBody?.categoryBitMask = WallCategory
physicsBody?.contactTestBitMask = BallCategory
physicsBody?.collisionBitMask = BallCategory
}
func setupBall(){
ball.physicsBody = SKPhysicsBody(circleOfRadius: 40)
ball.fillColor = SKColor.whiteColor()
ball.name = "ball"
ball.physicsBody?.categoryBitMask = BallCategory
ball.physicsBody?.contactTestBitMask = WallCategory
ball.physicsBody?.collisionBitMask = WallCategory
ball.physicsBody?.dynamic = true //In order to detect contact between two bodies, at least on body has to be dynamic
ball.physicsBody?.affectedByGravity = false
ball.physicsBody?.restitution = 0.5
ball.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame)) // placing to ball in the middle of the screen
addChild(ball)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
ball.physicsBody?.applyImpulse(CGVector(dx: 200, dy: 200))
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
Try this for adding four walls with a thickness of twenty:
// Create walls
let leftWall = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 20, height: self.frame.height))
leftWall.fillColor = UIColor.whiteColor()
let physicsBodyLW = SKPhysicsBody(rectangleOfSize: CGSize(width: 20, height: self.frame.size.height), center: CGPoint(x:10, y:self.frame.height/2))
physicsBodyLW.affectedByGravity = false
physicsBodyLW.dynamic = false
leftWall.physicsBody = physicsBodyLW
self.addChild(leftWall)
let rightWall = SKShapeNode(rect: CGRect(x: self.frame.width - 20, y: 0, width: 20, height: self.frame.height))
rightWall.fillColor = UIColor.whiteColor()
let physicsBodyRW = SKPhysicsBody(rectangleOfSize: CGSize(width: 20, height: self.frame.size.height), center: CGPoint(x:10, y:self.frame.height/2))
physicsBodyRW.affectedByGravity = false
physicsBodyRW.dynamic = false
rightWall.physicsBody = physicsBodyRW
self.addChild(rightWall)
let topWall = SKShapeNode(rect: CGRect(x: 0, y: self.frame.height-20, width: self.frame.width, height: 20))
topWall.fillColor = UIColor.whiteColor()
let physicsBodyTW = SKPhysicsBody(rectangleOfSize: CGSize(width: self.frame.size.width, height: 20), center: CGPoint(x:self.frame.width/2, y:10))
physicsBodyTW.affectedByGravity = false
physicsBodyTW.dynamic = false
topWall.physicsBody = physicsBodyTW
self.addChild(topWall)
let bottomWall = SKShapeNode(rect: CGRect(x: 0, y: 0, width: self.frame.width, height: 20))
print(bottomWall.frame)
bottomWall.fillColor = UIColor.whiteColor()
let physicsBodyBW = SKPhysicsBody(rectangleOfSize: CGSize(width: self.frame.size.width, height: 20), center: CGPoint(x:self.frame.width/2, y:10))
physicsBodyBW.affectedByGravity = false
physicsBodyBW.dynamic = false
bottomWall.physicsBody = physicsBodyBW
self.addChild(bottomWall)

flappy bird clone Xcode 7 Swift 2 how to reset rotation

I learn how to create games on iOS. And now I create a Flappy bird clone.
Flappy Bird Clone
And I'm stack on that - when bird touch some pipe it begin rotate. And game of course is over. But when I start a new game Bird still rotates.
If I put this line
bird.physicsBody?.allowsRotation = false
in the GameScene.swift then bird stop rotate at all. That is not what I want. I want to allow rotation. But when start a new game Bird should be in default position and do not rotate.
What should I do to make it work? Thanks for help.
//
// GameScene.swift
// Flappy Bird
//
// Created by Admin on 10.10.15.
// Copyright (c) 2015 Alex. All rights reserved.
//
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var score = 0
var gameOver = false
var gameOverLabel = SKLabelNode()
var scoreLabel = SKLabelNode ()
var bird = SKSpriteNode()
var bg = SKSpriteNode()
var movingObjects = SKSpriteNode()
var labelContainer = SKSpriteNode()
var pipe1 = SKSpriteNode()
var pipe2 = SKSpriteNode()
enum ColliderType: UInt32 {
case Bird = 1
case Object = 2
case Gap = 4
}
func makeBg () {
let bgTexture = SKTexture(imageNamed: "bg.png")
let movebg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 10)
let replacebg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
let movebgForever = SKAction.repeatActionForever(SKAction.sequence([movebg,replacebg]))
for var i: CGFloat = 0; i<2; i++ {
bg = SKSpriteNode(texture: bgTexture)
bg.position = CGPoint(x: bgTexture.size().width / 2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
bg.zPosition = -5
bg.size.height = self.frame.height
bg.runAction(movebgForever)
movingObjects.addChild(bg)
}
}
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
self.addChild(movingObjects)
self.addChild(labelContainer)
makeBg()
scoreLabel.fontName = "Helvetica"
scoreLabel.fontSize = 60
scoreLabel.text = "0"
scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height - 70)
addChild(scoreLabel)
let birdTexture = SKTexture(imageNamed: "flappy1.png")
let birdTexture2 = SKTexture(imageNamed: "flappy2.png")
let animation = SKAction.animateWithTextures([birdTexture,birdTexture2], timePerFrame: 0.1)
let makeBirdFlap = SKAction.repeatActionForever(animation)
bird = SKSpriteNode(texture: birdTexture)
bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
bird.runAction(makeBirdFlap)
bird.physicsBody = SKPhysicsBody(circleOfRadius: birdTexture.size().height/2)
bird.physicsBody!.dynamic = true
bird.physicsBody!.categoryBitMask = ColliderType.Bird.rawValue
bird.physicsBody?.contactTestBitMask = ColliderType.Object.rawValue
bird.physicsBody?.collisionBitMask = ColliderType.Object.rawValue
self.addChild(bird)
var ground = SKNode()
ground.position = CGPointMake(0, 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.size.width, 1))
ground.physicsBody!.dynamic = false
ground.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
ground.physicsBody?.contactTestBitMask = ColliderType.Object.rawValue
ground.physicsBody?.collisionBitMask = ColliderType.Object.rawValue
self.addChild(ground)
_ = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("makePipes"), userInfo: nil, repeats: true)
}
func makePipes () {
let gapHeight = bird.size.height * 4
let movementAmount = arc4random() % UInt32(self.frame.size.height / 2)
let pipeOffset = CGFloat(movementAmount) - self.frame.size.height / 4
let movePipes = SKAction.moveByX(-self.frame.size.width * 2, y: 0, duration: NSTimeInterval (self.frame.size.width / 100))
let removePipes = SKAction.removeFromParent()
let moveAndRemovePipes = SKAction.sequence([movePipes,removePipes])
let pipeTexture1 = SKTexture(imageNamed: "pipe1.png")
let pipe1 = SKSpriteNode(texture: pipeTexture1)
pipe1.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + pipeTexture1.size().height/2 + gapHeight/2 + pipeOffset)
pipe1.runAction(moveAndRemovePipes)
pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: pipeTexture1.size())
pipe1.physicsBody?.dynamic = false
pipe1.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
pipe1.physicsBody?.contactTestBitMask = ColliderType.Object.rawValue
pipe1.physicsBody?.collisionBitMask = ColliderType.Object.rawValue
movingObjects.addChild(pipe1)
let pipeTexture2 = SKTexture(imageNamed: "pipe2.png")
let pipe2 = SKSpriteNode(texture: pipeTexture2)
pipe2.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) - pipeTexture2.size().height/2 - gapHeight/2 + pipeOffset)
pipe2.runAction(moveAndRemovePipes)
pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: pipeTexture2.size())
pipe2.physicsBody!.dynamic = false
pipe2.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
pipe2.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
pipe2.physicsBody!.collisionBitMask = ColliderType.Object.rawValue
movingObjects.addChild(pipe2)
var gap = SKNode()
gap.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + pipeOffset)
gap.runAction(moveAndRemovePipes)
gap.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(pipe1.size.width, gapHeight))
gap.physicsBody?.dynamic = false
gap.physicsBody!.categoryBitMask = ColliderType.Gap.rawValue
gap.physicsBody!.contactTestBitMask = ColliderType.Bird.rawValue
gap.physicsBody!.collisionBitMask = ColliderType.Gap.rawValue
movingObjects.addChild(gap)
}
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == ColliderType.Gap.rawValue || contact.bodyB.categoryBitMask == ColliderType.Gap.rawValue {
score++
scoreLabel.text = String(score)
} else {
if gameOver == false {
gameOver = true
self.speed = 0
gameOverLabel.fontName = "Helvetica"
gameOverLabel.fontSize = 30
gameOverLabel.text = "Game is over. Tap to play again."
gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
labelContainer.addChild(gameOverLabel)
}
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if gameOver == false {
bird.physicsBody!.velocity = CGVectorMake(0, 0)
bird.physicsBody!.applyImpulse(CGVectorMake(0, 50))
} else {
score = 0
scoreLabel.text = "0"
bird.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
bird.physicsBody?.velocity = CGVectorMake(0, 0)
bird.physicsBody!.allowsRotation = false
movingObjects.removeAllChildren()
makeBg()
self.speed = 1
gameOver = false
labelContainer.removeAllChildren()
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
An easy solution is to reset the bird's angular speed and rotation angle. Modify the code in touchesBegan which is invoked when game is over:
bird.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
bird.physicsBody?.velocity = CGVectorMake(0, 0)
bird.physicsBody?.angularVelocity = 0
bird.zRotation = 0

Resources