SKAction.waitForDuration not working in SpriteKit - ios

I have been pulling my hair out over this seemingly simple feature I'm trying to build in to a game I am working on in SpriteKit and Swift. I have searched and can't figure why this isn't working.
What I want to do:
When the player comes in contact with an enemy or an obstacle, I want to load and play through an explosion animation createExplosion() (The animation completes in 0.5 seconds) and then progress to the gameOverAction(). The problem is that only the first frame of the animation is shown and then it progresses to the gameOverAction().
I tried to make an SKAction.sequence but Xcode did not like me calling functions inside the sequence, so I found SKAction.waitForDuration. Problem is, it is having no effect on the execution of the code.
How should this be done properly?
func collisionWithPlayer(playerObject: SKSpriteNode) {
runAction(SKAction.playSoundFileNamed("spaceExplosion.mp3", waitForCompletion: false))
createExplosion(playerObject)
playerObject.removeFromParent()
runAction(SKAction.waitForDuration(NSTimeInterval(0.5)))
gameOverAction()
}
And here is the code for the gameOverAction():
func gameOverAction() {
backgroundMusicPlayer.stop()
globalHighScore = NSUserDefaults.standardUserDefaults().objectForKey("GameHighScore") as? Int
localHighScore = self.monstersDestroyed
if globalHighScore == nil {
globalHighScore = localHighScore
}
globalHighScore = self.recordHighScore(localHighScore, highScore: globalHighScore!)
println(globalHighScore!)
let reveal = SKTransition.fadeWithDuration(1.0)
let gameOverScene = GameOverScene(size: self.size, won: false)
self.view?.presentScene(gameOverScene, transition: reveal)
}
func createExplosion(object: SKSpriteNode) -> SKSpriteNode {
var explosionArray = Array<SKTexture>()
var explosionSprite = SKSpriteNode()
explosionArray.append(SKTexture(imageNamed: "explosionSprite01.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite02.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite03.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite04.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite05.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite06.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite07.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite08.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite09.png"))
let animateAction = SKAction.animateWithTextures(explosionArray, timePerFrame: 0.05)
explosionSprite = SKSpriteNode(texture:explosionArray[0])
explosionSprite.position = CGPoint(x: object.position.x + 10.0, y: object.position.y)
addChild(explosionSprite)
explosionSprite.runAction(SKAction.sequence([animateAction, SKAction.removeFromParent()]))
return(explosionSprite)
}

The waitForDuration SKAction can be used to introduce a delay into an action sequence, but it doesn't block the thread calling runAction - so your waitForDuration action will be set to run on your node, but then gameOverAction() will be called immediately.
You can use the completion block of runAction:completion to perform gameOverAction() after the actions are complete -
func collisionWithPlayer(playerObject: SKSpriteNode) {
var actions = Array<SKAction>();
actions.append(SKAction.playSoundFileNamed("spaceExplosion.mp3", waitForCompletion: false))
actions.append(SKAction.waitForDuration(NSTimeInterval(0.5)))
actions.append(SKAction.removeFromParent())
createExplosion(playerObject)
let sequence = SKAction.sequence(actions);
playerObject.runAction(sequence,completion: { () -> Void in
self.gameOverAction()
})
}
Depending on what createExplosion does it may be possible to incorporate it into the sequence too and remove the need for the waitForDuration

Related

Timer is not getting updated

I basically running a timer in the didMove function of the GameScene class in SpriteKit. The function basically just makes the player shoot bullets at 0.5-second intervals. However, the problem I am getting is that once the player dies, and I use removeFromParent() on the player, the bullets are still firing, as if they are just appearing in empty space.
Here is my code:
var timer1 = Timer()
self.timer1 = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { _ in self.bullet1.fireBullet1(player: self.player1, scene: self)})
And here is the fireBullet1 function:
func fireBullet2(player: Player2, scene: SKScene) {
let bulletNode = Bullet()
bulletNode.texture = self.textureAtlas.textureNamed("bullet2")
bulletNode.position = player.position
bulletNode.position.y += -sin(player.zRotation)*10
bulletNode.position.x += -cos(player.zRotation)*10
bulletNode.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width/500)
bulletNode.physicsBody?.mass = 0.005
bulletNode.physicsBody?.affectedByGravity = false
bulletNode.zRotation = player.zRotation
bulletNode.physicsBody?.categoryBitMask = PhysicsCategory.bullet2.rawValue
bulletNode.physicsBody?.contactTestBitMask =
PhysicsCategory.boundary.rawValue |
PhysicsCategory.bullet1.rawValue |
PhysicsCategory.player1.rawValue
scene.addChild(bulletNode)
bulletNode.physicsBody?.applyImpulse(CGVector(dx: -cos(player.zRotation)*1, dy: -sin(player.zRotation)*1))
}
Can someone please tell me what I need to do in order to make the timer stop calling the function after the player dies.

Swift: Obtain and save the updated SCNNode over time using projectPoint in scenekit

I am trying to use projectPoint to get the 2D information of the updated SCNNode in scenekit and save them.
Based on ignotusverum's suggestion, I am able to save the SCNNode in to a path in a button.
var lastPosition: CGPoint?
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
guard anchor == currentFaceAnchor,
let contentNode = selectedContentController.contentNode,
contentNode.parent == node
else { return }
for (index, vertex) in vertices.enumerated() {
let vertex = sceneView.projectPoint(node.convertPosition(SCNVector3(vertex), to: nil))
let xVertex = CGFloat(vertex.x)
let yVertex = CGFloat(vertex.y)
Position = CGPoint(x: xVertex, y: yVertex)
}
selectedContentController.session = sceneView?.session
selectedContentController.sceneView = sceneView
selectedContentController.renderer(renderer, didUpdate: contentNode, for: anchor)
}
Started saving via a start button:
private var fpsTimer = Timer()
private var currentCaptureFrame = 0
#IBAction private func startPressed() {
currentCaptureFrame = 0 //inital capture frame
fpsTimer = Timer.scheduledTimer(withTimeInterval: 1/fps, repeats: true, block: {(timer) -> Void in self.recordData()})
}
Saved them via a stop button clicks:
#IBAction private func stopPressed() {
do {
fpsTimer.invalidate() //turn off the timer
let capturedData = captureData.map{$0.stringRepresentation}.joined(separator:"\(lastPosition)>")
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url = dir.appendingPathComponent("testing.txt")
try capturedData.appendLineToURL(fileURL: url as URL)
}
catch {
print("Could not write to file")
}
}
So far works fine with the points being saved. The problem is that in the saved data, I realized the data is only saving one frame of the x and y vertices. For example:
[(411.0618591308594, 534.4215087890625), (410.7286071777344, 544.9381713867188), (411.5425720214844, 522.1063232421875), (412.0340881347656, 512.1854248046875),...
[(411.0618591308594, 534.4215087890625), (410.7286071777344, 544.9381713867188), (411.5425720214844, 522.1063232421875), (412.0340881347656, 512.1854248046875)
The data is repeating with one frame rather than the period I want to save from the moment when I click start button to stop button.
My question is how to save the updated SCNNode over time from the moment when I click start button to stop button?
Thanks in advance!
If I understand your question correctly you want to save the vertex positions each time they are updated, keeping track of all previous updates as well as the most recent one. In order to do this you simply need to append the new vertex position array to the global array with saved data.
var savedPositions = [CGPoint]()
var beginSaving = false
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
guard anchor == currentFaceAnchor,
let contentNode = selectedContentController.contentNode,
contentNode.parent == node
else { return }
for vertex in vertices {
let projectedPoint = sceneView.projectPoint(node.convertPosition(SCNVector3(vertex), to: nil))
if beginSaving {
savedPositions.append(CGPoint(x: projectedPoint.x, y: projectedPoint.y))
}
}
selectedContentController.session = sceneView?.session
selectedContentController.sceneView = sceneView
selectedContentController.renderer(renderer, didUpdate: contentNode, for: anchor)
}
#IBAction private func startPressed() {
beginSaving = true
}
#IBAction private func stopPressed() {
beginSaving = false
....//do whatever with your array of saved positions
}
SceneKit calls this method exactly once per frame. If you do stuff inside the renderer, you gotta "get it done" and get out, otherwise it can have an impact on FPS.
If you need to move or check a lot of loop stuff, you can use timers to perform those efforts separately and that allows you some control over it. You can still keep your FPS up and break loops up or chunk work in the timers. If you do this, just make sure you put timers in the main thread.
You might also look at: node.presentation, ie: properties reflect the transitory values determined by any in-flight animations currently affecting the node.

iOS equivalent of Androids ValueAnimator

I would like something that mimics the behaviour in iOS of the ValueAnimator in Android. Under normal circumstances I would use a UIView animation, but unfortunately the objects value im trying to interpolate over time isnt working with a normal animation.
In my particular case im using a Lottie animation, and trying to change the progress of the animation via a UIView animation, but instead of the animation changing the value of the progress over time, it just jumps to the final value. eg:
let lottieAnim = ...
lottieAnim.animationProgress = 0
UIView.animate(withDuration: 2) {
lottieAnim.animationProgress = 1
}
This example does not animate the lottie animation over time, but simply jumps to the end. I know lottie has methods to play the animation, but Im trying to use a custom animation curve to set the progress (its a progress animation that has no finite duration) which is why I need to use a UIView animation to interpolate the value.
I need something that updates intermittently to mimic an animation, the first thing that comes to mind is Android ValueAnimator class.
I put together a class that will mimic a ValueAnimator from android somewhat, and will allow you to specify your own animation curve if need be (default is just linear)
Simple usage
let valueAnimator = ValueAnimator(duration: 2) { value in
animationView.animationProgress = value
}
valueAnimator.start()
Advanced usage
let valueAnimator = ValueAnimator(from: 0, to: 100, duration: 60, animationCurveFunction: { time, duration in
return atan(time)*2/Double.pi
}, valueUpdater: { value in
animationView.animationProgress = value
})
valueAnimator.start()
You can cancel it at any point as well using:
valueAnimator.cancel()
Value Animator class in Swift 5
// swiftlint:disable:next private_over_fileprivate
fileprivate var defaultFunction: (TimeInterval, TimeInterval) -> (Double) = { time, duration in
return time / duration
}
class ValueAnimator {
let from: Double
let to: Double
var duration: TimeInterval = 0
var startTime: Date!
var displayLink: CADisplayLink?
var animationCurveFunction: (TimeInterval, TimeInterval) -> (Double)
var valueUpdater: (Double) -> Void
init (from: Double = 0, to: Double = 1, duration: TimeInterval, animationCurveFunction: #escaping (TimeInterval, TimeInterval) -> (Double) = defaultFunction, valueUpdater: #escaping (Double) -> Void) {
self.from = from
self.to = to
self.duration = duration
self.animationCurveFunction = animationCurveFunction
self.valueUpdater = valueUpdater
}
func start() {
displayLink = CADisplayLink(target: self, selector: #selector(update))
displayLink?.add(to: .current, forMode: .default)
}
#objc
private func update() {
if startTime == nil {
startTime = Date()
valueUpdater(from + (to - from) * animationCurveFunction(0, duration))
return
}
var timeElapsed = Date().timeIntervalSince(startTime)
var stop = false
if timeElapsed > duration {
timeElapsed = duration
stop = true
}
valueUpdater(from + (to - from) * animationCurveFunction(timeElapsed, duration))
if stop {
cancel()
}
}
func cancel() {
self.displayLink?.remove(from: .current, forMode: .default)
self.displayLink = nil
}
}
You could probably improve this to be more generic but this serves my purpose.

How to perform action on simple UIView Animation

I've never done any coding before until a few weeks ago, and whilst I've made reasonable progress on my own, I'm struggling with something that I hope someone wouldn't mind helping with.
I'm trying to make a simple app for my daughter in Swift (xCode Version 7.3 - 7D175). It's a series of animations where characters pop up and down inside a moon crater.
What I want to be able to do, is call a function when one of the animated UIImages is pressed. Ideally I'd like it to play a sound when the animation is pressed (I know how to call sounds, just not in this way)
I've created a Class where I wrote the code for the animations
func alienAnimate() {
UIView.animateWithDuration(0.3, delay: 0, options: [.CurveLinear, .AllowUserInteraction], animations: {
self.center.y -= 135
}, completion: nil)
UIView.animateWithDuration(0.3, delay: 3, options: [.CurveLinear, .AllowUserInteraction], animations: {
self.center.y += 135
}, completion: nil)
}
If someone could point me in the right direction, I'd greatly appreciated. I guess there's probably better ways of animating, but this is all I know at the moment.
EDIT - 2nd May
OK so I eventually got it working...well, nearly :) It still doesn't work exactly as I'd like, but I'm working on it.
The animations essentially move vertically along a path (so the aliens pop up like they're coming out of a hole). The below code works, but still allows you to click on the image at the start of it's path when it's effectively down a hole.
I still need to work out how to click where it actually is now, and not be able to press its starting point.
View of App
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.locationInView(self.view)
if self.imgAlien.layer.presentationLayer()!.hitTest(touchLocation) != nil {
sfxPop.play()
} else if self.imgAlien.layer.presentationLayer()!.hitTest(touchLocation) != nil {
sfxPop.play()
} else if self.imgAlien2.layer.presentationLayer()!.hitTest(touchLocation) != nil {
sfxPop.play()
} else if self.imgAlien3.layer.presentationLayer()!.hitTest(touchLocation) != nil {
sfxPop.play()
} else if self.imgAlien4.layer.presentationLayer()!.hitTest(touchLocation) != nil {
sfxPop.play()
} else if self.imgAlien5.layer.presentationLayer()!.hitTest(touchLocation) != nil {
sfxPop.play()
} else if self.imgUFO.layer.presentationLayer()!.hitTest(touchLocation) != nil {
sfxPop.play()
}
}
Supposing your have your planet and animation working up and down as you wish, what do you do is to detecting touch:
This is just an example:
var image : UIImage = UIImage(named:"alien.png")!
var image2 : UIImage = UIImage(named:"alienBoss.png")!
var alien1 = UIImageView(image: image)
var alien2 = UIImageView(image: image)
var alienBoss = UIImageView(image: image2)
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
if(touch.view == alien1){
// User touch alien1, do whatever you want
} else
if(touch.view == alien2){
// User touch alien2, do whatever you want
} else
if(touch.view == alienBoss){
// User touch alienBoss, do whatever you want
}
}
Then, you want to enable sound so you can use AVAudioPlayer library:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player:AVAudioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let audioPath = NSBundle.mainBundle().pathForResource("alienSound", ofType: "mp3")
var error:NSError? = nil
}
You can use the code below to play a sound, stop, set the volume:
do {
player = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
// to play the mp3
player.play()
// to stop it
player.stop()
// to pause it
player.pause()
// to set the volume
player.volume = 0.5 // from 0.0 to 1.0
...
}
catch {
print("Something bad happened.")
}

Applying impulse on a SpriteKit node

I'm developing a Sprite Kit Game, which I have a node named hero who dodges approaching villains. I have an issue with applyImpulse for a jump action and, in this case, the hero can jump repeatedly and fly instead of dodging the villains. I'm used a boolean variable to change the status with a timer value to jump only once in 3 seconds but that is not working. Here is my code
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
if isGameOver == true {
self.restart()
}
if isstarted {
self.runAction(SKAction.playSoundFileNamed("jump.wav", waitForCompletion: false))
for touch: AnyObject in touches{
if jumpon == false { //hero has not jumped
let location = touch.locationInNode(self)
heroAction()
jumpon = true //hero jumped
}
}
} else {
isstarted = true
hero.stop()
hero.armMove()
hero.rightLegMove()
hero.leftLegMove()
treeMove()
villanMove()
let clickToStartLable = childNodeWithName("clickTostartLable")
clickToStartLable?.removeFromParent()
addGrass()
// star.generateStarWithSpawnTime()
}
}
func changeJump(){
jumpon = false // hero has landed
}
and function update is called in every second and then changeJump must be called
override func update(currentTime: CFTimeInterval) {
if isstarted == true {
let pointsLabel = childNodeWithName("pointsLabel") as MLpoints
pointsLabel.increment()
}
if jumpon == true { // checked everyframe that hero jumped
jumpingTimer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "changeJump", userInfo: nil, repeats: true) // changing jump status
}
}
How should I update my code to make the hero jump only once in three seconds. Thanks in advance.
Some thoughts...
I suggest you use an SKAction or two instead of an NSTimer in Sprite Kit games. SKActions pause/resume appropriately when you pause/resume the scene and/or view
The update method is called ~60 times a second not every second
Checking the status of a jump in update is not need
Alternatively, you can check if the hero is on the ground before starting another jump sequence instead of using a timer. The game may frustrate the user if the hero is on the ground (and ready to jump) but the timer is still counting down
and some code...
if (!jumping) {
jumping = true
let wait = SKAction.waitForDuration(3)
let leap = SKAction.runBlock({
// Play sound and apply impulse to hero
self.jump()
})
let action = SKAction.group([leap, wait])
runAction(action) {
// This runs only after the action has completed
self.jumping = false
}
}

Resources