gpt4 book ai didi

ios - 将 UIButton 的属性赋予 SpriteKit 中的 SKSpriteNode

转载 作者:行者123 更新时间:2023-11-29 01:15:17 24 4
gpt4 key购买 nike

我想知道是否有一种方法可以将 UIButton 的属性赋予 SKSpiteNode ,例如按下按钮后使按钮变暗...... SKSpiteNode 具有更多自定义功能,因为我使用的是 SpriteKit。我见过另外 1 个这样的问题,但没有一个答案有效。这是我必须创建 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 方法中添加代码来实现这一点。在这些方法中,我只是将 Sprite 颜色设置为黑色,然后更改其颜色混合因子。让我知道这是否适合您!

  //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

}

}

关于ios - 将 UIButton 的属性赋予 SpriteKit 中的 SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35258342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com