touchesBegan Swift 2自定义SKSpriteKit节点

时间:2016-01-05 23:08:40

标签: ios arrays swift skspritenode touchesbegan

我是创建游戏的新手,所以作为一种练习我目前正在做一个愚蠢的游戏。这个游戏有一个公文包,其中包含按钮(0-9)我想保存在数组中按下的数字的值。如果按下特定类型,我不确定如何使用触摸开始执行特定操作。 ButtonInput:

    tinymce.init({
        indent_use_margin: true,
        indentation: 20,

的loadView():

class ButtonInput: SKSpriteNode{

var value: Int = 0

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

}
}

更新:

经过几个小时的研究后,我尝试了接触,我想出了这个:

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

2 个答案:

答案 0 :(得分:0)

您应该在主板中创建按钮,并将它们连接到视图控制器。然后,您可以为连接的操作中的每个按钮编写func

希望这会有所帮助

答案 1 :(得分:0)

如果您使用SpriteKit,我会这样做:

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

}

如果游戏中没有其他SKSpriteNode,这将有效,因为在触摸开始时我假设所触摸的精灵有一个数字(0-9)。要解决此问题,您可以添加if语句以验证名称是否在0-9之间。

希望这有帮助!