【问题标题】:Creating a button using SKLabelNode as a parent in Swift SpriteKit在 Swift SpriteKit 中使用 SKLabelNode 作为父级创建按钮
【发布时间】:2015-05-22 20:09:22
【问题描述】:

我正在尝试为 SpriteKit 中的按钮创建 SKLabelNode 的子类。我尝试使用 SKLabelNode 作为父级来创建一个按钮,因此我可以在创建按钮时使用我所知道的关于标签的所有信息(字体、文本大小、文本颜色、位置等)。

我已经查看了Swift Spritekit Adding Button Programaticly,我正在使用它所说的基础,但我正在创建一个子类而不是一个变量,并且我正在使用标签的代码创建按钮。子类具有允许点击它并触发动作的附加功能。

class StartScene: SKScene {

class myButton: SKLabelNode {

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
                if myButton.containsPoint(location) {

                // Action code here
                }
        }
    }
}

    override func didMoveToView(view: SKView) {

        let startGameBtn = myButton(fontNamed: "Copperplate-Light")
        startGameBtn.text = "Start Game"
        startGameBtn.fontColor = SKColor.blackColor()
        startGameBtn.fontSize = 42
        startGameBtn.position = CGPointMake(frame.size.width/2, frame.size.height * 0.5)
        addChild(startGameBtn)

    }
}

我的错误在于:if myButton.containsPoint(location)

错误显示:无法使用类型为“(CGPoint)”的参数列表调用“containsPoint”

我知道它与我的子类有关,但我不知道它具体是什么。

我还尝试在 myButton.containsPoint(location) 周围加上括号,如下所示:

if (myButton.containsPoint(location))

然后错误显示:'CGPoint' 不可转换为'SKNode'

谢谢

【问题讨论】:

    标签: ios swift sprite-kit sklabelnode


    【解决方案1】:

    我有一个替代解决方案可以以同样的方式工作。 更改以下代码

    if myButton.containsPoint(location) {
    

    为此,它应该编译并具有相同的功能

    if self.position == location {
    

    【讨论】:

    • 如果有效,请将此答案标记为已解决,以便遇到相同问题的其他人可以看到它以提高知名度
    猜你喜欢
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多