所以我正在使用
swift制作一个spritekit游戏.我想知道哪个最好用手势明智?
目前我有一个覆盖func touchesBegan处理所有水龙头和一个UILongPressGestureRecognizer处理长水龙头/保持.
所以你知道,按下按钮跳跃英雄.漫长的举动使得英雄成为了鸭子.
由于某种原因,我的longPress功能并不总是被调用(有时你可以按住10次然后停止被叫(不被识别),有时它是5,它会变化),这导致昨天整整一天尝试新事物和调查,这让我想到了这个问题.
使用touchesBegan或将我的所有触摸调用移动到由UITapGestureRecognizer处理的新函数是否更好?
我确实将所有内容从touchesBegan移动到了UITapGestureRecognizer,但看起来非常缓慢.但我可能一直在实施它错了?
这是识别器的设置方式:
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手势并不总是被调用,这些之间是否会有一些冲突?
解决方法
因此,如果您在红色方块上按住两秒钟,您将收到一条消息,当您放开时,消息将消失.你可能需要在那里添加一些布尔值,以确保你的角色在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 } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。