import SpriteKit
import GameplayKit
class GameScene: SKScene {
//MARK: - Variables
private var menuBg : SKSpriteNode?
private var buttonBg : SKSpriteNode?
private var SnowEmitter : SKEmitterNode?
private var Interval : CGFloat = 140
override func didMove(to view: SKView) {
createMenu()
}
private func createMenu(){
createBg()
createButtons()
playMusic()
createSnow()
}
private func createButton(buttonText : String, Interv : CGFloat, ButName : String) {
let firstButPosition = CGPoint(x: frame.midX, y: 460 )
let ButZpos = 0.2
let TexZpos = 0.3
//Create Button
buttonBg = SKSpriteNode(imageNamed: "menuButton.jpg")
buttonBg?.position = CGPoint(x: firstButPosition.x, y: firstButPosition.y - Interv)
buttonBg?.zPosition = CGFloat(ButZpos)
buttonBg?.xScale = 1.35
buttonBg?.name = ButName
buttonBg?.isUserInteractionEnabled = true
self.addChild(buttonBg!)
//Cretae button text
let textNode = SKLabelNode(fontNamed: "Snell Roundhand")
textNode.text = buttonText
textNode.fontSize = 55
textNode.fontColor = SKColor.purple
textNode.position = CGPoint(x: (buttonBg?.position.x)!, y: (buttonBg?.position.y)! - 15)
textNode.zPosition = CGFloat(TexZpos)
self.addChild(textNode)
}
private func createBg(){
menuBg = SKSpriteNode(imageNamed: "Bg.jpg")
menuBg?.zPosition = 0.0
menuBg?.position = CGPoint(x: frame.midX, y: frame.midY)
menuBg?.size.height = frame.size.height
menuBg?.size.width = frame.size.width
self.addChild(menuBg!)
}
private func createButtons(){
createButton(buttonText: "Play", Interv: Interval, ButName: "PlayButton" )
createButton(buttonText: "New Game", Interv: Interval * 2, ButName: "NewGameButton" )
createButton(buttonText: "Mision Select", Interv: Interval * 3, ButName: "MissionButton" )
createButton(buttonText: "Otions", Interv: Interval * 4, ButName: "OptionsButton" )
createButton(buttonText: "Exit", Interv: Interval * 5, ButName: "ExitButton")
}
private func createSnow(){
SnowEmitter = SKEmitterNode(fileNamed: "MySnow.sks")
SnowEmitter?.position = CGPoint(x: frame.midX, y: frame.maxY)
SnowEmitter?.zPosition = 0.4
SnowEmitter?.targetNode = self
self.addChild(SnowEmitter!)
}
private func playMusic(){
let backgroundSound = SKAudioNode(fileNamed: "SoftPiano.mp3")
backgroundSound.autoplayLooped = true
self.addChild(backgroundSound)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.location(in: self)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.location(in: self)
}
}
}
How to make my buttons doing some action when they are touched? Thanks for your answer. I am studying at the university