所以我正在使用swift制作一个spritekit游戏。我想知道哪个最好用手势?
目前我有一个override func touchesBegan
处理所有点按和UILongPressGestureRecognizer
处理长点击/暂停。
所以你知道,按下按钮并跳跃英雄。长期保持让英雄变成了鸭子。
由于某种原因,我的longPress功能并不总是被调用(有时你可以按住10次然后它不再被调用(不被识别),其他时候它是5,它这导致了昨天整整一天尝试新事物和调查,这让我想到了这个问题。
使用touchesBegan
或将我的所有触控调用移至由UITapGestureRecognizer
处理的新功能是否更好?
我确实将所有内容从touchesBegan
移到了UITapGestureRecognizer
,但似乎非常缓慢。但我可能错误地实施了它?
这就是设置recognisers
的方式:
func setupRecognizers() {
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
view!.addGestureRecognizer(tapRecognizer)
let longTapRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("handleLongPress:"))
longTapRecognizer.minimumPressDuration = 0.2
view!.addGestureRecognizer(longTapRecognizer)
}
这些是处理手势的功能:
func handleTap(recognizer: UIGestureRecognizer) {
//currently all handled in touchesBegan
}
func handleLongPress(recognizer: UIGestureRecognizer) {
print("1 --------> longPress Called.... ", recognizer.state.rawValue, gameState)
if gameState == .Play {
//do stuff
//duck Hero
} else {
//switch gameState
}
}
这是处理触摸/点击的功能:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
//do stuff
switch gameState {
case .MainMenu:
break
... //more states
}
}
super.touchesBegan(touches, withEvent: event)
}
如果我将所有内容从touchesBegans
移动到tapRecogniser
(上面的空函数),我也必须实现这一点,以转换触摸位置坐标:
func handleTap(recognizer: UIGestureRecognizer) {
let location = convertPointFromView(CGPoint(x: recognizer.locationInView(nil).x, y: recognizer.locationInView(nil).y))
print("Converted Coords: ", location)
//then do all touchesBegan stuff
}
我已经尝试了两种方法,但后者看起来非常缓慢而且迟钝。也许我忘了实施推荐的东西?
似乎我的longPress手势并不总是被调用,这些之间是否会有冲突?
答案 0 :(得分:3)
所以,如果您在红色方块上按住两秒钟,您将收到一条消息,当您放开时,消息将消失。你可能需要在那里添加一些布尔值,以确保你的角色在2秒按钮保持后不会每帧重复一些动作。这应该足以让你开始希望
import SpriteKit
class GameScene: SKScene {
// time values
var delta = NSTimeInterval(0)
var last_update_time = NSTimeInterval(0)
var longTouchTimer = NSTimeInterval(0)
var touched = false
var testLabel = SKLabelNode(text: "you have touched for awhile now bro")
let touchSprite = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(100, 100))
override func didMoveToView(view: SKView) {
touchSprite.position = CGPointMake(self.size.width/2, self.size.height/2)
addChild(touchSprite)
testLabel.hidden = true
testLabel.position = touchSprite.position
testLabel.position.y += 100
testLabel.fontSize = 20
addChild(testLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if touchSprite.containsPoint(location) {
touched = true
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if !touchSprite.containsPoint(location) {
touched = false
longTouchTimer = 0
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
touched = false
longTouchTimer = 0
}
override func update(currentTime: NSTimeInterval) {
if last_update_time == 0.0 {
delta = 0
} else {
delta = currentTime - last_update_time
}
last_update_time = currentTime
if touched {
longTouchTimer += delta
}
if longTouchTimer >= 2 {
testLabel.hidden = false
} else {
testLabel.hidden = true
}
}
}