如何在 Swift 和 SpriteKit 中使用 UISwipeGestureRecognizer 而不是 touchesMoved?

Posted

技术标签:

【中文标题】如何在 Swift 和 SpriteKit 中使用 UISwipeGestureRecognizer 而不是 touchesMoved?【英文标题】:How do I use UISwipeGestureRecognizer instead of touchesMoved with Swift and SpriteKit? 【发布时间】:2015-07-29 23:06:40 【问题描述】:

在尝试使用 touchesMoved 函数拖放 SKSpriteNodes 时,当我尝试滑动精灵时,让我拖动精灵的代码不起作用。

这就是我所拥有的:

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
    for touch: AnyObject in touches
        let location = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(location)
        let MySprite = SKSpriteNode(imageNamed: "image.png")

        if touchedNode == MySprite
            MySprite.position = location
        
    

这就是我想要做的:

func swipedRight(sender:UISwipeGestureRecognizer)
    let MySprite = SKSpriteNode(imageNamed: "image.png")
    let touch = UITouch()
    let location = touch.locationInNode(self)
    let touchedNode = self.nodeAtPoint(location)

    if touchedNode == MySprite
        MySprite.position = location
    


override func didMoveToView(view: SKView) 
    let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight"))
    swipeRight.direction = .Right
    view.addGestureRecognizer(swipeRight)

但是当我尝试滑动精灵时,我收到一个 SIGABRT 错误。我查看了很多资源,但没有看到此类问题的答案。

有人能给我一些关于如何使用 UISwipeGestureRecognizer 拖动 SKSpriteNodes 的建议吗?

【问题讨论】:

【参考方案1】:

使用此功能在 Spritekit 中滑动视图。

    var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
    var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))

func handleSwipes(sender:UISwipeGestureRecognizer ) 
    if (sender.direction == .Right) 
        if screenIndex > 0  && disableFlag == 0
            disableFlag         =   1
            let action = SKAction.moveTo(CGPointMake(scrollBg.position.x + frame.size.width, scrollBg.position.y), duration: 0.10)
            let resetAction    =   SKAction.runBlock 
                self.disableFlag         =   0
            
            scrollBg.runAction(SKAction.sequence([action,resetAction]))
            screenIndex--
            var suiteStatus = NSUserDefaults.standardUserDefaults().integerForKey(itemIDArr[screenIndex] as NSString)
            if suiteStatus == 2 
                playEffectSound(suitSoundArray[screenIndex] as NSString)
            
        
    
    if (sender.direction ==  .Left)
        if screenIndex < itemIDArr.count-1  && disableFlag == 0
            disableFlag         =   1
            let action = SKAction.moveTo(CGPointMake(scrollBg.position.x - frame.size.width, scrollBg.position.y), duration: 0.10)
            let resetAction    =   SKAction.runBlock 
                self.disableFlag         =   0
            
            scrollBg.runAction(SKAction.sequence([action,resetAction]))
            screenIndex++
            var suiteStatus = NSUserDefaults.standardUserDefaults().integerForKey(itemIDArr[screenIndex] as NSString)
                if suiteStatus == 2 
                    playEffectSound(suitSoundArray[screenIndex] as NSString)
            
        
    
 

https://***.com/users/5171280/brofist5

【讨论】:

有没有办法拖动精灵?您的代码显示了如何拖动视图 这样好多了。非常感谢【参考方案2】:

精灵从一侧移动到另一侧:-

func moveGooses() 
    let path = UIBezierPath()
    path.moveToPoint(CGPoint(x:self.parentScene.frame.width*0.99, y: self.parentScene.frame.height*0.90)) path.addCurveToPoint(CGPoint( x:self.parentScene.frame.width*0.01, y: self.parentScene.frame.height*0.90), controlPoint1: CGPoint(x: self.parentScene.frame.width*0.01, y:self.parentScene.frame.height*0.90) , controlPoint2: CGPoint(x: self.parentScene.frame.width*0.01, y:self.parentScene.frame.height*0.90))
    let anim = SKAction.followPath(path.CGPath, asOffset: false, orientToPath: false, duration: 3.0)

    let seq = SKAction.sequence([anim])
    let removeAction = SKAction.runBlock
        let myGameScene =   self.parentScene as GameScene
        self.removeFromParent()
    
    self.runAction(SKAction.sequence([seq,removeAction]))

希望对你有帮助

【讨论】:

以上是关于如何在 Swift 和 SpriteKit 中使用 UISwipeGestureRecognizer 而不是 touchesMoved?的主要内容,如果未能解决你的问题,请参考以下文章

如何在情节提要中创建一个按钮以启动在 xcode swift 中使用 spritekit 创建的游戏

使用 swift 和 spritekit,你如何阻止场景在变得活跃/移动到前台后取消暂停?

如何使用 Swift 在 SpriteKit 程序中初始化/移动相机?

如何在swift(SpriteKit)中绘制一个作为单个节点的圆点[关闭]

在更新函数中使用 spriteKit 增加 spriteNode 的大小(Swift)

SpriteKit + Swift - 如何通过缓动移动节点?