RPG TUTORIAL: fatal error: unexpectedly found nil while unwrapping an Optional V

Home 4 Forums Tutorial Help Any Other Tutorials RPG TUTORIAL: fatal error: unexpectedly found nil while unwrapping an Optional V

This topic contains 1 reply, has 2 voices, and was last updated by  Justin 1 year, 6 months ago.

  • May 28, 2017 at 2:30 am #167407

    Hello,

    At the Player config step,I got an issue with some animations

    The player sprite don’t move any more and when i made an attack i got an error : fatal error: unexpectedly found nil while unwrapping an Optional value

    This error concerning the nameAnimation

    I thought thoses issue are income files and maybe the GameDataplist

    Here the code :

    This one is like the attackArea class :

    //
    //  bomb.swift
    //  AlienOdissey
    //
    //  Created by jonathan sinien on 06/05/2017.
    //  Copyright © 2017 jonathan sinien. All rights reserved.
    //
    
    import Foundation
    import SpriteKit
    import GameplayKit
    
    //Bombe parce que dans un jeu de vaisseau sans bombe c'est nul  :)
    
    class bombSpec: SKSpriteNode {
        
        
        var animationName:String! = ""
        var scaleSize:CGFloat = 2
        
        func setupBomb()  {
            
            
            let body:SKPhysicsBody = SKPhysicsBody(circleOfRadius: self.frame.size.width / 2, center: CGPoint.zero)
            
            self.physicsBody = body
            
            
            body.isDynamic = true
            body.affectedByGravity = false
            body.allowsRotation = false
            
            self.physicsBody?.categoryBitMask = BodyType.bomb.rawValue
            self.physicsBody?.collisionBitMask = 0
            self.physicsBody?.contactTestBitMask = BodyType.item.rawValue
            
            explode()
            
            
            if ( animationName != "") {
            
            self.run(SKAction(named : animationName)! )
            }
        }
        
        
        func explode(){
            
            let grow:SKAction = SKAction.scale(by: scaleSize, duration: 0.5)
            let finish:SKAction = SKAction.run {
                self.removeFromParent()
                
            }
            
            let seq:SKAction = SKAction.sequence([grow, finish])
            self.run(seq)
            
            
        
        
        
        
        }
        
        
        
        
        
        
        
        
    }
    
    
    //
    //  GameScene_Gestures.swift
    //  AlienOdissey
    //
    //  Created by jonathan sinien on 09/05/2017.
    //  Copyright © 2017 jonathan sinien. All rights reserved.
    //
    
    import Foundation
    import SpriteKit
    
    extension GameScene  {
        
        
        
        func tappedView(_ sender:UITapGestureRecognizer) {
            
            let point:CGPoint = sender.location(in: self.view)
            
            print(point)
            
            if (disableAttack == false){
                
                if ( attackAnywhere == true){
                    
                    attack()
                } else {
                    
                    if (point.x > self.view!.bounds.width / 2) {
                        
                        attack()
                        
                    }
                    
                    
                    
                }
                
                
                
                
            }
            
        }
        
        func tappedView2(_ sender:UITapGestureRecognizer) {
            
            
            var proceedToAttack:Bool = false
            
            let point:CGPoint = sender.location(in: self.view)
            
            
            if (disableAttack == false){
                
                if ( attackAnywhere == true){
                    
                    proceedToAttack = true
                    
                } else {
                    
                    if (point.x > self.view!.bounds.width / 2) {
                        
                        proceedToAttack =  true
                        
                    }
                    
                }
                
                
            }
            
            
            
            if (proceedToAttack == true){
                
                if (thePlayer.currentProjectile != ""){
                    
                    if ( prevPlayerProjectileName == thePlayer.currentProjectile) {
                        
                        // go straight to attacking with prev proj dict
                        
                        print ( "reusing projectile data")
                        
                        rangedAttackWithDict(theDict:prevPlayerProjectileDict)
                        
                    } else {
                        
                        for (key, value) in projectilesDict {
                            
                            if (key == thePlayer.currentProjectile) {
                                
                                
                                print ( "found projectile data")
                                prevPlayerProjectileName = key
                                prevPlayerProjectileDict = value as! [String : Any]
                                
                                for (key, value) in prevPlayerProjectileDict {
                                    
                                    if (key == "Image") {
                                        
                                        if (value is String) {
                                            
                                            prevPlayerProjectileImageName = value as! String
                                            
                                            
                                        }
                                        break
                                        
                                    }
                                    
                                    
                                }
                                
                                rangedAttackWithDict(theDict:prevPlayerProjectileDict)
                                
                                // create ranged atttack
                                break
                                
                            }
                            
                            
                        }
                        
                    }
                    
                    
                }
                
            }
            
        }
        
        
        func swipedRight() {
            
            
            
            move(theXAmount: 100, theYAmount: 0, theAnimation: "WalkRight")
        }
        
        func swipedLeft() {
            
            
            
            move(theXAmount: -100, theYAmount: 0, theAnimation: "WalkLeft")
        }
        
        func swipedUp() {
            
            
            
            move(theXAmount: 0, theYAmount: 100, theAnimation: "WalkBack")
        }
        
        func swipedDown() {
            
            
            
            move(theXAmount: 0, theYAmount: -100, theAnimation: "WalkFront")
            
            
        }
        
        
        
        
        
        func cleanUp(){
            
            //only need to call when presenting a different scene class
            
            for gesture in (self.view?.gestureRecognizers)! {
                
                self.view?.removeGestureRecognizer(gesture)
            }
            
            
        }
        
        
        
        func rotatedView(_ sender:UIRotationGestureRecognizer) {
            
            if (sender.state == .began) {
                
                print("rotation began")
                
            }
            if (sender.state == .changed) {
                
                print("rotation changed")
                
                //print(sender.rotation)
                
                let rotateAmount = Measurement(value: Double(sender.rotation), unit: UnitAngle.radians).converted(to: .degrees).value
                print(rotateAmount)
                
                thePlayer.zRotation = -sender.rotation
                
            }
            if (sender.state == .ended) {
                
                print("rotation ended")
                
                
            }
            
            
        }
        
        
        
        
        
    }
    
    //
    //  GameScene_Player.swift
    //  AlienOdissey
    //
    //  Created by jonathan sinien on 09/05/2017.
    //  Copyright © 2017 jonathan sinien. All rights reserved.
    //
    
    import Foundation
    import SpriteKit
    
    extension GameScene  {
        
        
        
        func move(theXAmount:CGFloat , theYAmount:CGFloat, theAnimation:String )  {
            
            
            let wait:SKAction = SKAction.wait(forDuration: 0.05)
            
            let walkAnimation:SKAction = SKAction(named: theAnimation, duration: moveSpeed )!
            
            let moveAction:SKAction = SKAction.moveBy(x: theXAmount, y: theYAmount, duration: moveSpeed )
            
            let group:SKAction = SKAction.group( [ walkAnimation, moveAction ] )
            
            let finish:SKAction = SKAction.run {
                
                //print ( "Finish")
                
                
            }
            
            
            let seq:SKAction = SKAction.sequence( [wait, group, finish] )
            
            
            
            thePlayer.run(seq)
            
        }
        
        
        //MARK: ============= ATTACK
        
        func attack(){
            
            print("gestures")
            
            let newAttack:bombSpec = bombSpec(imageNamed: "BlueBulletExplo")
            newAttack.position = thePlayer.position
            newAttack.scaleSize = thePlayer.meleeScaleSize
            newAttack.animationName = thePlayer.meleeAnimationFXName
            
            newAttack.setupBomb()
            self.addChild(newAttack)
            newAttack.zPosition = thePlayer.zPosition - 1
            
            
            var animationName:String = ""
            
            if (playerFacing == .front){
                
                animationName = thePlayer.frontMelee
                
            } else if (playerFacing == .back){
                
                newAttack.xScale = -1
                newAttack.yScale = -1
                
                animationName = thePlayer.backMelee
                
            } else if (playerFacing == .right){
                
                animationName = thePlayer.rightMelee
                
            } else if (playerFacing == .left){
                newAttack.xScale = -1
                animationName = thePlayer.leftMelee
            }
            
            
            
            let attackAnimation:SKAction = SKAction(named: animationName)!
            
            let finish:SKAction = SKAction.run {
                
                
                
                
                self.runIdleAnimation()
                
            }
            
            let seq:SKAction = SKAction.sequence( [ attackAnimation, finish] )
            
            thePlayer.run(seq, withKey: "Attack" )
            
        }
        
        
        func rangedAttackWithDict(theDict:[String : Any]){
            
            print ( theDict)
            
            let newProjectile:Projectile = Projectile(imageNamed: prevPlayerProjectileImageName)
            newProjectile.position = thePlayer.position
            newProjectile.setUpWithDict(theDict: theDict)
            
            self.addChild(newProjectile)
            
            var moveAction:SKAction = SKAction()
            
            var theDistance:CGFloat = 0
            
            if ( newProjectile.distance > 0) {
                
                theDistance = newProjectile.distance
                
            } else {
                
                if ( playerFacing == .front || playerFacing == .back){
                    
                    theDistance = (self.view?.bounds.size.height)!
                    
                } else {
                    
                    theDistance = (self.view?.bounds.size.width)!
                }
                
                
            }
            
            
            
            
            if ( playerFacing == .front){
                
                moveAction = SKAction.moveBy(x: 0, y: -theDistance, duration: newProjectile.travelTime)
                
            } else if ( playerFacing == .back){
                
                moveAction = SKAction.moveBy(x: 0, y: theDistance, duration: newProjectile.travelTime)
                
            } else if ( playerFacing == .left){
                
                moveAction = SKAction.moveBy(x: -theDistance, y: 0 , duration: newProjectile.travelTime)
                
            } else if ( playerFacing == .right){
                
                moveAction = SKAction.moveBy(x: theDistance, y: 0, duration: newProjectile.travelTime)
                
            }
            
            moveAction.timingMode = .easeOut
            
            let finish:SKAction = SKAction.run {
                
                if (newProjectile.removeAfterThrow == true) {
                    
                    newProjectile.removeFromParent()
                }
                
                
            }
            
            let seq:SKAction = SKAction.sequence([moveAction, finish ])
            
            newProjectile.run(seq)
            
            if (newProjectile.rotationTime > 0) {
                
                let randomAddOn:UInt32 = arc4random_uniform(10)
                let addOn:CGFloat = CGFloat( randomAddOn / 10)
                let rotateAction:SKAction = SKAction.rotate(byAngle: 6.28319 + addOn, duration: newProjectile.rotationTime)
                let repeatRotate:SKAction = SKAction.repeat(rotateAction, count: Int(newProjectile.travelTime / newProjectile.rotationTime))
                
                newProjectile.run(repeatRotate)
                
                
            }
            
            
            if ( walkWithPath == false) {
                
                touchingDown = false
                
                thePlayer.removeAction(forKey: thePlayer.backWalk)
                thePlayer.removeAction(forKey: thePlayer.leftWalk)
                thePlayer.removeAction(forKey: thePlayer.rightWalk)
                thePlayer.removeAction(forKey: thePlayer.frontWalk)
                
            }
            
            
            
        }
        
        
        
        
        //MARK: SOME MATH
        
        func getDifference(point:CGPoint) -> CGPoint{
            
            let newPoint:CGPoint = CGPoint(x: point.x + currentOffset.x, y: point.y + currentOffset.y)
            
            return newPoint
        }
        
        
        //MARK: TOUCH DOWN
        
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            
            
            
            for t in touches {
                
                if ( walkWithPath == true) {
                    
                    self.touchDown(atPoint: t.location(in: self))
                    
                    
                } else {
                    
                    self.touchDown2(atPoint: t.location(in: self))
                    
                }
                
                break
                
            }
        }
        
        func touchDown(atPoint pos : CGPoint) {
            
            //Touch down for walking with path
            
            pathArray.removeAll()
            
            currentOffset = CGPoint(x: thePlayer.position.x - pos.x, y:thePlayer.position.y - pos.y)
            
            pathArray.append(getDifference(point:pos))
            
            walkTime = 0
            
        }
        
        func touchDown2(atPoint pos : CGPoint) {
            
            //Touch down for NOT walking with path
            
            if (pos.x < thePlayer.position.x) {
                
                touchingDown = true
                
                thePlayer.removeAction(forKey: "Idle")
                
                offsetFromTouchDownToPlayer = CGPoint(x: thePlayer.position.x - pos.x, y:thePlayer.position.y - pos.y)
                
                
                if ( touchDownSprite.parent == nil) {
                    
                    touchDownSprite = SKSpriteNode(imageNamed: "TouchDown")
                    self.addChild(touchDownSprite)
                    touchDownSprite.position = pos
                    
                } else {
                    
                    touchDownSprite.position = pos
                }
                
                
                if ( touchFollowSprite.parent == nil) {
                    
                    touchFollowSprite = SKSpriteNode(imageNamed: "TouchDown")
                    self.addChild(touchFollowSprite)
                    touchFollowSprite.position = pos
                    
                } else {
                    
                    touchFollowSprite.position = pos
                }
                
                
                
                
                
            }
            
            
            
        }
        
        //MARK: TOUCH MOVED
        
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
            for t in touches {
                
                if ( walkWithPath == true) {
                    
                    self.touchMoved(toPoint: t.location(in: self))
                    
                } else {
                    
                    self.touchMoved2(toPoint: t.location(in: self))
                }
                
                break
            }
        }
        
        
        
        func touchMoved(toPoint pos : CGPoint) {
            
            //Touch moved for  walking with path
            
            if (thePlayer.action(forKey: "PlayerMoving") != nil &&  pathArray.count > 4) {
                
                
                thePlayer.removeAction(forKey: "PlayerMoving")
                
            }
            
            walkTime = walkTime + thePlayer.walkSpeedOnPath
            
            pathArray.append(getDifference(point:pos))
            
            
        }
        
        func touchMoved2(toPoint pos : CGPoint) {
            
            //Touch moved for NOT walking with path
            
            if ( touchingDown == true){
                
                orientCharacter(pos:pos)
                touchFollowSprite.position = pos
                
            }
            
        }
        
        
        
        //MARK: TOUCH ENDED
        
        
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            
            if (walkWithPath == true ){
                
                createLineWith(array:pathArray)
                
                pathArray.removeAll()
                currentOffset = CGPoint.zero
                
            } else {
                
                if ( touchingDown == true) {
                    
                    thePlayer.removeAllActions()
                    
                    touchingDown = false
                    
                    touchFollowSprite.removeFromParent()
                    touchDownSprite.removeFromParent()
                    
                    runIdleAnimation()
                    
                }
                
                
                
            }
            
        }
        
        //MARK: CREATE THE PATH TO FOLLOW
        
        
        func createLineWith(array:[CGPoint]) {
            
            let path = CGMutablePath()
            path.move(to:pathArray[0])
            
            for point in pathArray {
                
                path.addLine(to:point)
                
            }
            
            
            let line = SKShapeNode()
            line.path = path
            line.lineWidth = 10
            line.strokeColor = UIColor.white
            line.alpha = pathAlpha
            
            
            self.addChild(line)
            
            let fade:SKAction = SKAction.fadeOut(withDuration: 0.3)
            let runAfter:SKAction = SKAction.run {
                
                
                line.removeFromParent()
                print("removing line after fade")
                
            }
            
            line.run( SKAction.sequence([fade, runAfter]) )
            
            
            makePlayerFollowPath(path: path)
            
        }
        
        func makePlayerFollowPath(path:CGMutablePath) {
            
            let followAction:SKAction = SKAction.follow(path, asOffset: false, orientToPath: false, duration: walkTime)
            
            let finish:SKAction = SKAction.run {
                
                self.runIdleAnimation()
            }
            
            let seq:SKAction = SKAction.sequence([followAction, finish])
            
            thePlayer.run(seq, withKey: "PlayerMoving")
            
            
        }
        
        
        func runIdleAnimation() {
            
            var animationName:String = ""
            
            
            if (playerFacing == .front){
                
                animationName = thePlayer.frontIdle
                
                
            } else if (playerFacing == .back){
                
                animationName = thePlayer.backIdle
                
                
            } else if (playerFacing == .left){
                
                animationName = thePlayer.leftIdle
                
                
            } else if (playerFacing == .right){
                
                animationName = thePlayer.rightIdle
                
                
            }
            
            if ( animationName != ""){
                
                let idleAnimation:SKAction = SKAction(named: animationName, duration: 1 )!
                thePlayer.run(idleAnimation, withKey: "Idle")
                
            }
            
            
            
        }
        //MARK: Player Update for Walking ON a Path
        
        
        func playerUpdate() {
            
            //runs at the same frame rate as the game (called by update statement)
            
            if (playerLastLocation != CGPoint.zero) {
                
                //make sure player IS moving, but not attacking
                if (thePlayer.action(forKey: "PlayerMoving") != nil && thePlayer.action(forKey: "Attack") == nil) {
                    
                    if ( abs(thePlayer.position.x - playerLastLocation.x) > abs(thePlayer.position.y - playerLastLocation.y) ) {
                        
                        //greater movement on the x
                        
                        if ( thePlayer.position.x > playerLastLocation.x){
                            //right
                            
                            playerFacing = .right
                            
                            animateWalk()
                            
                            
                            
                        } else {
                            //left
                            playerFacing = .left
                            
                            animateWalk()
                            
                        }
                        
                        
                        
                    } else {
                        
                        //greater movement on the y
                        
                        if ( thePlayer.position.y > playerLastLocation.y){
                            //up (back)
                            
                            playerFacing = .back
                            
                            animateWalk()
                            
                            
                            
                        } else {
                            //down (forward)
                            playerFacing = .front
                            
                            animateWalk()
                        }
                        
                        
                        
                    }
                    
                }
                
            }
            
            playerLastLocation = thePlayer.position
            
            
            
        }
        
        //MARK: Player Update for Walking NOT on a Path
        
        
        
        
        
        func playerUpdate2() {
            
            if (touchingDown == true) {
                
                
                let walkSpeed = thePlayer.walkSpeed
                
                touchDownSprite.position = CGPoint(x: thePlayer.position.x - offsetFromTouchDownToPlayer.x, y: thePlayer.position.y - offsetFromTouchDownToPlayer.y)
                
                if ( playerFacing == .front){
                    
                    thePlayer.position = CGPoint(x:thePlayer.position.x, y:thePlayer.position.y - walkSpeed)
                }else if ( playerFacing == .back){
                    
                    thePlayer.position = CGPoint(x: thePlayer.position.x , y: thePlayer.position.y + walkSpeed)
                }
                else if ( playerFacing == .left){
                    
                    thePlayer.position = CGPoint(x: thePlayer.position.x - walkSpeed, y: thePlayer.position.y)
                }
                else if ( playerFacing == .right){
                    
                    thePlayer.position = CGPoint(x: thePlayer.position.x + walkSpeed, y: thePlayer.position.y)
                }
                
                
                animateWalk2()
                
                
                
            }
            
            
        }
        
        
        func orientCharacter(pos:CGPoint) {
            
            
            
            if ( abs(touchDownSprite.position.x - pos.x) > abs(touchDownSprite.position.y - pos.y) ) {
                
                //greater movement on the x
                
                if ( touchDownSprite.position.x < pos.x){
                    //right
                    
                    playerFacing = .right
                    
                    
                } else {
                    //left
                    playerFacing = .left
                    
                }
                
                
                
            } else {
                
                //greater movement on the y
                
                if ( touchDownSprite.position.y < pos.y){
                    //up (back)
                    
                    playerFacing = .back
                    
                } else {
                    //down (forward)
                    playerFacing = .front
                    
                    
                }
                
                
                
            }
            
            
            
            
        }
        
        
        
        
        func animateWalk() {
            
            var theAnimation:String = ""
            
            if (playerFacing == .right){
                
                theAnimation = thePlayer.rightWalk
                
            } else if (playerFacing == .left){
                
                theAnimation = thePlayer.leftWalk
                
            }  else if (playerFacing == .back){
                
                theAnimation = thePlayer.backWalk
                
            }  else if (playerFacing == .front){
                
                theAnimation = thePlayer.frontWalk
            }
            
            
            
            if (thePlayer.action(forKey:  theAnimation ) == nil){
                
                let walkAnimation:SKAction = SKAction(named:theAnimation, duration: 0.25)!
                thePlayer.run (walkAnimation, withKey: theAnimation )
                
            }
            
            
        }
        func animateWalk2() {
            
            
            
            
            var theAnimation:String = ""
            
            if (playerFacing == .right){
                
                theAnimation = thePlayer.rightWalk
                
            } else if (playerFacing == .left){
                
                theAnimation = thePlayer.leftWalk
                
            }  else if (playerFacing == .back){
                
                theAnimation = thePlayer.backWalk
                
            }  else if (playerFacing == .front){
                
                theAnimation = thePlayer.frontWalk
            }
            
            
            if (theAnimation != ""){
                
                //make sure the animation isn't blank
                
                if (thePlayer.action(forKey:  theAnimation ) == nil){
                    
                    thePlayer.removeAction(forKey: thePlayer.rightWalk)
                    thePlayer.removeAction(forKey: thePlayer.leftWalk)
                    thePlayer.removeAction(forKey: thePlayer.backWalk)
                    thePlayer.removeAction(forKey: thePlayer.frontWalk)
                    
                    
                    let walkAnimation:SKAction = SKAction(named:theAnimation, duration: 0.25)!
                    let repeatAction:SKAction = SKAction.repeatForever(walkAnimation)
                    thePlayer.run (repeatAction, withKey: theAnimation )
                    
                    
                    
                    
                }
                
            }
            
            
            
        }
        
        
        
        
    }
    
    
    //
    //  player.swift
    //  AlienOdissey
    //
    //  Created by jonathan sinien on 24/05/2017.
    //  Copyright © 2017 jonathan sinien. All rights reserved.
    //
    
    import Foundation
    import SpriteKit
    
    class Player: SKSpriteNode {
        
        var frontWalk:String = ""
        var frontIdle:String = ""
        var frontMelee:String = ""
        
        var backWalk:String = ""
        var backIdle:String = ""
        var backMelee:String = ""
        
        var leftWalk:String = ""
        var leftIdle:String = ""
        var leftMelee:String = ""
        
        var rightWalk:String = ""
        var rightIdle:String = ""
        var rightMelee:String = ""
        
        var meleeAnimationFXName:String = "Attacking"
        var meleeScaleSize:CGFloat = 2
        var meleeAnimationSize:CGSize = CGSize(width: 100, height: 100)
        var meleeTimeBetweenUse:TimeInterval = 0
        var meleeDamage:Int = 1
        
        
        var walkSpeedOnPath:TimeInterval = 0.15 ////the smaller the faster
        var health:Int = 20
        var armor:Int = 20
        var immunity:TimeInterval = 1
        
        var walkSpeed:CGFloat = 2
        
        var currentHealth:Int = 20
        var currentArmor:Int = 20
        var currentProjectile:String = ""
        
        
        func setUpWithDict(theDict: [String : Any]) {
            
            for (key, value) in theDict {
                
                if (key == "Animation"){
                    
                    if (value is [String : Any]) {
                        
                        sortAnimationDict(theDict: value as! [String : Any])
                        
                    }
                    
                } else if (key == "Melee"){
                    
                    if (value is [String : Any]) {
                        
                        sortMeleeDict(theDict: value as! [String : Any])
                        
                    }
                    
                } else if (key == "Stats"){
                    
                    if (value is [String : Any]) {
                        
                        sortStatsDict(theDict: value as! [String : Any])
                        
                    }
                    
                } else if (key == "Ranged"){
                    
                    if (value is [String : Any]) {
                        
                        sortRangedDict(theDict: value as! [String : Any])
                        
                    }
                    
                }
                
                
            }
            
            
        }
        
        func sortAnimationDict(theDict: [String : Any]) {
            
            for (key, value) in theDict {
                
                if (key == "Back"){
                    
                    if let backDict:[String : Any] = value as? [String : Any] {
                        
                        for (key, value) in backDict {
                            
                            if ( key == "Walk"){
                                
                                backWalk = value as! String
                                
                            }
                            else if ( key == "Idle"){
                                
                                backIdle = value as! String
                                
                            }
                            else if ( key == "Melee"){
                                
                                backMelee = value as! String
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                    
                } else if (key == "Front"){
                    
                    if let frontDict:[String : Any] = value as? [String : Any] {
                        
                        for (key, value) in frontDict {
                            
                            if ( key == "Walk"){
                                
                                frontWalk = value as! String
                                
                            }
                            else if ( key == "Idle"){
                                
                                frontIdle = value as! String
                                
                            }
                            else if ( key == "Melee"){
                                
                                frontMelee = value as! String
                                
                            }
                            
                            
                        }
                        
                    }
                    
                }else if (key == "Left"){
                    
                    if let leftDict:[String : Any] = value as? [String : Any] {
                        
                        for (key, value) in leftDict {
                            
                            if ( key == "Walk"){
                                
                                leftWalk = value as! String
                                
                            }
                            else if ( key == "Idle"){
                                
                                leftIdle = value as! String
                                
                            }
                            else if ( key == "Melee"){
                                
                                leftMelee = value as! String
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                } else if (key == "Right"){
                    
                    if let rightDict:[String : Any] = value as? [String : Any] {
                        
                        for (key, value) in rightDict {
                            
                            if ( key == "Walk"){
                                
                                rightWalk = value as! String
                                
                            }
                            else if ( key == "Idle"){
                                
                                rightIdle = value as! String
                                
                            }
                            else if ( key == "Melee"){
                                
                                rightMelee = value as! String
                                
                            }
                            
                            
                        }
                        
                    }
                    
                }
                
            }
            
            
        }
        
        func sortMeleeDict(theDict: [String : Any]) {
            
            
            for (key, value) in theDict {
                
                if (key == "Damage"){
                    
                    if ( value is Int){
                        
                        meleeDamage = value as! Int
                    }
                    
                    
                }
                else if (key == "Size"){
                    
                    if ( value is String){
                        
                        meleeAnimationSize = CGSizeFromString(value as! String)
                        
                    }
                    
                    
                }
                else if (key == "Animation"){
                    
                    if ( value is String){
                        
                        meleeAnimationFXName = value as! String
                        
                    }
                    
                    
                }
                else if (key == "ScaleTo"){
                    
                    if ( value is CGFloat){
                        
                        meleeScaleSize = value as! CGFloat
                        
                    }
                    
                    
                    
                }
                else if (key == "TimeBetweenUse"){
                    
                    if ( value is TimeInterval){
                        
                        meleeTimeBetweenUse = value as! TimeInterval
                    }
                    
                    
                }
                
                
            }
            
            
        }
        
        func sortRangedDict(theDict: [String : Any]) {
            
            
            for (key, value) in theDict {
                
                if (key == "Projectile"){
                    
                    if ( value is String){
                        
                        currentProjectile = value as! String
                    }
                    
                    
                }
                
                
            }
            
            
        }
        
        
        
        func sortStatsDict(theDict: [String : Any]) {
            
            for (key, value) in theDict {
                
                if (key == "Speed"){
                    
                    if ( value is CGFloat){
                        
                        walkSpeed = value as! CGFloat
                        
                    }
                    
                    
                } else if (key == "PathSpeed"){
                    
                    if ( value is TimeInterval){
                        
                        walkSpeedOnPath = value as! TimeInterval
                        
                    }
                    
                    
                } else if (key == "Health"){
                    
                    if ( value is Int){
                        
                        health = value as! Int
                        
                        currentHealth = health
                        
                        
                    }
                    
                    
                } else if (key == "Armor"){
                    
                    if ( value is Int){
                        
                        armor = value as! Int
                        
                        currentArmor = armor
                        
                    }
                    
                    
                }else if (key == "Immunity"){
                    
                    if ( value is TimeInterval){
                        
                        immunity = value as! TimeInterval
                        
                    }
                    
                    
                }
                
            }
            
        }
        
        
        
        
    }
    

    It is a lot of code I understand. But I’m locked since 3 days. I think my pliât file got some string error.

    Any help are welcome 🙂

    Thank you.

  • June 6, 2017 at 4:47 pm #168398

    Did you email me? I think I answered something similar via email a week ago. If not, I think the answer is because an Action is missing in the Actions.sks file. In the kit, I added a check on the actions to make sure it can successfully create the action. If not it just fails gracefully. like…

    if let someAction:SKAction = SKAction(named: "BackAttack") {
    //success 
    }

    So if you forgot to add an action named “BackAttack”, it wouldn’t crash

You must be logged in to reply to this topic.


Got something to sell online? We trust the amazing tools from Easy Digital Downloads