将UIButton的属性赋予SpriteKit中的SKSpriteNode [英] Giving properties of a UIButton to a SKSpriteNode in SpriteKit

查看:128
本文介绍了将UIButton的属性赋予SpriteKit中的SKSpriteNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法给出 UIButton 的属性,比如按下按钮后变暗,...到 SKSpiteNode 因为 SKSpiteNode 有更多自定义,因为我使用的是 SpriteKit 。我已经看到了另外一个这样的问题,但没有一个答案奏效。以下是我必须创建 SKSpriteNode 并检测触摸的代码:

I was wondering if there was a way to give the properties of a UIButton like darkening the button once it has been pressed,... to a SKSpiteNode since an SKSpiteNode has more customization and because I am using SpriteKit. I have seen 1 other question like this but none of the answers worked. Here is the code I have to create the SKSpriteNode and to detect a touch on it:

import SpriteKit
class StartScene: SKScene {    
var startButton = SKSpriteNode()
override func didMoveToView(view: SKView) {
    startButton = SKSpriteNode(imageNamed: "playButton")
    startButton.size = CGSize(width: 100, height: 100)
    startButton.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 50)
    self.addChild(startButton)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */

    for touch in touches {
        let location = touch.locationInNode(self)

        if startButton.containsPoint(location){
           // When it has been selected
        }   
    }
}
//...

请帮忙。在此先感谢... Anton

Pleas help. Thanks in advance... Anton

推荐答案

我总是通过添加代码来触摸开始实现这一点,并触及结束方法。在这些方法中,我只需将sprite颜色设置为黑色,然后更改其颜色混合因子。让我知道这是否适合您!

I have always achieved this by adding code to the touches began, and touches ended method. Inside of these methods I simply set the sprites color to black, and then change its color blend factor. Let me know if this works for you!

  //This will hold the object that gets darkened
    var target = SKSpriteNode() 
    //This will keep track if an object is darkened
    var have = false

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var first = touches.first as! UITouch
        var location:CGPoint = first.locationInNode(self)
        touchP = location
        mouse.position = touchP
        var node:SKNode = self.nodeAtPoint(location)
       if let button = node as? SKSpriteNode
       {
          target = button
           have = true
       }
       else
       {
         have = false
       }

       if (have == true)
       {
           target.color = UIColor.blackColor()
           target.colorBlendFactor = 0.2

       }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
            if (havet == true)
            {
                target.color = UIColor.blackColor()
                target.colorBlendFactor = 0
                target = SKSpriteNode()
                have = false

            }

}

这篇关于将UIButton的属性赋予SpriteKit中的SKSpriteNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆