Pause and resume animation in the same keyframe - ios

Is it possible to pause animation of SCNNode (animaton with model from .dae collada) and then resume in the same keyframe which it was paused?

Will using the isPaused property on the SCNNode work for your needs?
Example:
self.childNode.isPaused = true

Related

SKAudioNode won't play sound

I have a small game that needs some background music. However I cannot figure out how to make this happen! I've tried using SKAudioNode with this code.
override func didMoveToView(view: SKView) {
/* Setup your scene here */
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
self.addChild(sprite)
//music
let music = SKAudioNode(fileNamed: "music")
self.addChild(music)
}
I have a Data Set in Assets.xcassets called "music" with only one file in it called "music.mp3". I've tried changing the initialization of the music constant to SKAudioNode(fileNamed: "music.mp3") but that had no effect. Any help would be highly appreciated.
I have my doubts that you can use music from the XCAssets file. Instead drag and drop the file into your folder. Then use it.
Secondly, just "music" will not work. It requires an extension!
You can even play music using the SKAction Class also!
[SKAction repeatForever:[SKAction playSoundFileNamed:#"yourFile.mp3" waitForCompletion:YES]];

Changing the animation duration of a CAShapeLayer transform in swift

I am using CATransform3DMakeScale to resize a vector graphic in an iPad app. It is animated by default and I would like to be able to change the duration of this animation. Any advice?
If it's layers you want to adjust the duration try + setAnimationDuration:
Swift
class func setAnimationDuration(_ dur: CFTimeInterval)
Objective-C
+ (void)setAnimationDuration:(CFTimeInterval)duration
Sets the animation duration used by all animations within this
transaction group.
https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CATransaction_class/index.html#//apple_ref/occ/clm/CATransaction/setAnimationDuration:

Sprites disappears sometimes [Sprite Kit]

Main menu looks like this.
When i press start code below changes the scene.
let newScene = LevelScene(size: self.scene!.size)
let transition = SKTransition.revealWithDirection(SKTransitionDirection.Up, duration: 1)
newScene.scaleMode = SKSceneScaleMode.AspectFill
self.scene!.view!.presentScene(newScene, transition: nil)
And after I return to the main menu it looks like this
or this
So, as you can see, sprites just disappears. I can't come up with a reason for this to happen.
Are you doing,
self.addChild(...)
self.addChild(...)
in your didMoveToView method?
Another possible problem might be that you are setting the zPosition of the node to less than your background, and / or other nodes.
I had the same problem with sprites disappearing and after changing the zPositions of all my sprites and background I still wasn't able to solve it.
I ended up just redeclaring the scene inside my restartButton:
var scene = SKScene(fileNamed: "GameScene")
scene?.scaleMode = .aspectFill
view!.presentScene(scene)
And that worked for me.

Pausing and Playing SpriteKit Game in Swift

How do I pause and resume a game in SpriteKit in a single Scene Game (gameScene)?
addChild(pauseText)
self.runAction(SKAction.runBlock(self.pauseGame))
To pause a game from the view controller, you should call gameView.paused = true (where gameView is an SKView).
To pause a game from the scene itself, you should call physicsWorld.speed = 0. To un-pause the game, call physicsWorld.speed = 1.

pause spritekit scene and show "paused" label

I want to show a "paused" SKLabelNode when someone clicks on the screen and therefore pauses the sprite kit game.
So I have in touchesBegan ->
[self.pausedLabel setHidden:!self.pausedLabel.hidden];
[self.scene.view setPaused:!self.scene.view.paused];
The Game is paused correctly but the SKLabelNode is not shown (scene not rendered before paused?!)
If I add a NSTimer for pausing the scene the label is shown, but then the game continues for that timer-time.
Does anyone have a better solution for this?
Thanks in advance
I would use SKAction for this. You can use +runBlock: to add the code related to hiding the label, and then use the -runAction method with the completion handler to pause the scene. The runBlock: method may return immediately, but this way, the screen manages to update before the scene is paused.
SKAction *action = [SKAction runBlock:^{
[self.pausedLabel setHidden:!self.pausedLabel.hidden];
}];
[self.pausedLabel runAction:action completion:^{
[self.scene.view setPaused:!self.scene.paused];
}];
Just use a state ivar to determine if the scene should update its content or not.
When you click the button set this state to PAUSE and in your scene frame update loop test the state.
if (_state != PAUSE) {// Use enum for the state var
// Update scene objects
}
when your button is clicked:
Add pause label to scene
Set state to PAUSE
The benefits of this approach is that it allows you to decide exactly what will happen on pause (opposing to pausing the whole scene). You can animate a cool background while in pause or do anything you'd like as this is totally in your control

Resources