如何在swift SKSpriteNode中添加按钮效果
How to add button effects in swift SKSpriteNode
我有一个 Var 按钮。
var soundButton: SKSpriteNode! = nil
详细信息保持不变。
soundButton = SKSpriteNode(imageNamed: "\(soundImg)")
soundButton.position = CGPoint(x: frame.size.width*0.08 , y:frame.size.width*0.08);
soundButton.size = CGSizeMake(frame.size.width*0.10,frame.size.width*0.10 )
self.addChild(soundButton)
然后如何添加按钮效果,例如按钮颜色灯,当我点击它并发出声音时。
一种方法是您可以在选择按钮时更改图像。
像这样进入你的 touchesBegan
方法:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self. soundButton {
soundButton.texture = SKTexture(imageNamed: "soundSelected")
}
}
}
如果你想延迟到另一个场景并且你想要过渡,你可以在你的 if
语句中使用此代码:
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let letsPlay = playScene(size: self.size)
self.view?.presentScene(letsPlay, transition: reveal)
我有一个 Var 按钮。
var soundButton: SKSpriteNode! = nil
详细信息保持不变。
soundButton = SKSpriteNode(imageNamed: "\(soundImg)")
soundButton.position = CGPoint(x: frame.size.width*0.08 , y:frame.size.width*0.08);
soundButton.size = CGSizeMake(frame.size.width*0.10,frame.size.width*0.10 )
self.addChild(soundButton)
然后如何添加按钮效果,例如按钮颜色灯,当我点击它并发出声音时。
一种方法是您可以在选择按钮时更改图像。
像这样进入你的 touchesBegan
方法:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self. soundButton {
soundButton.texture = SKTexture(imageNamed: "soundSelected")
}
}
}
如果你想延迟到另一个场景并且你想要过渡,你可以在你的 if
语句中使用此代码:
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let letsPlay = playScene(size: self.size)
self.view?.presentScene(letsPlay, transition: reveal)