所以我最近做了一个快速游戏与精灵套件,现在我卡住了。 我有一个角色选择屏幕,如果你持有角色,我想显示角色的描述,但如果你只是触摸角色,你可以选择并使用角色。我已经有了播放/显示描述的代码。我只需要知道何时调用相应的函数,以及如何区分节点是否被触摸。迅速skscene触摸并举行其他行动,而不仅仅是触摸
在此先感谢
所以我最近做了一个快速游戏与精灵套件,现在我卡住了。 我有一个角色选择屏幕,如果你持有角色,我想显示角色的描述,但如果你只是触摸角色,你可以选择并使用角色。我已经有了播放/显示描述的代码。我只需要知道何时调用相应的函数,以及如何区分节点是否被触摸。迅速skscene触摸并举行其他行动,而不仅仅是触摸
在此先感谢
所以,在这里是如何使用SKActions
做到这一点......另外,还有使用SKAction
另一种方式,但我真的不能告诉你一切准备:)不管怎么说,这里是这你想要做什么代码:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
let kLongPressDelayActionKey = "longPressDelay"
let kLongPressStartedActionKey = "longPressStarted"
let kStoppingLongPressActionKey = "stoppingLongPressActionKey"
let desc = SKSpriteNode(color: .white, size: CGSize(width: 150, height: 150))
let button = SKSpriteNode(color: .purple, size: CGSize(width: 150, height: 150))
let other = SKSpriteNode(color: .yellow, size: CGSize(width: 150, height: 150))
override func didMove(to view: SKView) {
addChild(desc)
addChild(button)
addChild(other)
button.position.y = -160
button.name = "button"
desc.alpha = 0.0
desc.name = "description"
other.name = "other"
other.alpha = 0.0
other.position.y = -320
}
private func singleTap(onNode:SKNode){
other.alpha = other.alpha == 0.0 ? 1.0 : 0.0
}
private func startLongPress(withDuration duration:TimeInterval){
//How long does it take before long press is fired
//If user moves his finger of the screen within this delay, the single tap will be fired
let delay = SKAction.wait(forDuration: duration)
let completion = SKAction.run({
[unowned self] in
self.desc.removeAction(forKey: self.kLongPressDelayActionKey)
self.desc.run(SKAction.fadeIn(withDuration: 0.5), withKey: self.kLongPressStartedActionKey)
})
self.desc.run(SKAction.sequence([delay,completion]), withKey: kLongPressDelayActionKey)
}
private func stopLongPress(){
//Fire single tap and stop long press
if desc.action(forKey: kLongPressDelayActionKey) != nil{
desc.removeAction(forKey: kLongPressDelayActionKey)
self.singleTap(onNode: self.other)
//or just stop the long press
}else{
desc.removeAction(forKey: kLongPressStartedActionKey)
//Start fade out action if it isn't already started
if desc.action(forKey: kStoppingLongPressActionKey) == nil {
desc.run(SKAction.fadeOut(withDuration: 0.2), withKey: kStoppingLongPressActionKey)
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
//To stop the long press if we slide a finger of the button
if let touch = touches.first {
let location = touch.location(in: self)
if !button.contains(location) {
stopLongPress()
}
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
if button.contains(location) {
print("Button tapped")
startLongPress(withDuration: 1)
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
stopLongPress()
}
}
您可以运行的代码,你会看到,如果你的紫色按键敲击,一个黄色的框将弹出。如果再次点击它,它会隐藏。此外,如果您将手指放在紫色框上1秒钟,白色盒子就会褪色。松开手指时,它会淡出。另外,我已经实现了touchesMoved,所以如果你在蓝色框出来时滑动你的紫色按钮的手指,它也会淡出。
所以在这个例子中,我融合了长按和单击。单击是被认为是不长按的一切。例如,如果用户手指按住0.01秒或0.5秒或0.99秒,则它将被视为单击并且黄色框将弹出。如果你的手指保持> = 1秒,长按动作将被触发。
另一种方法是使用手势识别......,我可能会做一个例子,后来:)
这里是如何使用手势识别做到这一点:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var longPressGesture:UILongPressGestureRecognizer!
var singleTapGesture:UITapGestureRecognizer!
let kLongPressStartedActionKey = "longPressStarted"
let kStoppingLongPressActionKey = "stoppingLongPressActionKey"
let desc = SKSpriteNode(color: .white, size: CGSize(width: 150, height: 150))
let button = SKSpriteNode(color: .purple, size: CGSize(width: 150, height: 150))
let other = SKSpriteNode(color: .yellow, size: CGSize(width: 150, height: 150))
override func didMove(to view: SKView) {
longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(GameScene.longPress(_:)))
singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(GameScene.singleTap(_:)))
self.view?.addGestureRecognizer(longPressGesture)
self.view?.addGestureRecognizer(singleTapGesture)
addChild(desc)
addChild(button)
addChild(other)
button.position.y = -160
button.name = "button"
desc.alpha = 0.0
desc.name = "description"
other.name = "other"
other.alpha = 0.0
other.position.y = -320
}
private func stopLongPress(){
desc.removeAction(forKey: self.kLongPressStartedActionKey)
//Start fade out action if it isn't already started
if desc.action(forKey: kStoppingLongPressActionKey) == nil {
desc.run(SKAction.fadeOut(withDuration: 0.2), withKey: kStoppingLongPressActionKey)
}
}
func singleTap(_ sender:UITapGestureRecognizer){
if sender.state == .ended {
let location = convertPoint(fromView: sender.location(in: self.view))
if self.button.contains(location) {
self.other.alpha = self.other.alpha == 0.0 ? 1.0 : 0.0
}
}
}
func longPress(_ sender: UILongPressGestureRecognizer) {
let longPressLocation = convertPoint(fromView: sender.location(in: self.view))
if sender.state == .began {
if button.contains(longPressLocation) {
desc.run(SKAction.fadeIn(withDuration: 0.5), withKey: self.kLongPressStartedActionKey)
}
}else if sender.state == .ended {
self.stopLongPress()
}else if sender.state == .changed {
let location = convertPoint(fromView: sender.location(in: self.view))
if !button.contains(location) {
stopLongPress()
}
}
}
}
你可以实现手势识别器,或者你可以自己做。两种方法都有其自身的缺点。识别器的问题是它们被添加到视图中,而不是添加到节点。手动执行的问题是......它需要你做一些编码:)但是你可以尝试使用SKAction序列,这会在延迟后弹出一个描述。这个动作应该在touchesBegan中开始,并以touchesEnded结束。 – Whirlwind
准确地说,这里的延迟代表了印刷机应该在描述出现之前的时间。 – Whirlwind
是的,谢谢这就是我如何显示说明,但我如何做到这一点,如果你只是点击它触发一个不同的功能比如果你长时间按它 –