-1

I am new to creating games and such so as a practice I am making a silly game currently. This game has a briefcase which contains buttons (0-9) I would like to save the value of the number that was pressed in an array. I am unsure of how to use touches began to do a specific action if a specific type is pressed. ButtonInput:

class ButtonInput: SKSpriteNode{

var value: Int = 0

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

}
}

loadView():

let briefcase = ButtonInput(imageNamed: "briefcase")
    let Button0 = ButtonInput(imageNamed: "Button0")
    let Button1 = ButtonInput(imageNamed: "Button1")
    let Button2 = ButtonInput(imageNamed: "Button2")
    let Button3 = ButtonInput(imageNamed: "Button3")
    let Button4 = ButtonInput(imageNamed: "Button4")
    let Button5 = ButtonInput(imageNamed: "Button5")
    let Button6 = ButtonInput(imageNamed: "Button6")
    let Button7 = ButtonInput(imageNamed: "Button7")
    let Button8 = ButtonInput(imageNamed: "Button8")
    let Button9 = ButtonInput(imageNamed: "Button9")


    Button0.value = 0
    Button1.value = 1
    Button2.value = 2
    Button3.value = 3
    Button4.value = 4
    Button5.value = 5
    Button6.value = 6
    Button7.value = 7
    Button8.value = 8
    Button9.value = 9

UPDATE:

I tried making a touches began after hours of research and I came up with this:

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


    for touch: AnyObject! in touches {
        let touchLocation = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(touchLocation)
        let name = touchedNode.name

            if name == "Button0"
            {
                userSequence.append(Button0.value)
                print("Button0 pressed")
            }
            if name == "Button1"
            {
                userSequence.append(Button1.getValue())
                print("Button1 pressed")
            }
            if name == "Button2"
            {
                userSequence.append(Button2.value)
            }
            if name == "Button3"
            {
                userSequence.append(Button3.value)
            }
            if name == "Button4"
            {
                userSequence.append(Button4.value)
            }
            if name == "Button5"
            {
                userSequence.append(Button5.value)
            }
            if name == "Button6"
            {
                userSequence.append(Button6.value)
            }
            if name == "Button7"
            {
                userSequence.append(Button7.value)
            }
            if name == "Button8"
            {
                userSequence.append(Button8.value)
            }
            if name == "Button9"
            {
                userSequence.append(Button9.value)
            }
            if name == "ButtonEnter"
            {
                compareSequences()
            }
    }


}
Avi
  • 17
  • 6

2 Answers2

0

You should make buttons in the mainstoryboard, and connect them to the view controller. Then you can write the func for each button inside the connected actions

hope this helps

Tarjerw
  • 505
  • 1
  • 5
  • 12
0

If you use SpriteKit, I would do something like this:

import SpriteKit

var _button = [SKSpriteNode]()

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch in touches {
        let location = touch.locationInNode(self)

        if(self.nodeAtPoint(location).name != nil && self.nodeAtPoint().name != "enter") {
            _array.append(self.nodeAtPoint(location).name)
        }
    }
}

func loadView() {

    for(var i = 0; i < 10; i++) {
        let tempButton = SKSpriteNode(imageNamed: "Button\(i)")
        tempButton.position = CGPoint(x: yourValue, y: yourValue)
        tempButton.size = CGSize(width: yourValue, height: yourValue)
        tempButton.name = "\(i)"
        temButton.zPosition = 2
        _button.append(tempButton)
        self.addChild(_button.last!)
    }

}

This will work if you have no other SKSpriteNode in the game because in the touches began I assume that the sprite touched has a number (0-9). To counter this problem, you could add an if statement to verify that the name is between 0-9.

Hope this help!

  • Thanks for the suggestion but I will end up having another SKSpriteNode (Enter Button) so that the user has to hit it to confirm they have chosen the sequence. I have made an update to the post as well with my findings. – Avi Jan 06 '16 at 02:49
  • Give your enter button the name "enter" for exemple and in the touches began function add the code if(self.nodeAtPoint(location).name != nil && self.nodeAtPoint().name != "enter") I have edit my answer ... – Charles-olivier Demers Jan 06 '16 at 02:52
  • Thanks for the help! I would like the _array to store the integer value of self.nodeAtPoint(location).name instead. Any Ideas how to? Also if I may ask, how were you taught coding & game development? I am a 2nd year computer engineering student at my university and I have just learnt basic coding from C and basic OPP from a Java course. I was wondering if there were any textbooks or something that I could read to help me better understand everything. Thank you again – Avi Jan 06 '16 at 03:26
  • To insert self.nodeAtPoint(location).name, simply cast the string to an int. For an example: var _array = [Int]() do: _array.append(Int(self.nodeAtPoint(location).name!)!) -I suggest you to read the apple book's about Swift: http://apple.co/1DgqEVo -Also you can look on UDemy.com website and search for SpriteKit & Swift development, you will find some interesting course And stack overflow is a good place to find answer to problem :) – Charles-olivier Demers Jan 06 '16 at 03:33
  • Thank you. Lastly I want when I was running the program the placement of my buttons disappeared. Please go here to see the code properly: https://gyazo.com/ace1957ad664e7e75d7721e8995c47d0 When I run this my buttons are not in the same place anymore (They are not on the screen even) – Avi Jan 06 '16 at 03:41
  • Do the button disappear when you do something? Do you see the button sometimes? – Charles-olivier Demers Jan 06 '16 at 03:43
  • they do not appear at all. Before changing my code I they were all perfectly placed – Avi Jan 06 '16 at 03:47
  • What is your briefCase? Try to put a button at this position: CGPoint(x: self.view!.frame.width / 2, y: self.view!.frame.height / 2) Do you see it? – Charles-olivier Demers Jan 06 '16 at 03:52
  • Yes I see the button at the given position. Also my briefcase is a SKSpriteNode that contains a graphic of a briefcase shown here: https://gyazo.com/481da9a92f4e2a0b47a976b7274de972 This was my previous button layout: https://gyazo.com/da5d261c85e7af1d87c8e9b21d9cff20 – Avi Jan 06 '16 at 03:56
  • There's most likely a problem with you position. Maybe the coordinate is out of the screen? Try to print(Button.position) and run it on an iPhone 6s. The screen size of the iPhone is 375x667 so if it is outside you got your problem – Charles-olivier Demers Jan 06 '16 at 03:59
  • One of the buttons are placed at (62.6669998168945, 207.0) also the previous code i used to get the correct button placement is here: https://gyazo.com/9546a2c8c1125fdf57ff8273cbdaaee9 – Avi Jan 06 '16 at 04:04
  • Oh, try to change the zPosition of the sprite. Your button should have the zPosition = 2 and the briefCase should have the zPosition =1 Like this: Button.zPosition = 2 and briefCase.zPosition =1. Try this? – Charles-olivier Demers Jan 06 '16 at 04:08
  • I got it all to work now! Thank you so much for everything that you have done – Avi Jan 06 '16 at 04:23