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()
}
}
}
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!