Physics and Discovering Elements in Code that reference Plist

Home Forums iOS / tvOS Starter Kits The Story Tellers Kit 2 for iOS or tvOS Apps Physics and Discovering Elements in Code that reference Plist

This topic contains 4 replies, has 2 voices, and was last updated by  Justin at CartoonSmart 7 months, 2 weeks ago.

  • April 19, 2016 at 4:12 pm #147546

    I am looking at two different aspects of the Storyteller Kit I would to emulate in my own game. The quarter gumball machine, and the infamous Angry Boulder paradoux, I am trying to match the code with the Plist to see the ins-outs on how to match the listeners to the elements being rendered in the scene. Any assistance or point in the right direction would greatly appreciated.

  • April 20, 2016 at 9:59 am #147575

    There’s a lot you could pick apart in that project. And for anyone reading this that doesn’t already, the Story Tellers Kit 2 includes about 10 hours of training, covering it’s entire creation.

    So a couple videos to pay attention to would be this one…

    Event Listeners to trigger events when certain values equal, exceed or are less than a value

    And then a whole bunch of videos later, I teach a bit more to extend the functionality….

    Listening for all Conditions or All Conditions in a Group

    But I can explain some Swift basics here. One thing to keep in mind is that the kit has a lot of code thats only there because it’s setup to work with a property list. For example, users of the kit, list out Events they want to occur (Hide, Show, PlaySounds, AnimateNodeToNode, etc) and in the Page_Listeners.swift file we “listen” out for certain things (intersections, values equaling each other, etc), and the code blocks that are checking those things are being re-run periodically with the Events in tow, so to speak. Point is, in your project you can probably hard-code in exactly what you want to happen when something else does, which means you don’t have to pass around an Events dictionary each time you re-check a condition. The Page_Listeners.swift also has a lot of code to check if not just one but multiple conditions are met. Which is an awesome feature of the starter kit, but also one you probably don’t need (or need as flexible).

    So for example, your code might just want to periodically check…

    Did something intersect?
    Or is a variable equal to 5?

    And if so, do something. So in Swift and Sprite Kit, one of the easiest “out of the box” ways to do that is in the update statement which runs about 60 times per second.

    class GameScene: SKScene {
        
        var myVar:Int = 0
        
        override func didMoveToView(view: SKView) {
            /* Setup your scene here */
            
        }
        
       
        override func update(currentTime: CFTimeInterval) {
            /* runs about 60 times per second */
            
            if (myVar == 5) {
                
                // do something
            }
            
        }
    }

    60 times per second might be overkill though for your needs. And depending on how much else you’re doing in that statement (or in the game in general), you might not want to check that often. A simple if statement, I wouldn’t worry about checking that often, but imagine theres a lot more going on.

    So you could create an SKAction to “listen” out periodically. In the example below, it checks to see if myVar is greater than 1000, every half second (notice the SKAction.waitForDuration is set to 0.5). If the condition isn’t met, we re-run the same function, checkForSomething.

    class GameScene: SKScene {
        
        var myVar:Int = 0
        
        override func didMoveToView(view: SKView) {
            /* Setup your scene here */
        
            
            checkForSomething()
        }
        
        
        func checkForSomething(){
            
            //this function will re-run itself until a condition is met
            
            let waitAction:SKAction = SKAction.waitForDuration(0.5)
            let runAction:SKAction = SKAction.runBlock{
                
                if ( self.myVar >= 1000){
                    
                    print ( "myVar is 1000")
                    
                } else {
                    
                    
                    self.checkForSomething()
                    print ("will keep checking something. the var is only \(self.myVar)")
                    
                }
                
                
            }
            
            
            let sequence:SKAction = SKAction.sequence( [  waitAction, runAction] )
            self.runAction(sequence)
            
            
        }
        
       
        override func update(currentTime: CFTimeInterval) {
            /* runs about 60 times per second, increments myVar */
            
            myVar += 1
        }
    }

    Hope that makes sense! Physics is a whole different thing. I’d recommend watching our iOS and tvOS Pinball Game Tutorials for that. Here’s a lesson from that video series…

    SKPhysicsContacts and Locking the Flipper Up

    And actually nearly every Swift and Sprite Kit course goes into physics contacts and listening for one thing colliding with another.

  • April 20, 2016 at 10:53 pm #147793

    Made you a quick video on the topic…

    http://cartoonsmart.com/free-swift-ios-and-tvos-video-tutorials/

  • April 25, 2016 at 11:10 pm #148310

    Thank you for posting the video and the code extremely helpful. Currently checking out the traffic and playing the wild west pinball game. Hilarious to hear your “voice over” in the beginning… bout to check it out on Apple TV.

  • April 27, 2016 at 2:41 am #148371

    Oops, I posted the wrong link. I think you found it though…

    How to Check Values Periodically with Swift and Sprite Kit – Video Tutorial

    Thanks!

You must be logged in to reply to this topic.