0
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

Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • Please a bit of more information to your question in order to get people to understand the problem much better. – DvixExtract Aug 07 '18 at 13:22
  • In the event that u want to use buttons that are provided by apple in interface builder i suggest you read this apple document : https://developer.apple.com/documentation/uikit/uibutton. However if you are want to create a custom ui element like a button , i suggest you have a look at : https://stackoverflow.com/a/26376815/6516391 – DvixExtract Aug 07 '18 at 13:22

1 Answers1

0

It could be an example:

buttonBg.addTarget(self, action: #selector(playMusic(_:)), for: UIControlEvents.touchUpInside)
Roy Bernal
  • 121
  • 3