How to get my score counter working in a SpriteKit game - sprite-kit

I am using SpriteKit and Xcode to make a game and the counter is 'counting' but it only updates at the end of the game when you start a new one.
func adjustScore(by points: Int) {
score += points
}
func projectileDidCollideWithMonster(projectile: SKSpriteNode, monster: SKSpriteNode) {
print("Hit")
projectile.removeFromParent()
monster.removeFromParent()
monstersDestroyed += 1
adjustScore(by: 100)
if monstersDestroyed > 30 {
let reveal = SKTransition.flipHorizontal(withDuration: 0.5)
let gameOverScene = GameOverScene(size: self.size, won: true)
view?.presentScene(gameOverScene, transition: reveal)
}
Where have I gone wrong?
answer solved but heres what is after my View Did Load:
override func didMove(to view: SKView) {
background.zPosition = -1
background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
addChild(background)
scoreLabel = SKLabelNode(fontNamed: "Chalkduster")
scoreLabel.text = "Points = \(score)"
scoreLabel.fontSize = 20
scoreLabel.fontColor = SKColor.black
scoreLabel.position = CGPoint(x: size.width/6.2, y: size.height/1.2)
addChild(scoreLabel)
HighScoreLabel = SKLabelNode(fontNamed: "Chalkduster")
HighScoreLabel.text = "High score = \(highScore)"
HighScoreLabel.fontSize = 20
HighScoreLabel.fontColor = SKColor.black
HighScoreLabel.position = CGPoint(x: size.width/5.2, y: size.height/6)
addChild(HighScoreLabel)
NewSkinLabel = SKLabelNode(fontNamed: "Chalkduster")
NewSkinLabel.text = ""
NewSkinLabel.fontSize = 20
NewSkinLabel.fontColor = SKColor.black
NewSkinLabel.position = CGPoint(x: size.width/2, y: size.height/4.4)
addChild(NewSkinLabel)
}

You have two problems. First, you never access scoreLabel outside of didMove so the score label never changes. You have to write code to update the score label, as Alexandru demonstrated in his answer.
Your second problem is that you have two scoreLabel variables, a global var and a local scoreLabel let constant in the didMove function. When you declare scoreLabel in didMove,
let scoreLabel = SKLabelNode(fontNamed: "Chalkduster")
scoreLabel.text = "Total Coins: \(score)"
scoreLabel.fontSize = 40
scoreLabel.fontColor = SKColor.black
scoreLabel.position = CGPoint(x: size.width/2, y: size.height/1.5)
addChild(scoreLabel)
This version of scoreLabel goes away when you exit didMove. Now if you access the global scoreLabel to display the score, the label is empty. You loaded and set everything on the temporary local version.
The easiest way to fix this issue and avoid confusion is to have only one scoreLabel variable. Get rid of the global scoreLabel and declare scoreLabel as a property of the GameScene class. Then in didMove, remove the let to load scoreLabel.
scoreLabel = SKLabelNode(fontNamed: "Chalkduster")
Now the value of scoreLabel will stay around when the scene finishes loading. If you update scoreLabel in adjustScore, the score should update.
One more thing. If you have a response about things not working, you need to provide more information than "I tried this and it didn't work." That is not a helpful response. You have to describe what didn't work or else there will be a lot of back and forth trying to get the relevant information, increasing the time it takes for you to get the solution.

When the projectile hits a monster, you update the score, but you don't update the label. In this case, the label will always be equal to the initial value that you set at the start of your game
When you increment the score, you should also update the score_label.
func adjustScore(by points: Int)
{
score += points
score_label.text = "Points = \(score)"
}

Related

how to carry across values across scenes

I am trying to make a 'money' value show on 3 different scenes, and it is showing on the GameScene but not on the GameOverScene or the ShopScene.
this is the relevant code of the GameScene that is working:
func adjustMoney(by points: Int){
var money = UserDefaults().integer(forKey: "moneyscore")
money += points
MoneyLabel.text = "Money = " + UserDefaults().integer(forKey: "moneyscore").description
UserDefaults().set(money, forKey: "moneyscore")
}
func projectileDidCollideWithMonster(projectile: SKSpriteNode, monster: SKSpriteNode) {
print("Hit")
projectile.removeFromParent()
monster.removeFromParent()
monstersDestroyed += 1
adjustScore(by: 1)
adjustMoney(by: 2)
and this is the total code in the other scenes (there is just the ShopScene as it is the same in the other):
import Foundation
import SpriteKit
var welcomeLabel: SKLabelNode!
class ShopScene: SKScene {
var background = SKSpriteNode(imageNamed: "background")
override func didMove(to view: SKView) {
background.zPosition = -1
background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
addChild(background) welcomeLabel = SKLabelNode(fontNamed: "Chalkduster")
welcomeLabel.text = "Welcome to the shop!"
welcomeLabel.fontSize = 40
welcomeLabel.fontColor = SKColor.black
welcomeLabel.position = CGPoint(x: size.width/2, y: size.height/1.2)
addChild(welcomeLabel)
MoneyLabel = SKLabelNode(fontNamed: "Chalkduster")
MoneyLabel.text = "Money = \(money)"
MoneyLabel.fontSize = 20
MoneyLabel.fontColor = SKColor.black
MoneyLabel.position = CGPoint(x: size.width/6.2, y: size.height/1.35)
addChild(MoneyLabel)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let gameScene = GameScene(size: self.size)
gameScene.scaleMode = .aspectFill
self.view?.presentScene(gameScene, transition: SKTransition.doorsCloseHorizontal(withDuration: 1.0))
}
}
However it only says money = 0
---- I'm not sure if it is of any relevance but before my 'projectile' and 'monster' first collide each game all my values of high score and score and money are 0 then they go back to their saved values, apart from score obviously that goes to 0. Score is the only value that isn't saved and that does move across to the GameOverScene.
You need to access UserDefaults().integer(forKey: "moneyscore") to use this in both the GameOverScene or the ShopScene class as you are updating userdefaults value(money).
This is actually for your specific case only. On the other hand, if you are interested to pass data in different classes(you can mention them as screens if they have views), you can follow different methodologies. You can follow this link for references.

How to change color of SKShapeNode randomly each time spawned?

Currently, when running this code, a colored ball runs from the top of the screen to the bottom. When it reaches the bottom it respawns at the top and drops again. I need the ball to change to a random color each time it spawns again. I have a colorChange action but it still doesn't change the color of the ball when it runs.
import SpriteKit
class GameScene: SKScene {
let ball = SKShapeNode(circleOfRadius: 20)
let label = SKLabelNode(fontNamed: "Futura")
let colorChange = SKAction.colorizeWithColor(.blueColor(), colorBlendFactor: 1, duration: 1)
let randomRed = Int(arc4random_uniform(255))
let randomGreen = Int(arc4random_uniform(255))
let randomBlue = Int(arc4random_uniform(255))
override func didMoveToView(view: SKView) {
let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody = sceneBody
//Ball Transition
let ballTransition = SKAction.sequence([SKAction.fadeInWithDuration(1)])
ball.runAction(ballTransition)
ball.fillColor = UIColor(red: 100, green: 100, blue: 0, alpha: 1)
ball.physicsBody = SKPhysicsBody(circleOfRadius: 25)
ball.physicsBody?.affectedByGravity = false
ball.position = CGPoint(x: self.frame.size.width/2, y: CGFloat(self.frame.size.height*1))
ballMovement()
self.addChild(ball)
self.addChild(label)
}
func ballMovement() {
let moveBall = SKAction.moveToY(0, duration: 3)
let goBackUp = SKAction.moveToY(self.frame.size.height, duration:0)
let keepFalling = SKAction.sequence([moveBall, goBackUp, colorChange])
ball.runAction(SKAction.repeatActionForever(keepFalling))
//Label Sprite
label.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
label.fontColor = SKColor.redColor()
label.fontSize = 30
}
override func update(currentTime: CFTimeInterval) {
label.text = "\(ball.position.y)"
if ball.position.y < 26 {
ball.position = CGPoint(x: self.frame.size.width/2, y: CGFloat(self.frame.size.height*1))
}
}
}
Instead of using an SKAction to change the color, just do
ball.color = UIColor...
To generate a random color check this out: How to make a random background color with Swift
and use that to generate a random color
ball.color = .randomColor()
I suggest you don't use an SKShape for an important piece that moves because I find that they can slow down the app and cause problems. I would use an SKSpritenode instead for the ball. If you then make the ball default as all white, you can change the colour by doing this:
ball.colorBlendFactor = 1
ball.color = SKColor(hue: randomNumberFunctionBetweenZeroAndOne(), saturation: 0.75, contrast: 0.5)
The hue should be a random value between 0 and 1.
If you put this inside a function, you could call the function to change the colour of the ball inside and SKAction or anywhere else.

Touch Sprite, make it jump up then fall down again(repeat as many times as spritenode is tapped.)

I created a project where I have a ball and when the view loads, it falls down, which is good. I'm trying to get the ball to jump back up and fall down again when the Spritenode is tapped.
--This Question was edited--
Originally, I was able to get it to work when sprite.userInteractionEnabled = false. I had to turn this statement true in order to get the score to change.
Now I can't get the balls to fall and be tapped to jump. When I turn ball.physicsBody?.dynamic = true, the balls will fall due to gravity. How do I tap the sprite itself and make it jump.
GameScene.swift (For those who want to try the code for themselves.)
import SpriteKit
class GameScene: SKScene {
var ball: Ball!
private var score = 0 {
didSet { scoreLabel.text = "\(score)" }
}
override func didMoveToView(view: SKView) {
let ball = Ball()
scoreLabel = SKLabelNode(fontNamed:"Geared-Slab")
scoreLabel.fontColor = UIColor.blackColor()
scoreLabel.position = CGPoint( x: self.frame.midX, y: 3 * self.frame.size.height / 4 )
scoreLabel.fontSize = 100.0
scoreLabel.zPosition = 100
scoreLabel.text = String(score)
self.addChild(scoreLabel)
ball.position = CGPoint(x:self.size.width / 2.0, y: 440)
addChild(ball)
ball.physicsBody = SKPhysicsBody(circleOfRadius: 120)
ball.physicsBody?.dynamic = true
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.restitution = 3
ball.physicsBody?.friction = 0
ball.physicsBody?.angularDamping = 0
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.usesPreciseCollisionDetection = true
}
class Ball: SKSpriteNode {
init() {
let texture = SKTexture(imageNamed: "Ball")
super.init(texture: texture, color: .clearColor(), size: texture.size())
userInteractionEnabled = true
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let scene = self.scene as! GameScene
scene.score += 1
}
Before, it was a SKNode being tapped using CGVectorMake(impulse, velocity) now, it's a SKSpriteNode, and I tried using SKAction, but it either does not work, or I'm putting it in the wrong place(touches begin).
I tested your code and it seems that using firstBall.userInteractionEnabled = true is the cause. Without it, it should work. I did some research (here for example), but can't figure out what's the reason of this behavior with userInteractionEnabled. Or for what reason do you use userInteractionEnabled?
Update due to update of question
First ball.physicsBody?.restitution = 3 defines the bounciness of the physics body. The default value is 0.2 and the property must be between 0.0 ans 1.0. So if you set it to 3.0 it will cause some unexpected effects. I just deleted it to use the default value of 0.2.
Second, to make the ball jump after tap and increase the score I put
physicsBody?.velocity = CGVectorMake(0, 600)
physicsBody?.applyImpulse(CGVectorMake(0, 1100))
in the touchesBegan method of the Ball class
Result

How to add score to SpriteKit using SKAction

runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(addScore),
SKAction.waitForDuration(1.0) ]) ))
func addScore() {
let scoreSprite = SKSpriteNode(imageNamed: "scoreSprite")
let actualY = random1(min: scoreSprite.size.height/2, max: size.height - scoreSprite.size.height/2)
scoreSprite.position = CGPoint(x: size.width + scoreSprite.size.width/2, y: actualY)
self.addChild(scoreSprite)
let actualDuration = random1(min: CGFloat(0.5), max: CGFloat(0.5))
let actionMove = SKAction.moveTo(CGPoint(x: -scoreSprite.size.width/2, y: actualY), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
scoreSprite.runAction(SKAction.sequence([actionMove, actionMoveDone])) }
Okay, so I want my score to increase by 1 for every second the person plays the game. I looked into using NSTimeInterval but from what I could gather, the better option is to use SKAction. The only way I could think of was to use an almost clear sprite and to move it across the screen every second. What I want to know is how to make a score increase each time the node moves across the screen, or if this is no good what a better option would be. The code I am using to move the node is above. Thanks in advance.
Try adding this to your didMoveToView method
let wait = SKAction.waitForDuration(1.0)
let incrementScore = SKAction.runBlock ({
++self.score
self.scoreLabel.text = "\(self.score)"
})
self.runAction(SKAction.repeatActionForever(SKAction.sequence([wait,incrementScore])))
I fixed it using by adding this to the end of the scoreSprite function:
if scoreSprite.position.x > endOfScreenLeft {
updateScore()
}
Then did this to update the score:
func updateScore() {
score++
scoreLabel.text = String(score)
}
End of screen left is a CGFloat:
endOfScreenLeft = (self.size.width / 2) * CGFloat(-1)

Swift : Updated Variable in a different scene

I've got a variable in my GameScene, it's score variable, and it's being displayed on screen with SKLabelNode.
Everytime time there is a collision, 1 is being added to score, which is being transfered as a string to the SKLabelNode, and then updated on screen.
Problem is, when I call it from my GameOverScene (scene where the final score is being displayed along with "game over"), I get first value of score, which is 0. Like GameOverScene is reading variable, but not the updated one.
How to get the updated variable? Can anyone help?
Code:
class GameScene: SKScene, SKPhysicsContactDelegate {
// S C O R E !
var score = 0
var scoreText: String = ""
var scoreOnScreen = SKLabelNode()
...
override func didMoveToView(view: SKView) {
// S C O R E
scoreOnScreen.position = CGPoint(x: size.width / 2, y: size.height * 0.8)
scoreText = String(score)
scoreOnScreen.text = scoreText
when collision takes place, score get's +1
func bulletDidCollideWithEnemy(enemy: SKSpriteNode, bullet: SKSpriteNode) {
score++
scoreText = String(score)
scoreOnScreen.text = scoreText
and then, the GameOverScene:
import SpriteKit
import UIKit
class GameOverScene: SKScene {
let GameSceneInstance = GameScene()
let bgImage = SKSpriteNode(imageNamed: "background")
let afraidLogo = SKSpriteNode(imageNamed: "gameoverlogo")
var gameOverLabel = SKLabelNode()
override func didMoveToView(view: SKView) {
GameSceneInstance.scoreOnScreen.text = String(GameSceneInstance.score)
bgImage.position = CGPoint(x: size.width / 2, y: size.height / 2)
bgImage.setScale(0.75)
addChild(bgImage)
afraidLogo.position = CGPoint(x: size.width / 2, y: size.height / 2)
afraidLogo.setScale(0.50)
addChild(afraidLogo)
gameOverLabel.fontSize = 40
gameOverLabel.fontColor = SKColor.whiteColor()
gameOverLabel.text = "score: \(GameSceneInstance.scoreOnScreen.text)"
gameOverLabel.position = CGPointMake(self.size.width/2, 2.0 / 3.0 * self.size.height);
addChild(gameOverLabel)
}
I bet it's just a simple issue with the code. Appreciate any help.
I figured it out with some help from a reddit guy with a nickname Jasamer. Guy rocks.
Here's what I've changed:
when the scene is being changed
let scene = GameOverScene(size: skView.bounds.size)
I added:
scene.gameScene = self
scene.score = score
and then, the GameOverScene:
class GameOverScene: SKScene {
var gameScene = GameScene()
override func didMoveToView(view: SKView) {
gameOverLabel.text = "score: \(gameScene.score)"
and it works.
The main problem I think was not setting scene.GameScene = self.
I hope this helps someone someday.