How to make scene transition happen after a certain amount of time?

Discussion in 'iOS Programming' started by Sharukh7, Jul 20, 2015.

  1. Sharukh7, Jul 20, 2015
    Last edited: Jul 20, 2015

    Sharukh7 macrumors newbie

    Joined:
    Jul 7, 2015
    #1
    In my game, once an object contacts with the ground, the game is over. So, once that happens, I made an animation which is pretty much two red flashes on the background when the object hits the ground. That animation doesn't happen anymore and I'm confused to why. I had recently added scene transition right after my animation code, but it doesn't run the animation and just transitions to new scene. Can you please help me determine to what I'm doing wrong? The scene class (a new swift file I created):

    Code:
    classGameOverNew:SKScene{
    
    var score =NSInteger()
    
    override func didMoveToView(view:SKView){
    
    backgroundColor =SKColor.redColor()
    
    var scoreTextNode =SKLabelNode(fontNamed:"Copperplate")
    scoreTextNode.text ="SCORE : \(score)"
    scoreTextNode.horizontalAlignmentMode =SKLabelHorizontalAlignmentMode.Center
    scoreTextNode.verticalAlignmentMode =SKLabelVerticalAlignmentMode.Center
    scoreTextNode.fontSize =20
    scoreTextNode.fontColor =SKColor.whiteColor()
    scoreTextNode.position =CGPointMake(size.width /2.0, size.height /2.0)
    addChild(scoreTextNode)
    }
    }
    My GameScene didBeginContact code:

    Code:
    func didBeginContact(contact: SKPhysicsContact) {
    
        if( moving.speed > 0 ) {
    
            if( ( contact.bodyA.categoryBitMask & scoreCategory ) == scoreCategory || ( contact.bodyB.categoryBitMask & scoreCategory ) == scoreCategory ) {
    
                score++;
                scoreLabelNode.text = "\(score)"
            } else {
                moving.speed = 0;
    
                object.physicsBody!.collisionBitMask = worldCategory
    
    
                self.removeActionForKey("flash")
                var turnBackgroundRed = SKAction.runBlock({() in self.setBackgroundColorRed()})
                var wait = SKAction.waitForDuration(0.05)
                var turnBackgroundWhite = SKAction.runBlock({() in self.setBackgroundColorWhite()})
                var turnBackgroundSky = SKAction.runBlock({() in self.setBackgroundColorSky()})
                var sequenceOfActions = SKAction.sequence([turnBackgroundRed,wait,turnBackgroundWhite,wait, turnBackgroundSky])
                var repeatSequence = SKAction.repeatAction(sequenceOfActions, count: 2)
                var canRestartAction = SKAction.runBlock({() in self.letItRestart()})
                var groupOfActions = SKAction.group([repeatSequence, canRestartAction])
                self.runAction(groupOfActions, withKey: "flash")
    
        // this is the scene transition code I added
        let myScene = GameOverNew(size: self.size)
        myScene.scaleMode = scaleMode
        let reveal = SKTransition.fadeWithDuration(2.0)
        self.view?.presentScene(myScene, transition: reveal)
    
    
    
           }
        }
    }
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    What does the self.runAction code look like? Any animate with duration and completion block stuff in there? If so, try placing the transition code into the completion block.
     
  3. Sharukh7 thread starter macrumors newbie

    Joined:
    Jul 7, 2015
    #3
    I did figure out what code of line I need to use(same as you mentioned) and have been doing for past 1.5 days, but I'm getting different errors different times. I'm really new to this, so I can't get it right, since I'm not understanding all of its elements. I'm trying to run the whole else statement of didBeginContact method before animation occurs, but can't get it to work.
     
  4. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    If you don't share the details, no one can help. People who have specific experience with SceneKit can also be more helpful.
     
  5. Sharukh7 thread starter macrumors newbie

    Joined:
    Jul 7, 2015
    #5
    Sorry for not mentioning. I didn't realize that I was missing details. But, do you happen to know how I can make this function work? I will deeply appreciate it if you can help me with this. I'm trying to run the whole else code in didBeginContact method before a transition takes place. The way I putted my the transition code in else statement, it ignores the else part and just transitions. The line of code that I did figure out is:
    Code:
    func runAction(action: SKAction, withKey: String, callback: () -> Void) {
        // stuff runAction should do
        callback()
    }
     

Share This Page