How to make text appear on button? -SpriteKit - swift

I am new to SpriteKit, and I am creating a game where I have designed the main menu with sketch and apply the image to spritekit as my background image. Everytime I put a label/text above the button images. It doesn't work properly. When they tap on play lets say, they will play and when they lose, and start over all of sudden the labels disappear.
Here is an image:
Here is my code
//creating the start game programmatically.
let dw_startButton = SKSpriteNode()
dw_startButton.name = "dw_startbutton"
dw_startButton.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(dw_startButton)
//Instructions Button
let dw_selector = SKSpriteNode()
dw_selector.name = "dw_selector"
dw_selector.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(dw_selector)
//Starting text
let startText = SKLabelNode(text: "Play!")
startText.fontColor = UIColor.white
startText.position = CGPoint(x: 50, y: 50)
startText.fontSize = 45
startText.fontName = "Helvetica-Bold"
startText.verticalAlignmentMode = SKLabelVerticalAlignmentMode(rawValue: 1)!
startText.name = "dw_startbutton"
dw_startButton.addChild(startText)
//instructions text
let startTexts = SKLabelNode(text: "Insturctions")
startTexts.fontColor = UIColor.white
startTexts.position = CGPoint(x: 0, y: 0)
startTexts.fontSize = 20
startTexts.fontName = "Helvetica-Bold"
startTexts.verticalAlignmentMode = SKLabelVerticalAlignmentMode(rawValue: 1)!
startTexts.name = "dw_selector"
dw_selector.addChild(startTexts)

You always need to add zPosition to your sprites and labels. Otherwise sometimes they will appear and other times they may appear behind the background
//creating the start game programmatically.
let dw_startButton = SKSpriteNode()
dw_startButton.zPosition = 1
dw_startButton.name = "dw_startbutton"
dw_startButton.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(dw_startButton)
//Instructions Button
let dw_selector = SKSpriteNode()
dw_selector.name = "dw_selector"
dw_selector.zPosition = 1
dw_selector.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(dw_selector)
//Starting text
let startText = SKLabelNode(text: "Play!")
startText.fontColor = UIColor.white
startText.position = CGPoint(x: 50, y: 50)
startText.fontSize = 45
startText.fontName = "Helvetica-Bold"
startText.verticalAlignmentMode = SKLabelVerticalAlignmentMode(rawValue: 1)!
startText.name = "dw_startbutton"
startText.zPosition = 2
dw_startButton.addChild(startText)
//instructions text
let startTexts = SKLabelNode(text: "Insturctions")
startTexts.fontColor = UIColor.white
startTexts.position = CGPoint(x: 0, y: 0)
startTexts.fontSize = 20
startTexts.fontName = "Helvetica-Bold"
startTexts.verticalAlignmentMode = SKLabelVerticalAlignmentMode(rawValue: 1)!
startTexts.name = "dw_selector"
startTexts.zPosition = 2
dw_selector.addChild(startTexts)
//new button
let newButton = SKSpriteNode(imageNamed: "button_back")
newButton.zPosition = 1
newButton.name = "button1"
newButton.position = CGPoint(x: 100, y: 100)
self.addChild(newButton)
let buttonText = SKLabelNode(text: "Play")
buttonText.fontColor = .white
buttonText.fontSize = 20
buttonText.fontName = "Helvetica-Bold"
buttonText.verticalAlignmentMode = .center
buttonText.horizontalAlignmentMode = .center
buttonText.name = "dw_selector"
buttonText.zPosition = 2
newButton.addChild(buttonText)

Related

How to fill out an SKLabelNode with a specific color through animation?

I've been searching the internet for a while now and I couldn't come up with any solution.
I've created a SKLabelNode in my SpriteKit project which displays level numbers.
SKLabelNode:
let levelLbl = SKLabelNode(fontNamed: "LEMON-Bold")
levelLbl.text = "1"
levelLbl.fontSize = 100
levelLbl.fontColor = .grey
levelLbl.zPosition = 2
levelLbl.horizontalAlignmentMode = .center
levelLbl.verticalAlignmentMode = .center
levelLbl.position = CGPoint(x: frame.midX, y: frame.height * 0.75)
addChild(levelLbl)
Screenshot:
Screenshot of my label
I want to fill up the number of my label smoothly with white color from the bottom to the top with an animation so that after x seconds the label is completely white.
I don't want the color to fade in but rather to be filled up similar to a cup of water.
It would be amazing if someone could help me out here!
This is how far I came by using a SKCropNode. My problem is I don't know why I don't see an animation:
func labelAnimation() {
let levelLbl = SKLabelNode(fontNamed: "LEMON-Bold")
let blackRect = SKShapeNode(rectOf: CGSize(width: levelLbl.frame.width, height: levelLbl.frame.height))
blackRect.fillColor = .grey
blackRect.strokeColor = .grey
blackRect.position = CGPoint(x: 0, y: levelLbl.frame.height / 2)
let whiteRect = SKShapeNode(rectOf: CGSize(width: levelLbl.frame.width, height: levelLbl.frame.height))
whiteRect.fillColor = .white
whiteRect.strokeColor = .white
whiteRect.position = CGPoint(x: 0, y: -levelLbl.frame.height / 2)
//Level Node
levelNode.position = CGPoint(x: 0, y: 0)
levelNode.addChild(blackRect)
levelNode.addChild(whiteRect)
//Mask Node
levelLbl.text = "\(levelNr)"
levelLbl.fontSize = 100
levelLbl.horizontalAlignmentMode = .center
levelLbl.verticalAlignmentMode = .center
//Crop Node
cropNode.addChild(levelNode)
cropNode.maskNode = levelLbl
cropNode.position = CGPoint(x: frame.midX, y: frame.midY)
cropNode.zPosition = 5
addChild(cropNode)
//action
let movelevelNode = SKAction.move(to: CGPoint(x: 0, y: frame.height / 3), duration: 8)
levelNode.run(movelevelNode)
}

swift even/odd uibezierpath with line

I have a rectangle and I want to cut it out by a line.
I can cut it out by another rectangle etc.. but not with a line.
I set the linewidth on 20 and use the exact same code as I do always but I really can't figure out how to do it with a line.
Here is my code:
let total = CALayer()
total.frame = UIScreen.main.bounds
let path1 = UIBezierPath(rect: UIScreen.main.bounds)
let path2 = UIBezierPath()
path2.move(to: CGPoint(x: 30, y: 30))
path2.addLine(to: CGPoint(x: 30, y: 100))
path2.lineWidth = 20
path1.append(path2)
path1.usesEvenOddFillRule = true
let bgLayer = CAShapeLayer()
bgLayer.path = path1.cgPath
bgLayer.fillColor = UIColor.black.cgColor
bgLayer.fillRule = kCAFillRuleEvenOdd
total.addSublayer(bgLayer)
imageview.layer.mask = total

How to create a subview inside an SKScene?

Before "Buy Ship" is pressed So I am trying to create a shop where players can buy new ships. When the player hits the "buy button" image I want a combination of images and texts to become visible which acts as a sort of conformation screen. Hopefully you can see here what I mean. I would also greatly appreciate if you could tell me how to dim everything besides the conformation box. After "Buy Ship" is pressed.
This is what my code looks like so far:
import Foundation
import SpriteKit
class ShopPage1: SKScene{
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background")
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
let balance = SKLabelNode(fontNamed: "The Bold Font")
balance.text = "$\(balanceAmount)"
balance.fontSize = 100
balance.fontColor = SKColor.green
balance.position = CGPoint(x: self.size.width/2, y: self.size.height*0.87)
balance.zPosition = 1
self.addChild(balance)
let backToMainMenuButton = SKSpriteNode(imageNamed: "backButton2")
backToMainMenuButton.position = CGPoint(x: self.size.width*0.25, y: self.size.height*0.89)
backToMainMenuButton.zPosition = 1
backToMainMenuButton.size = CGSize(width: 200, height: 200)
backToMainMenuButton.name = "backToMainMenuButton"
self.addChild(backToMainMenuButton)
let shipNameLabel = SKLabelNode(fontNamed: "The Bold Font")
shipNameLabel.text = "Stealth"
shipNameLabel.fontSize = 200
shipNameLabel.fontColor = SKColor.white
shipNameLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.60)
shipNameLabel.zPosition = 1
shipNameLabel.name = "shipNameLabel"
self.addChild(shipNameLabel)
let nextShipButton = SKSpriteNode(imageNamed: "nextShipButton1")
nextShipButton.position = CGPoint(x: self.size.width*0.75, y: self.size.height*0.40)
nextShipButton.zPosition = 1
nextShipButton.size = CGSize(width: 300, height: 300)
nextShipButton.name = "nextShipButton"
self.addChild(nextShipButton)
let nextShipClick = SKLabelNode(fontNamed: "The Bold Font")
nextShipClick.text = "▲"
nextShipClick.fontSize = 300
nextShipClick.fontColor = UIColor.clear
nextShipClick.position = CGPoint(x: self.size.width*0.753, y: self.size.height*0.36)
nextShipClick.zPosition = 2
nextShipClick.name = "nextShipClick"
self.addChild(nextShipClick)
let shipForSale = SKSpriteNode(imageNamed: "playerShip4")
shipForSale.position = CGPoint(x: self.size.width/2, y: self.size.height*0.40)
shipForSale.zPosition = 1
shipForSale.size = CGSize(width: 150, height: 300)
self.addChild(shipForSale)
let shipPodium = SKSpriteNode(imageNamed: "shipPodium")
shipPodium.position = CGPoint(x: self.size.width*0.527, y: self.size.height*0.31)
shipPodium.zPosition = 1
shipPodium.size = CGSize(width: 1200, height: 70)
self.addChild(shipPodium)
let shipsCostLabel = SKLabelNode(fontNamed: "The Bold Font")
shipsCostLabel.text = "$500"
shipsCostLabel.fontSize = 200
shipsCostLabel.fontColor = SKColor.white
shipsCostLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.20)
shipsCostLabel.zPosition = 1
self.addChild(shipsCostLabel)
let shipBuyButton = SKSpriteNode(imageNamed: "shipBuyButton")
shipBuyButton.position = CGPoint(x: self.size.width*0.54, y: self.size.height*0.15)
shipBuyButton.zPosition = 1
shipBuyButton.size = CGSize(width: 1500, height: 900)
shipBuyButton.name = "shipBuyButton"
self.addChild(shipBuyButton)
let conformationBackground = SKSpriteNode(imageNamed: "conformationBackground")
conformationBackground.position = CGPoint(x: self.size.width*0.51, y: self.size.height*0.40)
conformationBackground.zPosition = 2
conformationBackground.size = CGSize(width: 1300, height: 1400)
conformationBackground.name = "conformationBackground"
self.addChild(conformationBackground)
let conformationScreenTextTop = SKLabelNode(fontNamed: "The Bold Font")
conformationScreenTextTop.text = "Are you sure you wish to"
conformationScreenTextTop.fontSize = 80
conformationScreenTextTop.fontColor = SKColor.white
conformationScreenTextTop.position = CGPoint(x: self.size.width/2, y: self.size.height*0.46)
conformationScreenTextTop.zPosition = 3
self.addChild(conformationScreenTextTop)
let conformationScreenTextBottom = SKLabelNode(fontNamed: "The Bold Font")
conformationScreenTextBottom.text = "pruchase this ship?"
conformationScreenTextBottom.fontSize = 80
conformationScreenTextBottom.fontColor = SKColor.white
conformationScreenTextBottom.position = CGPoint(x: self.size.width/2, y: self.size.height*0.41)
conformationScreenTextBottom.zPosition = 3
self.addChild(conformationScreenTextBottom)
let conformationScreenTextYes = SKLabelNode(fontNamed: "The Bold Font")
conformationScreenTextYes.text = "Yes"
conformationScreenTextYes.fontSize = 150
conformationScreenTextYes.fontColor = SKColor.green
conformationScreenTextYes.position = CGPoint(x: self.size.width*0.30, y: self.size.height*0.30)
conformationScreenTextYes.zPosition = 3
self.addChild(conformationScreenTextYes)
let conformationScreenTextNo = SKLabelNode(fontNamed: "The Bold Font")
conformationScreenTextNo.text = "No"
conformationScreenTextNo.fontSize = 150
conformationScreenTextNo.fontColor = SKColor.red
conformationScreenTextNo.position = CGPoint(x: self.size.width*0.70, y: self.size.height*0.30)
conformationScreenTextNo.zPosition = 3
self.addChild(conformationScreenTextNo)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches{
let pointOfTouch = touch.location(in: self)
let tappedNode = atPoint(pointOfTouch)
let tappedNodeName = tappedNode.name
if tappedNodeName == "nextShipClick"{
let sceneToMoveTo = ShopPage2(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}
if tappedNodeName == "backToMainMenuButton"{
let sceneToMoveTo = MainMenuScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}
}
}
}
As Whirlwind said in his comment you can create an SKNode to the center of your scene (position CGPoint.zero) prepare it with all your stuff and hide it with the alpha properties set to 0 and put it's zPosition to -1 (under all the other visible nodes of your scene).
So when you button will be pressed, you simply change the zPosition to an highest value and the alpha to 1. To make more realistic these action s you could use some animations like:
extension UIView {
func fadeIn(_ duration:TimeInterval=1.0) {
UIView.animate(withDuration: duration, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = 1.0 // Instead of a specific instance of, say, birdTypeLabel, we simply set [thisInstance] (ie, self)'s alpha
}, completion: nil)
}
func fadeOut(_ duration:TimeInterval=1.0) {
UIView.animate(withDuration: duration, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.alpha = 0.0
}, completion: nil)
}
}
I have figured out how to get the conformation screen to work. Thanks to #AlessandroOrnano for all the help! I have attached my code below for future reference. If anyone has a more efficient way of doing this I would greatly appreciate any suggestions.
import Foundation
import SpriteKit
class ShopPage1: SKScene{
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background")
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
let balance = SKLabelNode(fontNamed: "The Bold Font")
balance.text = "$\(balanceAmount)"
balance.fontSize = 100
balance.fontColor = SKColor.green
balance.position = CGPoint(x: self.size.width/2, y: self.size.height*0.87)
balance.zPosition = 1
self.addChild(balance)
let backToMainMenuButton = SKSpriteNode(imageNamed: "backButton2")
backToMainMenuButton.position = CGPoint(x: self.size.width*0.25, y: self.size.height*0.89)
backToMainMenuButton.zPosition = 1
backToMainMenuButton.size = CGSize(width: 200, height: 200)
backToMainMenuButton.name = "backToMainMenuButton"
self.addChild(backToMainMenuButton)
let shipNameLabel = SKLabelNode(fontNamed: "The Bold Font")
shipNameLabel.text = "Stealth"
shipNameLabel.fontSize = 200
shipNameLabel.fontColor = SKColor.white
shipNameLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.60)
shipNameLabel.zPosition = 1
shipNameLabel.name = "shipNameLabel"
self.addChild(shipNameLabel)
let nextShipButton = SKSpriteNode(imageNamed: "nextShipButton1")
nextShipButton.position = CGPoint(x: self.size.width*0.75, y: self.size.height*0.40)
nextShipButton.zPosition = 1
nextShipButton.size = CGSize(width: 300, height: 300)
nextShipButton.name = "nextShipButton"
self.addChild(nextShipButton)
let nextShipClick = SKLabelNode(fontNamed: "The Bold Font")
nextShipClick.text = "▲"
nextShipClick.fontSize = 300
nextShipClick.fontColor = UIColor.clear
nextShipClick.position = CGPoint(x: self.size.width*0.753, y: self.size.height*0.36)
nextShipClick.zPosition = 2
nextShipClick.name = "nextShipClick"
self.addChild(nextShipClick)
let shipForSale = SKSpriteNode(imageNamed: "playerShip4")
shipForSale.position = CGPoint(x: self.size.width/2, y: self.size.height*0.40)
shipForSale.zPosition = 1
shipForSale.size = CGSize(width: 150, height: 300)
self.addChild(shipForSale)
let shipPodium = SKSpriteNode(imageNamed: "shipPodium")
shipPodium.position = CGPoint(x: self.size.width*0.527, y: self.size.height*0.31)
shipPodium.zPosition = 1
shipPodium.size = CGSize(width: 1200, height: 70)
self.addChild(shipPodium)
let shipsCostLabel = SKLabelNode(fontNamed: "The Bold Font")
shipsCostLabel.text = "$500"
shipsCostLabel.fontSize = 200
shipsCostLabel.fontColor = SKColor.white
shipsCostLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.20)
shipsCostLabel.zPosition = 1
self.addChild(shipsCostLabel)
let shipBuyButton = SKSpriteNode(imageNamed: "shipBuyButton")
shipBuyButton.position = CGPoint(x: self.size.width*0.54, y: self.size.height*0.15)
shipBuyButton.zPosition = 1
shipBuyButton.size = CGSize(width: 1500, height: 900)
shipBuyButton.name = "shipBuyButton"
self.addChild(shipBuyButton)
let shipBuyButtonClick = SKLabelNode(fontNamed: "The Bold Font")
shipBuyButtonClick.text = "▅▅"
shipBuyButtonClick.fontSize = 300
shipBuyButtonClick.fontColor = UIColor.clear
shipBuyButtonClick.position = CGPoint(x: self.size.width/2, y: self.size.height*0.05)
shipBuyButtonClick.zPosition = 2
shipBuyButtonClick.name = "shipBuyButtonClick"
self.addChild(shipBuyButtonClick)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches{
let pointOfTouch = touch.location(in: self)
let tappedNode = atPoint(pointOfTouch)
let tappedNodeName = tappedNode.name
if tappedNodeName == "nextShipClick"{
let sceneToMoveTo = ShopPage2(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}
if tappedNodeName == "backToMainMenuButton"{
let sceneToMoveTo = MainMenuScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}
if tappedNodeName == "shipBuyButtonClick"{
let sceneToMoveTo = ShopPage1ConformationScreen(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}
}
}
}
This is the code for ShopPage1ConformationScreen:
import Foundation
import SpriteKit
let shipForSale = SKSpriteNode(imageNamed: "playerShip4")
class ShopPage1ConformationScreen: SKScene{
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background")
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
let balance = SKLabelNode(fontNamed: "The Bold Font")
balance.text = "$\(balanceAmount)"
balance.fontSize = 100
balance.fontColor = SKColor.green
balance.position = CGPoint(x: self.size.width/2, y: self.size.height*0.87)
balance.zPosition = 1
self.addChild(balance)
let backToMainMenuButton = SKSpriteNode(imageNamed: "backButton2")
backToMainMenuButton.position = CGPoint(x: self.size.width*0.25, y: self.size.height*0.89)
backToMainMenuButton.zPosition = 1
backToMainMenuButton.size = CGSize(width: 200, height: 200)
backToMainMenuButton.name = "backToMainMenuButton"
self.addChild(backToMainMenuButton)
let shipNameLabel = SKLabelNode(fontNamed: "The Bold Font")
shipNameLabel.text = "Stealth"
shipNameLabel.fontSize = 200
shipNameLabel.fontColor = SKColor.white
shipNameLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.60)
shipNameLabel.zPosition = 1
shipNameLabel.name = "shipNameLabel"
self.addChild(shipNameLabel)
let nextShipButton = SKSpriteNode(imageNamed: "nextShipButton1")
nextShipButton.position = CGPoint(x: self.size.width*0.75, y: self.size.height*0.40)
nextShipButton.zPosition = 1
nextShipButton.size = CGSize(width: 300, height: 300)
nextShipButton.name = "nextShipButton"
self.addChild(nextShipButton)
let nextShipClick = SKLabelNode(fontNamed: "The Bold Font")
nextShipClick.text = "▲"
nextShipClick.fontSize = 300
nextShipClick.fontColor = UIColor.clear
nextShipClick.position = CGPoint(x: self.size.width*0.753, y: self.size.height*0.36)
nextShipClick.zPosition = 2
nextShipClick.name = "nextShipClick"
self.addChild(nextShipClick)
shipForSale.position = CGPoint(x: self.size.width/2, y: self.size.height*0.40)
shipForSale.zPosition = 1
shipForSale.size = CGSize(width: 150, height: 300)
self.addChild(shipForSale)
let shipPodium = SKSpriteNode(imageNamed: "shipPodium")
shipPodium.position = CGPoint(x: self.size.width*0.527, y: self.size.height*0.31)
shipPodium.zPosition = 1
shipPodium.size = CGSize(width: 1200, height: 70)
self.addChild(shipPodium)
let shipsCostLabel = SKLabelNode(fontNamed: "The Bold Font")
shipsCostLabel.text = "$500"
shipsCostLabel.fontSize = 200
shipsCostLabel.fontColor = SKColor.white
shipsCostLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.20)
shipsCostLabel.zPosition = 1
self.addChild(shipsCostLabel)
let shipBuyButton = SKSpriteNode(imageNamed: "shipBuyButton")
shipBuyButton.position = CGPoint(x: self.size.width*0.54, y: self.size.height*0.15)
shipBuyButton.zPosition = 1
shipBuyButton.size = CGSize(width: 1500, height: 900)
shipBuyButton.name = "shipBuyButton"
self.addChild(shipBuyButton)
let conformationBackground = SKSpriteNode(imageNamed: "conformationBackground")
conformationBackground.position = CGPoint(x: self.size.width*0.51, y: self.size.height*0.40)
conformationBackground.zPosition = 2
conformationBackground.size = CGSize(width: 1300, height: 1400)
conformationBackground.name = "conformationBackground"
self.addChild(conformationBackground)
let conformationScreenTextTop = SKLabelNode(fontNamed: "The Bold Font")
conformationScreenTextTop.text = "Are you sure you wish to"
conformationScreenTextTop.fontSize = 80
conformationScreenTextTop.fontColor = SKColor.white
conformationScreenTextTop.position = CGPoint(x: self.size.width/2, y: self.size.height*0.46)
conformationScreenTextTop.zPosition = 3
conformationScreenTextTop.name = "comformationScreenTextTop"
self.addChild(conformationScreenTextTop)
let conformationScreenTextBottom = SKLabelNode(fontNamed: "The Bold Font")
conformationScreenTextBottom.text = "purchase this ship?"
conformationScreenTextBottom.fontSize = 80
conformationScreenTextBottom.fontColor = SKColor.white
conformationScreenTextBottom.position = CGPoint(x: self.size.width/2, y: self.size.height*0.41)
conformationScreenTextBottom.zPosition = 3
conformationScreenTextBottom.name = "conformationScreenTextBottom"
self.addChild(conformationScreenTextBottom)
let conformationScreenTextYes = SKLabelNode(fontNamed: "The Bold Font")
conformationScreenTextYes.text = "Yes"
conformationScreenTextYes.fontSize = 150
conformationScreenTextYes.fontColor = SKColor.green
conformationScreenTextYes.position = CGPoint(x: self.size.width*0.30, y: self.size.height*0.30)
conformationScreenTextYes.zPosition = 3
conformationScreenTextYes.name = "conformationScreenTextYes"
self.addChild(conformationScreenTextYes)
let conformationScreenTextNo = SKLabelNode(fontNamed: "The Bold Font")
conformationScreenTextNo.text = "No"
conformationScreenTextNo.fontSize = 150
conformationScreenTextNo.fontColor = SKColor.red
conformationScreenTextNo.position = CGPoint(x: self.size.width*0.70, y: self.size.height*0.30)
conformationScreenTextNo.zPosition = 3
conformationScreenTextNo.name = "conformationScreenTextNo"
self.addChild(conformationScreenTextNo)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches{
let pointOfTouch = touch.location(in: self)
let tappedNode = atPoint(pointOfTouch)
let tappedNodeName = tappedNode.name
if(balanceAmount >= 500){
if tappedNodeName == "conformationScreenTextYes"{
player = shipForSale
balanceAmount -= 500
let sceneToMoveTo = ShopPage1(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}
}
if tappedNodeName == "conformationScreenTextNo"{
let sceneToMoveTo = ShopPage1(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}
}
}
}
For additional shop pages just copy and paste these sets of codes. With the exception of changing the the tapped nodes around to the correct order in which you wish to move from page to page.

How to fix this NSTimer issue?

Everything is fine until I use NSTimer to run the tree, that the images of tree is gone. I use SKview to show the physic then I find out that the code is still run, just only the image is gone. How can I fix it?
Part 1
class PlayScene: SKScene {
var BG6 = SKSpriteNode()
var balloon = SKSpriteNode()
var tree1 = SKSpriteNode()
var tree2 = SKSpriteNode()
Part 2
override func didMoveToView(view: SKView) {
MakeBG()
// add balloon
var ballontexture = SKTexture(imageNamed: "Balloon.png")
balloon = SKSpriteNode(texture: ballontexture)
balloon.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/5)
balloon.physicsBody = SKPhysicsBody(rectangleOfSize: balloon.size)
balloon.physicsBody?.dynamic = true
balloon.physicsBody?.allowsRotation = false
balloon.zPosition = 5
self.addChild(balloon)
var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("MakeTree"), userInfo: nil, repeats: true)
var ground = SKNode()
ground.position = CGPointMake(0, 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width,1))
ground.physicsBody?.dynamic = false
self.addChild(ground)
}
Part 3
func MakeBG(){
// chen hinh nen
//BG1
var BG1texture = SKTexture(imageNamed: "BG2")
var BG1 = SKSpriteNode(texture: BG1texture)
BG1.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
BG1.size.height = self.size.height
var BG1move = SKAction.moveToY(-self.frame.size.height, duration: 90)
BG1.runAction(BG1move)
self.addChild(BG1)
//BG2
var BG2texture = SKTexture(imageNamed: "BG3")
var BG2 = SKSpriteNode(texture: BG2texture)
BG2.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2 + self.frame.size.height)
BG2.size.height = self.size.height
var BG2move = SKAction.moveToY(-self.frame.size.height, duration: 140)
BG2.runAction(BG2move)
self.addChild(BG2)
//BG3
var BG3texture = SKTexture(imageNamed: "BG4")
var BG3 = SKSpriteNode(texture: BG3texture)
BG3.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2 + self.frame.size.height*2)
BG3.size.height = self.size.height
var BG3move = SKAction.moveToY(-self.frame.size.height, duration: 195)
BG3.runAction(BG3move)
self.addChild(BG3)
//BG4
var BG4texture = SKTexture(imageNamed: "BG5")
var BG4 = SKSpriteNode(texture: BG4texture)
BG4.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2 + self.frame.size.height*3)
BG4.size.height = self.size.height
var BG4move = SKAction.moveToY(-self.frame.size.height, duration: 250)
BG4.runAction(BG4move)
self.addChild(BG4)
//BG5
var BG5texture = SKTexture(imageNamed: "BG6")
var BG5 = SKSpriteNode(texture: BG5texture)
BG5.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2 + self.frame.size.height*4)
BG5.size.height = self.size.height
var BG5move = SKAction.moveToY(-self.frame.size.height, duration: 305)
BG5.runAction(BG5move)
self.addChild(BG5)
// BG6
var BG6texture = SKTexture(imageNamed: "BG7")
BG6 = SKSpriteNode(texture: BG6texture)
BG6.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2 + self.frame.size.height*5 + self.frame.size.height*5)
BG6.size.height = self.size.height
BG6.runAction(SKAction.moveTo(CGPoint(x: self.size.width/2 , y: self.size.height/2), duration: 276.5))
self.addChild(BG6)
}
func MakeTree(){
// between
let between = balloon.size.width
// random
var movementAmount = arc4random() % UInt32(self.frame.size.height/2)
var treeoffset = CGFloat(movementAmount) - self.frame.size.height/4
// move tree
var treemove = SKAction.moveByX(0, y: -self.frame.size.width*2, duration: NSTimeInterval(self.frame.size.height/50))
var treeremove = SKAction.removeFromParent()
var treemoveandremove = SKAction.sequence([treemove, treeremove])
// add tree1
var tree1texture = SKTexture(imageNamed: "C2.png")
tree1 = SKSpriteNode(texture: tree1texture)
tree1.position = CGPointMake(CGRectGetMidX(self.frame) - tree1texture.size().width/2 - between + treeoffset, CGRectGetMidY(self.frame) + self.frame.size.height)
tree1.runAction(treemoveandremove)
tree1.physicsBody = SKPhysicsBody(rectangleOfSize: tree1.size)
tree1.physicsBody?.dynamic = false
self.addChild(tree1)
// add tree2
var tree2texture = SKTexture(imageNamed: "C1.png")
tree2 = SKSpriteNode(texture: tree2texture)
tree2.position = CGPointMake(CGRectGetMidX(self.frame) + tree2texture.size().width/2 + between + treeoffset, CGRectGetMidY(self.frame) + self.frame.size.height)
tree2.runAction(treemoveandremove)
tree2.physicsBody = SKPhysicsBody(rectangleOfSize: tree2.size)
tree2.physicsBody?.dynamic = false
[enter image description here][1]
self.addChild(tree2)
}
If I understood your question correctly, then you want to know why your "tree" is gone.
It appears that your MakeTree() method moves all the tree sprite nodes off the screen:
var treemove = SKAction.moveByX(0, y: -self.frame.size.width*2, duration: NSTimeInterval(self.frame.size.height/50))
Also, you shouldn't use NSTimers in SpriteKit. Use SKAction.waitForDuration instead. Like this:
let delay = SKAction.waitForDuration(3)
someNode.runAction(delay) {
//run code here after 3 seconds
}

Swift game infinite spawning not working

i've a problem with my swift game. I want to spawn every 2 seconds two sprites and move them like flappy bird pipes. I tried on this way but my game crashes after the delay. Here is DidMoveToView: ( i jump right on the interesting parts)
let distanceToMove = CGFloat(self.frame.size.width + 140)
let movePipes = SKAction.repeatActionForever(SKAction.moveByX(-distanceToMove, y: 0, duration: NSTimeInterval(0.01 * distanceToMove)))
let removePipes = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([movePipes,removePipes])
let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
let spawn = SKAction.runBlock({() in self.initPipes()})
let spawnAndDelay = SKAction.sequence([spawn,delay])
let spawnAndDelayForever = SKAction.repeatActionForever(spawnAndDelay)
runAction(spawnAndDelayForever)
And here is the func of the pipes:
func initPipes() {
let pY = arc4random_uniform(UInt32(self.size.height - 250) + 250)
let pipePair = SKNode()
pipePair.position = CGPoint(x: self.frame.size.width + 70, y: 0)
//PIPE 1
pipe1.anchorPoint = CGPointMake(0, 0)
pipe1.position = CGPoint(x: 0, y: Int(pY))
pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(70/2, 700/2))
pipe1.physicsBody?.dynamic = false
pipe1.physicsBody?.affectedByGravity = false
pipePair.addChild(pipe1)
//PIPE 2
pipe2.anchorPoint = CGPointMake(0,1)
pipe2.position = CGPoint(x: 0, y: pipe1.position.y - 150)
pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(35, -700/2))
pipe2.physicsBody?.dynamic = false
pipe2.physicsBody?.affectedByGravity = false
pipePair.addChild(pipe2)
pipePair.runAction(moveAndRemove)
addChild(pipePair)
}
Any idea? Thanks
I think your issue is here
pipePair.addChild(pipe1)
You are not creating a new pipe but instead using an old one you created somewhere else. The first time through you are not going to have a problem, but if you didn't remove the pipe1 from its parent before you try adding it to a new parent it will crash. I would create your pipe1 and pipe2 in that method instead of having a variable outside of it.
func initPipes() {
let pY = arc4random_uniform(UInt32(self.size.height - 250) + 250)
let pipePair = SKNode()
pipePair.position = CGPoint(x: self.frame.size.width + 70, y: 0)
//PIPE 1
let pipe1 = //whatever pipe1 is
pipe1.anchorPoint = CGPointMake(0, 0)
pipe1.position = CGPoint(x: 0, y: Int(pY))
pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(70/2, 700/2))
pipe1.physicsBody?.dynamic = false
pipe1.physicsBody?.affectedByGravity = false
pipePair.addChild(pipe1)
//PIPE 2
let pipe2 = //whatever pipe1 is
pipe2.anchorPoint = CGPointMake(0,1)
pipe2.position = CGPoint(x: 0, y: pipe1.position.y - 150)
pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(35, -700/2))
pipe2.physicsBody?.dynamic = false
pipe2.physicsBody?.affectedByGravity = false
pipePair.addChild(pipe2)
pipePair.runAction(moveAndRemove)
addChild(pipePair)
}
or if the intention is to only have one set of pipes at a time call
pipe1.removeFromParent()
pipe2.removeFromParent()
before
pipePair.addChild(pipe1)
pipePair.addChild(pipe2)
Hopefully that makes sense and is helpful.