簡體   English   中英

如何使用UISwipeGestureRecognizer代替Swift和SpriteKit的touchesMoved?

[英]How do I use UISwipeGestureRecognizer instead of touchesMoved with Swift and SpriteKit?

當嘗試使用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的建議嗎?

使用此功能可以在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://stackoverflow.com/users/5171280/brofist5

雪碧從一側移到另一側:

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]))
}

希望對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM