Swift Spritekit以编程方式添加按钮

Swift Spritekit以编程方式添加按钮,swift,Swift,如何以编程方式添加一个按钮,该按钮将在单击时运行操作?将使用什么代码 我习惯于在故事板中添加一个按钮,然后从那里运行IBAction func tappedButton(theButton: UIButton!) { println("button tapped") } } 当点击按钮时,上述代码打印出点击的按钮 另外,swift电子书对于新的编程语言来说是一个非常好的指南。在SpriteKit中添加一个

如何以编程方式添加一个按钮,该按钮将在单击时运行操作?将使用什么代码

我习惯于在故事板中添加一个按钮,然后从那里运行IBAction

          func tappedButton(theButton: UIButton!) {
                println("button tapped")
           }
        }
当点击按钮时,上述代码打印出点击的按钮


另外,swift电子书对于新的编程语言来说是一个非常好的指南。

在SpriteKit中添加一个按钮并对点击做出响应并不像在UIKit中那样容易。基本上,您需要创建某种类型的
SKNode
,它将绘制您的按钮,然后检查场景中注册的触摸是否在该节点的边界内

一个非常简单的场景,中间只有一个红色矩形作为按钮,看起来像这样:

import UIKit
import SpriteKit
class ButtonTestScene: SKScene {
    var button: SKNode! = nil
    override func didMove(to view: SKView) {
        // Create a simple red rectangle that's 100x44
        button = SKSpriteNode(color: .red, size: CGSize(width: 100, height: 44))
        // Put it in the center of the scene
        button.position = CGPoint(x:self.frame.midX, y:self.frame.midY);
        self.addChild(button)
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        // Loop over all the touches in this event
        for touch in touches {
            // Get the location of the touch in this scene
            let location = touch.location(in: self)
            // Check if the location of the touch is within the button's bounds
            if button.contains(location) {
                print("tapped!")
            }
        }
    }
}
导入UIKit
进口SpriteKit
类按钮测试场景:SKScene{
var按钮:SKNode!=nil
覆盖func didMove(到视图:SKView){
//创建一个100x44的简单红色矩形
按钮=SKSpriteNode(颜色:红色,尺寸:CGSize(宽:100,高:44))
//把它放在场景的中心
button.position=CGPoint(x:self.frame.midX,y:self.frame.midY);
self.addChild(按钮)
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
//在此事件中循环所有接触
接触{
//获取此场景中触摸的位置
让位置=触摸。位置(in:self)
//检查触摸的位置是否在按钮的范围内
如果按钮包含(位置){
打印(“点击!”)
}
}
}
}

如果您需要一个按钮,它的外观和动画与UIKit中的类似,那么您需要自己实现它;SpriteKit没有内置任何功能。

我在Swift中创建了一个类SgButton(),用于为SpriteKit创建按钮。您可以使用图像、纹理(来自精灵表)、纯文本、文本和背景图像/纹理创建按钮。例如,要使用图像创建按钮,请执行以下操作:

SgButton(normalImageNamed: "back.png")
使用纹理创建按钮:

SgButton(normalTexture: buttonSheet.buy(), highlightedTexture: buttonSheet.buy_d(), buttonFunc: tappedButton)
创建圆角文本按钮:

SgButton(normalString: "Tap me", normalStringColor: UIColor.blueColor(), size: CGSizeMake(200, 40), cornerRadius: 10.0, buttonFunc: tappedButton)

Mike S-答案更新为-Swift 5.2

override func didMove(to view: SKView) {
        createButton()
        
    }

func createButton()
    {
        // Create a simple red rectangle that's 100x44
        button = SKSpriteNode(color: SKColor.red, size: CGSize(width: 100, height: 44))
        // Put it in the center of the scene
        button.position = CGPoint(x:self.frame.midX, y:self.frame.midY);
        
        self.addChild(button)
        
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let touchLocation = touch!.location(in: self)
            // Check if the location of the touch is within the button's bounds
            if button.contains(touchLocation) {
                print("tapped!")
            }
        
    }
override func didMove(to view:SKView){
createButton()
}
func createButton()
{
//创建一个100x44的简单红色矩形
按钮=SKSpriteNode(颜色:SKColor.red,尺寸:CGSize(宽度:100,高度:44))
//把它放在场景的中心
button.position=CGPoint(x:self.frame.midX,y:self.frame.midY);
self.addChild(按钮)
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
让我们先接触
让touchLocation=touch!.location(in:self)
//检查触摸的位置是否在按钮的范围内
如果按钮包含(触摸位置){
打印(“点击!”)
}
}
您可以使用。
文本/图像按钮,Swift 4。

我想他首先想问一下如何添加该按钮;在点击添加其操作之前,他要求按程序进行操作。感谢您的尝试。我还将研究swift电子书。谢谢你给我看这本书!:)为什么这会被否决?你不尊重其他人的贡献吗?我正在使用你的解决方案,它工作得很好-不知道为什么它最初被否决。@Tony Concern non,你的类很好,它只需要一些更新就可以使用最新的语法和引入的UITouch更改…如果“按钮”是SKView的子项,它工作得很好,但是,如果一个按钮是另一个SKSpriteNode或相机的子按钮,则这不起作用。鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防无法访问目标站点或永久脱机。我认为所有必要的信息都在这个主题中。根据上下文,此链接通过SpriteKit按钮实现直接指向GitHub上的库:-)