简体   繁体   English

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

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

While trying to drag and drop SKSpriteNodes with the touchesMoved function, the code that lets me drag sprites does not work when I try to swipe them. 当尝试使用touchesMoved函数拖放SKSpriteNodes时,让我拖动精灵的代码在尝试滑动它们时不起作用。

This is what I have: 这就是我所拥有的:

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

This is what I'm trying to do: 这就是我想要做的:

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

But when I try to swipe the sprite, I receive a SIGABRT error. 但是,当我尝试滑动精灵时,会收到SIGABRT错误。 I've looked at lots of sources, and have not seen an answer to a question like this. 我看过很多资料,还没有看到类似问题的答案。

Would someone please give me some advice on how to drag SKSpriteNodes using UISwipeGestureRecognizer? 有人可以给我一些有关如何使用UISwipeGestureRecognizer拖动SKSpriteNodes的建议吗?

use this function to swipe a view in Spritekit. 使用此功能可以在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 https://stackoverflow.com/users/5171280/brofist5

Sprite move from one side to another side:- 雪碧从一侧移到另一侧:

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

Hope it will help you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM