ios – 在SpriteKit中创建按钮:Swift

我想在SpriteKit或SKScene中创建一个按钮,将视图发送到另一个视图控制器.

我尝试使用“performSegue with identifier”,但显然SKScene不支持功能.
如何使用SpriteKit创建一个将视图发送到另一个视图的按钮?

这是我尝试用来执行此操作的代码.

带有“HomeButton.prepareForSegueWithIdentifier()”的行只是一个例子.它实际上不会让我添加“prepareForSegue”部分,它不支持它< ---我的意思是,当我去添加它时,它是无法识别的.

class GameOverScene: SKScene {

     var HomeButton: SKNode! = nil


    init(size: CGSize,won: Bool) {
        super.init(size: size)

        backgroundColor = SKColor.whiteColor()

        HomeButton = SKSpriteNode(color: SKColor.blueColor(),size: CGSize(width: 100,height: 100))
        HomeButton.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
        HomeButton.userInteractionEnabled = true
        self.addChild(HomeButton)

        let message = won ? "You Won!" : "You Lose!"





        let label = SKLabelNode(fontNamed: "Title 1")
        label.text = message
        label.fontSize = 40
        label.fontColor = SKColor.blackColor()
        label.position = CGPoint(x: size.width/2,y: size.height/2)
        addChild(label)

        runAction(SKAction.sequence([SKAction.waitForDuration(3.0),SKAction.runBlock() {
            let reveal = SKTransition.flipHorizontalWithDuration(0.5)
            let scene = GameScene(size: size)
            self.view?.presentScene(scene,transition: reveal)
        }
        ]))


    }

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

            if HomeButton.containsPoint(location) {

                HomeButton.prepareForSegueWithIdentifier()

            }
        }
    }

注意:我尝试过使用按钮,但它们不适用于SKScene.

如果有任何混淆,我会回应.

解决方法

如果你需要在SpriteKit中创建一个按钮,我认为这个按钮必须有所有或部分可用的动作来做任何你想做的事情(就像UIButton所做的那样)

在这里,您可以找到一个构建SpriteKit按钮的简单类,称为FTButtonNode:

class FTButtonNode: SKSpriteNode {

    enum FTButtonActionType: Int {
        case TouchUpInside = 1,TouchDown,TouchUp
    }

    var isEnabled: Bool = true {
        didSet {
            if (disabledTexture != nil) {
                texture = isEnabled ? defaultTexture : disabledTexture
            }
        }
    }
    var isSelected: Bool = false {
        didSet {
            texture = isSelected ? selectedTexture : defaultTexture
        }
    }

    var defaultTexture: SKTexture
    var selectedTexture: SKTexture
    var label: SKLabelNode

    required init(coder: NSCoder) {
        fatalError("NSCoding not supported")
    }

    init(normalTexture defaultTexture: SKTexture!,selectedTexture:SKTexture!,disabledTexture: SKTexture?) {

        self.defaultTexture = defaultTexture
        self.selectedTexture = selectedTexture
        self.disabledTexture = disabledTexture
        self.label = SKLabelNode(fontNamed: "Helvetica");

        super.init(texture: defaultTexture,color: UIColor.whiteColor(),size: defaultTexture.size())
        userInteractionEnabled = true

        //Creating and adding a blank label,centered on the button
        self.label.verticalAlignmentMode = SKLabelVerticalAlignmentMode.Center;
        self.label.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center;
        addChild(self.label)

        // Adding this node as an empty layer. Without it the touch functions are not being called
        // The reason for this is unknown when this was implemented...?
        let bugFixLayerNode = SKSpriteNode(texture: nil,color: UIColor.clearColor(),size: defaultTexture.size())
        bugFixLayerNode.position = self.position
        addChild(bugFixLayerNode)

    }

    /**
     * Taking a target object and adding an action that is triggered by a button event.
     */
    func setButtonAction(target: AnyObject,triggerEvent event:FTButtonActionType,action:Selector) {

        switch (event) {
        case .TouchUpInside:
            targetTouchUpInside = target
            actionTouchUpInside = action
        case .TouchDown:
            targetTouchDown = target
            actionTouchDown = action
        case .TouchUp:
            targetTouchUp = target
            actionTouchUp = action
        }

    }

    /*
    New function for setting text. Calling function multiple times does
    not create a ton of new labels,just updates existing label.
    You can set the title,font type and font size with this function
    */

    func setButtonLabel(title: NSString,font: String,fontSize: CGFloat) {
        self.label.text = title as String
        self.label.fontSize = fontSize
        self.label.fontName = font
    }

    var disabledTexture: SKTexture?
    var actionTouchUpInside: Selector?
    var actionTouchUp: Selector?
    var actionTouchDown: Selector?
    weak var targetTouchUpInside: AnyObject?
    weak var targetTouchUp: AnyObject?
    weak var targetTouchDown: AnyObject?

    override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
        if (!isEnabled) {
            return
        }
        isSelected = true
        if (targetTouchDown != nil && targetTouchDown!.respondsToSelector(actionTouchDown!)) {
            UIApplication.sharedApplication().sendAction(actionTouchDown!,to: targetTouchDown,from: self,forEvent: nil)
        }
    }

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

        if (!isEnabled) {
            return
        }

        let touch: AnyObject! = touches.first
        let touchLocation = touch.locationInNode(parent!)

        if (CGRectContainsPoint(frame,touchLocation)) {
            isSelected = true
        } else {
            isSelected = false
        }

    }

    override func touchesEnded(touches: Set<UITouch>,withEvent event: UIEvent?) {
        if (!isEnabled) {
            return
        }

        isSelected = false

        if (targetTouchUpInside != nil && targetTouchUpInside!.respondsToSelector(actionTouchUpInside!)) {
            let touch: AnyObject! = touches.first
            let touchLocation = touch.locationInNode(parent!)

            if (CGRectContainsPoint(frame,touchLocation) ) {
                UIApplication.sharedApplication().sendAction(actionTouchUpInside!,to: targetTouchUpInside,forEvent: nil)
            }

        }

        if (targetTouchUp != nil && targetTouchUp!.respondsToSelector(actionTouchUp!)) {
            UIApplication.sharedApplication().sendAction(actionTouchUp!,to: targetTouchUp,forEvent: nil)
        }
    }

}

该来源于this Gist年提供

用法

let backTexture: SKTexture! = SKTexture(image:"backBtn.png")
let backTextureSelected: SKTexture! = SKTexture(image:"backSelBtn.png")  
let backBtn = FTButtonNode(normalTexture: backTexture,selectedTexture: backTextureSelected,disabledTexture: backTexture,size:backTexture.size())
backBtn.setButtonAction(self,triggerEvent: .TouchUpInside,action: #selector(GameScene.backBtnTap))
backBtn.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
backBtn.zPosition = 1
backBtn.name = "backBtn"
self.addChild(backBtn)

func backBtnTap() {
    print("backBtnTap tapped")
    // Here for example you can do:
    let transition = SKTransition.fadeWithDuration(0.5)
    let nextScene = MenuScene(size: self.scene!.size)
    nextScene.scaleMode = .ResizeFill
    self.scene?.view?.presentScene(nextScene,transition: transition)
}

相关文章

背景 前端时间产品经理决定使用百度统计,使得 工程B 中原统计sdk-友盟统计,需要被去除。之前尝试去除...
结论: alloc负责分配内存和创建对象对应的isa指针; init只是返回alloc生成的对象。 所以alloc后,多次...
更新 如果UI愿意把启动图切割成n份,按一定约束在launchscreen.storyboard中进行排版,启动图效果会更好...
最近在看一本书《Effective OC 2.0》,今天看到有个tip是OC适中循环各自优劣性,作者最终推荐此块循环。...
// // ViewController.m // paintCodeTestOC //gif // Created by LongMa on 2019/7/25. // #import &a...
背景介绍 一般情况下,出于省电、权限、合理性等因素考虑,给人的感觉是很多奇怪的需求安卓可以实现,但...