Problems with SKAction.runBlock() in Swift
when you put snippets in the runBlock statement they need to be within curly braces {} unless they are a simple func call with no parameters
change it to
square.runAction(SKAction.sequence([moveSquare1, SKAction.runBlock( { self.checkIfSquareMatches(self.square) } ),SKAction.removeFromParent()]))
square2.runAction(SKAction.sequence([SKAction.waitForDuration(1.0),SKAction.runBlock( { self.addChild(self.square2 } )) ,moveSquare, SKAction.removeFromParent()]))
}
and the errors go away
Note you must reference variables using self. in runBlock calls
iOS SpriteKit - countdown before game starts?
Added these functions and a call to countdown(5)
at the end of didMoveToView
func countdown(count: Int) {
countdownLabel.horizontalAlignmentMode = .Center
countdownLabel.verticalAlignmentMode = .Baseline
countdownLabel.position = CGPoint(x: size.width/2, y: size.height*(1/3))
countdownLabel.fontColor = SKColor.whiteColor()
countdownLabel.fontSize = size.height / 30
countdownLabel.zPosition = 100
countdownLabel.text = "Launching ball in \(count)..."
addChild(countdownLabel)
let counterDecrement = SKAction.sequence([SKAction.waitForDuration(1.0),
SKAction.runBlock(countdownAction)])
runAction(SKAction.sequence([SKAction.repeatAction(counterDecrement, count: 5),
SKAction.runBlock(endCountdown)]))
}
func countdownAction() {
count--
countdownLabel.text = "Launching ball in \(count)..."
}
func endCountdown() {
countdownLabel.removeFromParent()
ball.physicsBody!.applyImpulse(CGVectorMake(20, 20))
}
So I create and set up the text of the countdown and then create and run an SKAction that waits for 1 second before decrementing the countdown and updating the label. It repeats this 5 times and then removes the countdown label before finally giving the ball an impulse to start it moving and so the game proper can start.
Seems to work ok...
SKAction not restarting
I think your biggest issue is the scene isn't resetting itself properly. I have yet to see someone do what you are doing and re presenting your scene (or call didMoveToView directly which I don't think is ever suppose to be called directly) instead of re initing a new scene. I would give the following a try...
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
/* Called when a touch begins */
if freezeMainChar == 0
{
changeSpeed()
let touch = touches.first
let touchLocation = touch?.locationInNode(self)
if touchLocation!.x == 450
{
//do something
}
}
else
{
score = 0
scoreLabel.text = "0"
movingObjects.removeAllChildren()
movingObjects.removeAllActions()
gameOverLabelHolder.removeAllChildren()
freezeMainChar = 0
movingObjects.speed = 1
self.removeAllActions()
self.removeAllChildren()
//didMoveToView(view!)
let newScene = GameScene(size: size)
newScene.scaleMode = scene.scaleMode
view.presentScene(newScene)
}
}
This should start everything fresh for a new game. Hopefully that helps solve your issue.
How do I use SpriteKit's SKAction to create a time interval for an SKSpriteNode?
Try to use SKAction.waitForDuration(_:)
.
Ex. something like this:
let waitDuration = NSTimeInterval(arc4random_uniform(20))
let waitAction = SKAction.waitForDuration(waitDuration)
let ballAction = SKAction.runBlock(self.Basketball) //call your function
runAction(SKAction.sequence([waitAction, ballAction]))
How to create transition into SKAction?
You can do it like this:
override func didMoveToView(view: SKView) {
colorize()
}
func colorize(){
let colorize = SKAction.sequence([
SKAction.colorizeWithColor(UIColor.randomColor(), colorBlendFactor: 1, duration: 3),
SKAction.runBlock({[unowned self] in self.colorize()})
])
runAction(colorize, withKey: "colorizing")
}
This is recursive function which calls itself every time colorizeWithColor action is finished. This is required due to fact that just repeating this:
SKAction.colorizeWithColor(UIColor.randomColor(), colorBlendFactor: 1, duration: 3)
inside an action sequence will colorize the background always to the same color. That will happen because when you create an action once, you can't change it over time (you can change its speed or pause it for example, but you can't change a duration
or any other passed parameter).Instead, we re-create the action associated with a certain key each time. And this is from the docs about actions associated with keys:
If an action using the same key is already running, it is removed
before the new action is added.
So each time we run a new action, associated with "colorizing" key, the previous action is removed and there will be always only one action with that key.
Related Topics
What Is the Purpose of Firebase Appcheck
Change Duration (Speed) on a Running Animation
Change Splash Screen Image Programmatically
Cocoa Pods Install on iOS Project Not Working
Scaling Physics Bodies in Xcode Spritekit
How to Update iOS 14 Widget Background Color from the App
Longer Subtitles in Mapview Annotations (Swift)
How to Make App Wait Until Firebase Request Is Finished
Uitextfield Starting Cursor Position Is Wrong
Running an iOS7 Project Under Xcode 6 - Image Assets Don't Show Up
How to Send Request from iOS (Swift) to Dialogflow V2 API Without Cloud Functions
Animating Strings Fading In/Out in Swift
Horizontal Paging Uicollectionview with Automatic Item Size in a Vertical Stack View
Change Uipopoverview Background + Arrow Color
Ios: How to Create Expandable Tableview in Swift Without Using Third Party Libraries or Pods