gpt4 book ai didi

ios - 快速 skscene 触摸并按住其他 Action ,而不仅仅是触摸

转载 作者:行者123 更新时间:2023-11-30 12:20:15 25 4
gpt4 key购买 nike

所以我最近用 Sprite 套件做了一个快速游戏,但现在我陷入了困境。我有一个角色选择屏幕,如果您按住该角色,我想显示该角色的描述,但如果您只是触摸它,您就会选择它并玩它。我已经有了播放/显示描述的代码。我只需要知道何时调用相应的函数以及如何区分节点是被握住还是刚刚被触摸。

提前致谢

最佳答案

所以,这是您如何使用 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 秒钟,白色框就会淡入。当您松开手指时,白色框就会淡出。另外,我还实现了 TouchMoved,因此如果在蓝色框消失时滑动紫色按钮的手指,它也会淡出。

所以在这个例子中,我将长按与单击融合在一起。单击被认为是不长按的所有内容。例如,如果用户按住手指0.01秒,或0.5秒,或0.99秒,则将被视为单击,并会弹出黄色框。如果你按住手指>=1秒,就会触发长按 Action 。

另一种方法是使用手势识别器......我稍后可能会为此做一个例子:)

关于ios - 快速 skscene 触摸并按住其他 Action ,而不仅仅是触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44874791/

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