I'm trying to update the coinLabel, so that when the "life" node is tapped, it will update the SKLabelNode and display the correct coins after they have been subtracted from the total. I'm new to coding, so if there is anything else wrong with this please reply! Thanks!
import Foundation
import SpriteKit
class ShopScene: SKScene {
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background")
background.size = self.size
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
let mainMenu = SKLabelNode(fontNamed: "The Bold Font")
mainMenu.text = "Main Menu"
mainMenu.fontSize = 100
mainMenu.fontColor = SKColor.darkGray
mainMenu.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.3)
mainMenu.zPosition = 1
mainMenu.name = "Main Menu"
self.addChild(mainMenu)
let Life = SKLabelNode(fontNamed: "The Bold Font")
Life.text = "Click here to buy 1 life!"
Life.fontSize = 130
Life.fontColor = SKColor.darkGray
Life.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.6)
Life.zPosition = 1
Life.name = "Life"
self.addChild(Life)
let coinLabel = SKLabelNode(fontNamed: "The Bold Font")
coinLabel.text = "Coins: \(coinNumber)"
coinLabel.fontSize = 100
coinLabel.fontColor = SKColor.black
coinLabel.zPosition = 1
coinLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1)
self.addChild(coinLabel)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointOfTouch = touch.location(in: self)
let nodeITapped = atPoint(pointOfTouch)
if nodeITapped.name == "Main Menu" {
let sceneToMoveTo = MainMenuScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTrasition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTrasition)
}
if nodeITapped.name == "Life" {
if coinNumber > 10 {
lifeNumber += 1
coinNumber -= 10
defaults.set(coinNumber, forKey: "coinNumberSaved")
defaults.set(lifeNumber, forKey: "lifeNumberSaved")
return
}
}
}
}
}
Just update your text in your tap recognizer again to change it
coinLabel.text = "Coins: \(coinNumber)"
You will need to make sure you have a reference of your coinLabel outside of your init so you can manipulate it outside of it.
At the top of your ShopScene you can do...
class ShopScene: SKScene {
var coinLabel = SKLabelNode()
//your code
}
and then when you go to init in your didMove(to view:) func, do instead...
coinLabel = SKLabelNode(fontNamed: "The Bold Font")
That way you can have a reference of it throughout your code. Although in the future you will want to think about making a subclass of SKNode to handle all your UI elements so you don't init everything in your GameScene.
Related
I have a SpriteKit game and I am using a SKAction to move my meteor (the meteors are the enemies) all the way to the end of the screen where the player (the player is at the bottom of the screen, and they can only move on the x-axis to dodge meteors not the y) must dodge these meteors. Right I'm only trying to get one meteor on the screen at a time which moves towards the direction of the player using SKAction. There should only be on meteor at a time on the screen, but I get the error 'Attempted to add a SKNode which already has a parent'. My SKAction adds the meteor to the screen but then deletes it after the meteor has moved to its destination which is all then way to the bottom of the screen. So why am I getting this error and how can I fix it?
NOTE: The SKAction removes the meteor from the screen by using meteor.removeFromParent()
Here is my code:
import SpriteKit
import GameplayKit
import UIKit
class GameScene: SKScene, SKPhysicsContactDelegate{
let player = SKSpriteNode(imageNamed: "spaceship")
let stars = SKSpriteNode(imageNamed: "stars")
let meteor = SKSpriteNode(imageNamed: "meteor")
var scoreLabel = SKLabelNode(fontNamed: "AmericanTypewriter-Bold")
var score:Int = 0
var playerLost:Bool = false
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
print(frame.size.width)
print(frame.size.height)
stars.position = CGPoint(x:0, y:0)
stars.zPosition = 1
player.position = CGPoint(x:0, y:-320)
player.zPosition = 4
player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size)
player.physicsBody?.affectedByGravity = false
player.physicsBody?.isDynamic = false
player.physicsBody?.categoryBitMask = 2
player.physicsBody?.collisionBitMask = 2
player.physicsBody?.contactTestBitMask = 1
self.addChild(player)
self.addChild(stars)
self.addMeteor()
self.setLabel()
//spawnEnemySKAction()
}
func spawnEnemySKAction() {
let spawn = SKAction.run(addMeteor)
let waitToSpawn = SKAction.wait(forDuration: 1)
let spawnSequence = SKAction.sequence([spawn, waitToSpawn])
let spawnForever = SKAction.repeatForever(spawnSequence)
self.run(spawnForever)
}
func didBegin(_ contact: SKPhysicsContact) {
gameOver()
}
func addMeteor() {
meteor.physicsBody = SKPhysicsBody(texture: meteor.texture!, size: meteor.size)
meteor.physicsBody?.affectedByGravity = false
meteor.setScale(0.50)
meteor.position = CGPoint(x:Int(arc4random()%300),y:Int(arc4random()%600))
//meteor.position = CGPoint(x:0 , y:0)
meteor.zPosition = 4
let moveMeteor = SKAction.moveTo(y: player.position.y - 300, duration: 1.5)
let deleteMeteor = SKAction.removeFromParent()
let meteorSequence = SKAction.sequence([moveMeteor, deleteMeteor])
meteor.run(meteorSequence)
meteor.physicsBody?.categoryBitMask = 1
meteor.physicsBody?.collisionBitMask = 2
meteor.physicsBody?.contactTestBitMask = 2
self.addChild(meteor)
}
func fireBullet() {
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.position = player.position
bullet.setScale(0.5)
bullet.zPosition = 3
self.addChild(bullet)
let moveBullet = SKAction.moveTo(y: self.size.height + bullet.size.height, duration: 1)
let deleteBullet = SKAction.removeFromParent()
let bulletSequence = SKAction.sequence([moveBullet, deleteBullet])
bullet.run(bulletSequence)
}
func setLabel() {
scoreLabel.text = "Score: " + String(score)
scoreLabel.position = CGPoint(x: 290, y: 590)
scoreLabel.zPosition = 20
}
func gameOver() {
playerLost = true
var gameOverLabel: SKLabelNode!
gameOverLabel = SKLabelNode(fontNamed: "AmericanTypewriter-Bold")
gameOverLabel.text = "Game Over! You lost! Your score was: " + String(score)
gameOverLabel.zPosition = 10
gameOverLabel.position = CGPoint(x: 0, y:0)
self.addChild(gameOverLabel)
player.removeFromParent()
meteor.removeFromParent()
stars.removeFromParent()
scoreLabel.removeFromParent()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if playerLost == false {
fireBullet()
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointOfTouch = touch.location(in: self)
let previousPointOfTouch = touch.previousLocation(in: self)
let amountDragged = pointOfTouch.x - previousPointOfTouch.x
player.position.x += amountDragged
}
}
override func update(_ currentTime: TimeInterval) {
addMeteor()
if meteor.position.y < player.position.y - 300 && playerLost == false {
meteor.removeFromParent()
addMeteor()
if playerLost == false {
score += 1
}
scoreLabel.removeFromParent()
scoreLabel.text = "Score: " + String(score)
self.addChild(scoreLabel)
}
}
}
Thanks for taking the time to look at this question!
I simply had to remove the self.addChild(meteor) in the update function
So I'm in the early stages of making an incremental game, I've been doing testing to understand how I could make the upgrades and such work. I'm trying to have it happen that when you buy the first upgrade it adds 1 coin every second, but the while statement is giving me this error in the debugger "Terminated due to memory issue" I believe this is due to the while statement. Please help and thanks!
//
// MainMenuScene.swift
// SoloPlane
//
// Created by -Zach on 7/27/16.
// Copyright © 2016 -Zach. All rights reserved.
//
import Foundation
import SpriteKit
import AVFoundation
import GameController
let defaults = UserDefaults()
var coins = defaults.integer(forKey: "coinsSaved")
let coinLabel = SKLabelNode(fontNamed: "The Bold Font")
var upgrade1 = defaults.integer(forKey: "upgrade1Saved")
class MainMenuScene: SKScene {
override func didMove(to view: SKView) {
//var backingAudio = AVAudioPlayer()
let background = SKSpriteNode(imageNamed: "background")
background.size = self.size
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
let gameTitle1 = SKLabelNode(fontNamed: "The Bold Font")
gameTitle1.text = "The Kingdom"
gameTitle1.fontSize = 200
gameTitle1.fontColor = SKColor.darkGray
gameTitle1.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.9)
gameTitle1.zPosition = 1
self.addChild(gameTitle1)
let Shop = SKLabelNode(fontNamed: "The Bold Font")
Shop.text = "Shop"
Shop.fontSize = 125
Shop.fontColor = SKColor.darkGray
Shop.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.3)
Shop.zPosition = 1
Shop.name = "Shop"
self.addChild(Shop)
let beginGame = SKLabelNode(fontNamed: "The Bold Font")
beginGame.text = "Coins"
beginGame.fontSize = 150
beginGame.fontColor = SKColor.darkGray
beginGame.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.4)
beginGame.zPosition = 1
beginGame.name = "Coins"
self.addChild(beginGame)
let Game = SKLabelNode(fontNamed: "The Bold Font")
Game.text = "Upgrade1"
Game.fontSize = 150
Game.fontColor = SKColor.darkGray
Game.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.2)
Game.zPosition = 1
Game.name = "Upgrade1"
self.addChild(Game)
coinLabel.text = "Coins: \(coins)"
coinLabel.fontSize = 100
coinLabel.fontColor = SKColor.black
coinLabel.zPosition = 1
coinLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1)
self.addChild(coinLabel)
//if backingAudio.playing {
/*
let settings = SKLabelNode(fontNamed: "The Bold Font")
settings.text = "Settings"
settings.fontSize = 150
settings.fontColor = SKColor.darkGrayColor()
settings.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.3)
settings.zPosition = 1
settings.name = "settings"
self.addChild(settings)
*/
while upgrade1 >= 1{
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1) ) {
coins += 1
}
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointOfTouch = touch.location(in: self)
let nodeITapped = atPoint(pointOfTouch)
if nodeITapped.name == "Coins" {
coins += 1
coinLabel.text = "Coins: \(coins)"
defaults.set(coins, forKey: "coinsSaved")
/*let sceneToMoveTo = GameScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTrasition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTrasition)*/
}
if nodeITapped.name == "Upgrade1" {
upgrade1 += 1
defaults.set(upgrade1, forKey: "upgrade1Saved")
}
if nodeITapped.name == "wood" {
/* let sceneToMoveTo = ShopScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTrasition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTrasition) */
}
if nodeITapped.name == "Credits" {
/*
let sceneToMoveTo = CreditsScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTrasition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTrasition) */
}
while (upgrade1 == 1){
//DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1) ) {
sleep(1)
upgrade1 += 1
coins += 1
coinLabel.text = "Coins: \(coins)"
defaults.set(coins, forKey: "coinsSaved")
}
}
}
}
I can see you were trying to use DispatchQueue and asyncAfter but don't know the right way to do it.
You can have a async helper method call that itself recursively to achieve the async while effect.
func update () {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1) ) {
if upgrade1 == 1 {
self.upgrade1 += 1
self.coins += 1
self.coinLabel.text = "Coins: \(coins)"
self.defaults.set(coins, forKey: "coinsSaved")
self.update() // schedule another update after 1 second
}
}
}
and replace the while loop with update()
You may need to tweak the position of if upgrade1 == 1
I'm trying to change the image of the SKSpriteNode in my shop scene when an upgrade is bought, I believe I'm on the right track with the variable, but I think I am doing it wrong. Thanks for the help in advanced, I've been trying for a while to figure it out with no luck.
// ShopScene.swift
// TheLastFlight
//
// Created by -Zachary Walensa- on 8/3/16.
// Copyright © 2016 -Zachary Walensa-. All rights reserved.
//
import Foundation
import SpriteKit
var coinUpgrade = defaults.integer(forKey: "coinUpgradeSaved")
var missileUpgrade = defaults.integer(forKey: "missileUpgradeSaved")
class ShopScene: SKScene {
var coinLabel = SKLabelNode(fontNamed: "The Bold Font")
var missileUpgrades = SKSpriteNode(imageNamed: "missile1")
override func didMove(to view: SKView) {
coinLabel.text = "Coins: \(coinNumber)"
coinLabel.fontSize = 100
coinLabel.fontColor = SKColor.black
coinLabel.zPosition = 1
coinLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1)
self.addChild(coinLabel)
let background = SKSpriteNode(imageNamed: "background")
background.size = self.size
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
let mainMenu = SKLabelNode(fontNamed: "The Bold Font")
mainMenu.text = "Main Menu"
mainMenu.fontSize = 100
mainMenu.fontColor = SKColor.darkGray
mainMenu.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.3)
mainMenu.zPosition = 1
mainMenu.name = "Main Menu"
self.addChild(mainMenu)
let Life = SKSpriteNode(imageNamed: "lifeButton")
Life.position = CGPoint(x: self.size.width*0.3, y: self.size.height*0.8)
Life.zPosition = 1
Life.name = "Life"
self.addChild(Life)
let coinUpgrades = SKSpriteNode(imageNamed: "coinUpgrade")
coinUpgrades.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.8)
coinUpgrades.zPosition = 1
coinUpgrades.name = "Coin"
self.addChild(coinUpgrades)
missileUpgrades.position = CGPoint(x: self.size.width*0.8, y: self.size.height*0.8)
missileUpgrades.zPosition = 1
missileUpgrades.name = "missile"
self.addChild(missileUpgrades)
/*let coinLabel = SKLabelNode(fontNamed: "The Bold Font")
coinLabel.text = "Coins: \(coinNumber)"
coinLabel.fontSize = 100
coinLabel.fontColor = SKColor.black
coinLabel.zPosition = 1
coinLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1)
self.addChild(coinLabel) */
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointOfTouch = touch.location(in: self)
let nodeITapped = atPoint(pointOfTouch)
if nodeITapped.name == "Main Menu" {
let sceneToMoveTo = MainMenuScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTrasition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTrasition)
}
if nodeITapped.name == "Life" {
if coinNumber >= 50 {
lifeNumber += 1
coinNumber -= 50
defaults.set(coinNumber, forKey: "coinNumberSaved")
defaults.set(lifeNumber, forKey: "lifeNumberSaved")
coinLabel.text = "Coins: \(coinNumber)"
}
}
if nodeITapped.name == "Coin" {
if coinNumber >= 600 {
coinNumber -= 600
defaults.set(coinNumber, forKey: "coinNumberSaved")
coinUpgrade = 1
defaults.set(coinUpgrade, forKey: "coinUpgradeSaved")
coinLabel.text = "Coins: \(coinNumber)"
}
}
if nodeITapped.name == "missile" {
if missileUpgrade == 2 {
missileUpgrade = 1
defaults.set(missileUpgrade, forKey: "missileUpgradeSaved")
if missileUpgrade == 1 {
missileUpgrade = 2
defaults.set(missileUpgrade, forKey: "missileUpgradeSaved")
}
}
else if missileUpgrade == 0 || coinNumber >= 1400 {
coinNumber -= 1400
defaults.set(coinNumber, forKey: "coinNumberSaved")
missileUpgrade = 2
defaults.set(missileUpgrade, forKey: "missileUpgradeSaved")
coinLabel.text = "Coins: \(coinNumber)"
missileUpgrades = SKSpriteNode(imageNamed: "missile")
}
}
}
}
}
There are a few ways to do this:
1) Create a Boolean variable that switches to true when the condition is met. Then in the update function, you can constantly check for if the condition is met:
var conditionIsMet = false
// Your code
//
//...
override func update(_ currentTime: CFTimeInterval) {
if conditionIsMet {
// Change your texture here
}
}
2) Where you get the trigger for when you want to change the texture, you can simply change the texture as part of the code/function that runs. That means that if you are trying to do this on a touch, you would add the code to change the texture there.
FYI: To change the texture just do something like this:
node.texture = SKTexture(imageNamed: "image name goes here")
Im trying to create a method that when I touch a sprite named StartSprite Through my touchesbegan function it will print out something in my console. But for some reason when I click on the sprite nothing happens. This is my code.
import SpriteKit
class GameScene: SKScene {
let StartSprite = SKSpriteNode(imageNamed: "startLabel")
override func didMoveToView(view: SKView) {
let borderRect = CGRect(x: 0 , y: 0 , width: 400, height: 725)
let welcomeLabel = SKLabelNode(fontNamed: "welcome");
welcomeLabel.text = "Welcome";
welcomeLabel.fontColor = UIColor.whiteColor()
welcomeLabel.fontSize = 65
welcomeLabel.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 150)
self.addChild(welcomeLabel)
//StartLabel
let rectangleBorder = SKShapeNode(rect: borderRect)
rectangleBorder.position = CGPoint(x: 315, y: 25)
rectangleBorder.strokeColor = UIColor.whiteColor()
self.addChild(rectangleBorder)
self.backgroundColor = UIColor.grayColor()
StartSprite.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
self.addChild(StartSprite)
println("Hello")
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
if touchLocation == StartSprite.position{
println("Touches")
}
}
}
Your code requires that you touch exactly the one pixel at StartSprite.position.
Try this instead:
let p = StartSprite.convertPoint(touchLocation, fromNode: self)
if StartSprite.containsPoint(p) {
print("touched StartSprite")
}
If you're going to add more buttons, you might want to do this instead:
let target = nodeAtPoint(touchLocation)
if target == StartSprite {
print("touched StartSprite"
}
// check other buttons here
I just started to learn Swift and Sprite Kit. I need assistance with the following code.
I have a countUp that when the ball is touched the countUp starts but my problem is that every time I tap the ball the rate of speed in the countUp increases. I want the CountUp to be stable. Any help will be appreciated.
class Game: SKScene {
var Ball = SKSpriteNode(imageNamed: "Red.png")
var QuitOption = SKLabelNode()
var ScoreLabel = SKLabelNode()
var timescore = Int()
var timesecond = Int()
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.blackColor() // background for the display
self.physicsWorld.gravity = CGVectorMake(0, -9.8)
let SceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
SceneBody.friction = 0
self.physicsBody = SceneBody
Ball.size = CGSize(width: 120, height: 120)
Ball.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.7)
Ball.physicsBody = SKPhysicsBody(circleOfRadius: 60)
Ball.physicsBody?.affectedByGravity = true
Ball.physicsBody?.restitution = 0.5
Ball.physicsBody?.linearDamping = 0
Ball.name = "Ball"
self.addChild(Ball)
QuitOption.text = "Quit"
QuitOption.fontName = "Noteworthy-Light"
QuitOption.fontColor = SKColor.greenColor()
QuitOption.fontSize = 35
QuitOption.position = CGPoint(x: self.frame.size.width/2 - 160, y: self.frame.size.height*1 - 110)
QuitOption.name = "Quit"
addChild(QuitOption)
ScoreLabel = SKLabelNode(fontNamed: "Noteworthy-Light")
ScoreLabel.fontSize = 50 // The + will move it to the right side and - to the left side for more accuracy.
ScoreLabel.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/4 + 400) // position of ScoreLabelNode
ScoreLabel.name = "Score+"
ScoreLabel.hidden = false
self.addChild(ScoreLabel)
}
// Making the ball jump after user touches ball
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.first as! UITouch
var location = touch.locationInNode(self)
var node = self.nodeAtPoint(location)
if (node.name == "Quit"){
let myScene = GameScene(size: self.size)
myScene.scaleMode = scaleMode
let reveal = SKTransition.fadeWithDuration(1)
self.view?.presentScene(myScene, transition: reveal)
}
if (node.name == "Ball"){
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
Ball.physicsBody?.allowsRotation = true
Ball.physicsBody?.velocity = CGVectorMake(0, 0)
Ball.physicsBody?.applyImpulse(CGVectorMake(0, 100))
}
}
var actionwait = SKAction.waitForDuration(0.5)
var actionrun = SKAction.runBlock({
self.timescore++
self.timesecond++
if self.timesecond == 60 {self.timesecond = 0}
self.ScoreLabel.text = " \(self.timescore/60):\(self.timesecond)"})
ScoreLabel.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))
}
}
I guess that's happening because you are running same block over and over again after every touch. To ensure you run block only once, you can use some Bool indicator and after the first run, set its value appropriately, like this:
if(!self.locked){
self.locked = true // at start, this value should be false
var actionrun = SKAction.runBlock({
self.timescore++
self.timesecond++
if self.timesecond == 60 {self.timesecond = 0}
self.ScoreLabel.text = " \(self.timescore/60):\(self.timesecond)"})
ScoreLabel.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))
}
}
Something as an addition to this topic and for future readers could be found here.