gpt4 book ai didi

ios - 按下时向 SKSpriteNode 添加视觉效果

转载 作者:搜寻专家 更新时间:2023-10-31 08:07:11 25 4
gpt4 key购买 nike

我正在 Xcode 6 中开发我的第一个 SpriteKit 应用程序,使用 Swift 进行编码。现在我用透明的 png 文件制作了一些漂亮的按钮。但我试图在按下按钮时显示视觉效果。

示例我现在如何显示静态按钮:

let playButton = Button(imageNamed:"playButton")
playButton.position = CGPointMake(self.size.width/2, self.size.height/2 - playButton.size.height * 2.5 - displacement)
self.sharedInstance.addChildFadeIn(playButton, target: self)

任何效果就足够了,也许是脉冲效果,或者在按下时发光。我已经搜索过了,但在 Swift 中我真的找不到任何东西。

编辑:更多信息

    class Button: SKSpriteNode {  
init(imageNamed: String) {
let texture = SKTexture(imageNamed: imageNamed)
// have to call the designated initializer for SKSpriteNode
super.init(texture: texture, color: nil, size: texture.size())
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.runAction(SKAction.scaleTo(1.3, duration: kButtonFadingSpeed))
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
self.runAction(SKAction.scaleTo(1.3, duration: kButtonFadingSpeed))
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
self.runAction(SKAction.scaleTo(1.0, duration: kButtonFadingSpeed))
}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

func addChildFadeIn(node: SKNode, target: SKNode) {
node.alpha = 0
target.addChild(node)
node.runAction(SKAction.fadeAlphaTo(1.0, duration: NSTimeInterval(kAddChildSpeed)))
}

函数 AddChildFadeIn 在类中定义:singleton

非常感谢任何帮助!

最佳答案

我发现这个问题的一个很好的解决方案是复制原始节点,将副本的 alpha 设置为 0.5 将其直接放在原始节点的顶部,并将其混合模式设置为添加。这是一个示例。

// this is our original node that we want to make glow
playButton.anchorPoint = CGPointMake(0.5, 0.5)

// create a copy of our original node create the glow effect
let glowNode : SKSpriteNode = playButton.copy() as! SKSpriteNode
glowNode.size = playButton.size
glowNode.anchorPoint = playButton.anchorPoint
glowNode.position = CGPoint(x: 0, y: 0)
glowNode.alpha = 0.5
glowNode.blendMode = SKBlendMode.Add

// add the new node to the original node
playButton.addChild(glowNode)

// add the original node to the scene
self.addChild(playButton)

关于ios - 按下时向 SKSpriteNode 添加视觉效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28698964/

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