将 UIButton 的属性赋予 SpriteKit 中的 SKSpriteNode
Giving properties of a UIButton to a SKSpriteNode in SpriteKit
我想知道是否有一种方法可以让 UIButton
的属性在按下按钮后变暗,...给 SKSpiteNode
,因为 SKSpiteNode
有更多的自定义,因为我正在使用 SpriteKit
。我已经看到另外 1 个类似的问题,但 none 的答案有效。这是我必须创建 SKSpriteNode
并检测其上的触摸的代码:
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
}
}
}
//...
请帮忙。提前致谢...安东
我总是通过在 touches begin 和 touches ended 方法中添加代码来实现这一点。在这些方法中,我只是将精灵颜色设置为黑色,然后更改其颜色混合因子。让我知道这是否适合您!
//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
的属性在按下按钮后变暗,...给 SKSpiteNode
,因为 SKSpiteNode
有更多的自定义,因为我正在使用 SpriteKit
。我已经看到另外 1 个类似的问题,但 none 的答案有效。这是我必须创建 SKSpriteNode
并检测其上的触摸的代码:
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
}
}
}
//...
请帮忙。提前致谢...安东
我总是通过在 touches begin 和 touches ended 方法中添加代码来实现这一点。在这些方法中,我只是将精灵颜色设置为黑色,然后更改其颜色混合因子。让我知道这是否适合您!
//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
}
}