Swift3 game development

SKAction

    override func didMove(to view: SKView) {
        self.anchorPoint = .zero
        let mySprite = SKSpriteNode(color: .blue, size:
            CGSize(width: 50, height: 50))
        mySprite.position = CGPoint(x: 150, y: 150)
        
        //let demoAction = SKAction.move(to: CGPoint(x: 300, y:150),duration:3)
        //let demoAction = SKAction.scale(to: 4, duration:5)
        let demoAction1 = SKAction.scale(to: 4, duration:5)
        let demoAction2 = SKAction.rotate(byAngle: 5, duration:5)
        //let actionGroup = SKAction.group([demoAction1, demoAction2])
        let actionSequence = SKAction.sequence([demoAction1, demoAction2])
        mySprite.run(actionSequence)
        
        self.addChild(mySprite)
  • bee-fly
        let bee = SKSpriteNode(imageNamed: "bee-fly")
        bee.size = CGSize(width:28,height:24)
        bee.position = CGPoint(x:250,y:250)
        
        self.addChild(bee)
  • bee never ending flight
    override func didMove(to view: SKView) {
        self.anchorPoint = .zero
        self.backgroundColor = UIColor(red: 0.4, green: 0.6, blue:0.95, alpha:1.0)
        
        let bee = SKSpriteNode()
        bee.position = CGPoint(x:250,y:250)
        bee.size = CGSize(width:28,height:24)
        self.addChild(bee)
        
        let beeAtlas = SKTextureAtlas(named:"Enemies")
        let beeFrames:[SKTexture] = [
            beeAtlas.textureNamed("bee"),
            beeAtlas.textureNamed("bee-fly")
        ]
        let flyAction = SKAction.animate(with:beeFrames,timePerFrame:0.14)
        let beeAction = SKAction.repeatForever(flyAction)
        bee.run(beeAction)
        let pathLeft = SKAction.moveBy(x:-200,y:-10,duration:2)
        let pathRight = SKAction.moveBy(x:200,y:10, duration:2)
        let flipTextureNegative = SKAction.scaleX(to:-1,duration:0)
        let flipTexturePositive = SKAction.scaleX(to:1, duration:0)
        let flightOfTheBee = SKAction.sequence([pathLeft,flipTextureNegative, pathRight, flipTexturePositive])
        let neverEndingFlight = SKAction.repeatForever(flightOfTheBee)
        bee.run(neverEndingFlight)
    }
  • bee2, bee3
import SpriteKit
import GameplayKit

class GameScene: SKScene {
    let cam = SKCameraNode()
    let ground = Ground()
    let bee = SKSpriteNode()

    override func didMove(to view: SKView) {
        self.anchorPoint = .zero
        self.backgroundColor = UIColor(red: 0.4, green: 0.6, blue:0.95, alpha:1.0)
        
        self.camera = cam
        self.addTheFlyingBee()
        let bee2 = Bee()
        bee2.position = CGPoint(x: 325, y:325)
        self.addChild(bee2)
        let bee3 = Bee()
        bee3.position = CGPoint(x:200, y:325)
        self.addChild(bee3)
        
        ground.position = CGPoint(x:-self.size.width*2, y:150)
        ground.size = CGSize(width:self.size.width*6, height:0)
        ground.createChildren()
        self.addChild(ground)
    }
    
    override func didSimulatePhysics() {
        self.camera!.position = bee.position
    }
    
    func addTheFlyingBee() {
        bee.position = CGPoint(x: 250, y:250)
        bee.size = CGSize(width: 28, height:24)
        self.addChild(bee)
        let beeAtlas = SKTextureAtlas(named:"Enemies")
        let beeFrames:[SKTexture] = [
            beeAtlas.textureNamed("bee"), beeAtlas.textureNamed("bee-fly")
        ]
        let flyAction = SKAction.animate(with: beeFrames, timePerFrame: 0.14)
        let beeAction = SKAction.repeatForever(flyAction)
        bee.run(beeAction)
        let pathLeft = SKAction.moveBy(x:-200,y:-10,duration:2)
         let pathRight = SKAction.moveBy(x:200,y:10, duration:2)
         let flipTextureNegative = SKAction.scaleX(to:-1,duration:0)
         let flipTexturePositive = SKAction.scaleX(to:1, duration:0)
         let flightOfTheBee = SKAction.sequence([pathLeft,flipTextureNegative, pathRight, flipTexturePositive])
         let neverEndingFlight = SKAction.repeatForever(flightOfTheBee)
         bee.run(neverEndingFlight)
    }
 }
  • didSimulatePhysics
   override func didSimulatePhysics() {
        var cameraYPos = screenCenterY
        cam.yScale = 1
        cam.xScale = 1
        if (player.position.y > screenCenterY) {
            cameraYPos = player.position.y
            let percentOfMaxHeight = (player.position.y - screenCenterY)
                / (player.maxHeight - screenCenterY)
            let newScale = 1 + percentOfMaxHeight
            cam.yScale = newScale
            cam.xScale = newScale
        }
        self.camera!.position = CGPoint(x:player.position.x, y:cameraYPos)
        
        playerProgress = player.position.x - initialPlayerPosition.x
        ground.checkForReposition(playerProgress:playerProgress)
        if player.position.x > nextEncounterSpawnPosition {
            encounterManager.placeNextEncounter(currentXPos:nextEncounterSpawnPosition)
            nextEncounterSpawnPosition += 1200
            //let starRoll = Int(arc4random_uniform(10))
            let starRoll = 0
            if starRoll == 0 {
                if abs(player.position.x - powerUpStar.position.x) > 1200 {
                    let randomYPos = 50 + CGFloat(arc4random_uniform(400))
                    powerUpStar.position = CGPoint(x:nextEncounterSpawnPosition, y:randomYPos)
                    powerUpStar.physicsBody?.angularVelocity = 0
                    powerUpStar.physicsBody?.velocity = CGVector(dx:0,dy:0)
                    
                }
            }
...
  • physicsbody
       bee2.physicsBody?.applyImpulse(CGVector(dx:-25,dy:0))
        bee2.physicsBody?.mass = 0.2
  • bat,madFly,bronzeCoin,goldCoin,star
        let bat = Bat()
        bat.position = CGPoint(x:400,y:200)
        self.addChild(bat)
        let blade = Blade()
        blade.position = CGPoint(x:300,y:76)
        self.addChild(blade)
        
        let madFly = MadFly()
        madFly.position = CGPoint(x:50,y:50)
        self.addChild(madFly)
        
        let bronzeCoin = Coin()
        bronzeCoin.position = CGPoint(x:-50,y:250)
        self.addChild(bronzeCoin)
        
        let goldCoin = Coin()
        goldCoin.position = CGPoint(x:25, y:250)
        goldCoin.turnToGold()
        self.addChild(goldCoin)
        
        let star = Star()
        star.position = CGPoint(x:250, y:250)
        self.addChild(star)

CoreMotion

class GameScene: SKScene {
    let cam = SKCameraNode()
    let ground = Ground()
    let player = Player()
    let motionManager = CMMotionManager()
    
    override func update(_ currentTime: TimeInterval){
        player.update()
        if let accleData = self.motionManager.accelerometerData {
            var forceAmount:CGFloat
            var movement = CGVector()
            switch UIApplication.shared.statusBarOrientation {
            case .landscapeLeft:
                forceAmount = 20000
            case .landscapeRight:
                forceAmount = -20000
            default:
                forceAmount = 0
            }
            if accleData.acceleration.y > 0.15 {
                movement.dx = forceAmount
            }
            else if accleData.acceleration.y < -0.15 {
                movement.dx = forceAmount
            }
            player.physicsBody?.applyForce(movement)
        }
    }
    
    override func didMove(to view: SKView) {
        self.anchorPoint = .zero
        self.backgroundColor = UIColor(red: 0.4, green: 0.6, blue:0.95, alpha:1.0)
        
        self.camera = cam
        
        let bee2 = Bee()
        bee2.position = CGPoint(x: 325, y:325)
        self.addChild(bee2)
        let bee3 = Bee()
        bee3.position = CGPoint(x:200, y:325)
        self.addChild(bee3)
        
        ground.position = CGPoint(x:-self.size.width*2, y:30)
        ground.size = CGSize(width:self.size.width*6, height:0)
        ground.createChildren()
        self.addChild(ground)
        
        player.position = CGPoint(x:150, y:250)
        self.addChild(player)
        //bee2.physicsBody?.applyImpulse(CGVector(dx:-25,dy:0))
        //bee2.physicsBody?.mass = 0.2
        
        self.motionManager.startAccelerometerUpdates()
        
    }
    
    override func didSimulatePhysics() {
        self.camera!.position = player.position
    }
 }

  • flyanimation
    init() {
        // Call the init function on the base class (SKSpriteNode)
        super.init(texture: nil, color: .clear, size: initialSize)
        
        createAnimations()
        // If we run an action with a key, "flapAnimation",
        // we can later reference that key to remove the action.
        self.run(flyAnimation, withKey: "flapAnimation")
        let bodyTexture = textureAtlas.textureNamed("pierre-flying-3")
        self.physicsBody = SKPhysicsBody(texture: bodyTexture, size:self.size)
        self.physicsBody?.linearDamping = 0.9
        self.physicsBody?.mass = 30
        self.physicsBody?.allowsRotation = false

GameViewController

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
            if let scene = GameScene(fileNamed: "GameScene") {
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFill
                // Size our scene to fit the view exactly:
                scene.size = view.bounds.size
                // Show the new scene:
                view.presentScene(scene)
            }

            view.ignoresSiblingOrder = true
            view.showsFPS = true
            view.showsNodeCount = true
        }
    }
...

Crate

    func explode() {
        // Do not do anything if this crate already exploded:
        if exploded { return }
        exploded = true
        
        // Prevent additional contact:
        self.physicsBody?.categoryBitMask = 0
    }