How do I change the direction of an object with swipe?

时间:2015-06-26 10:27:49

标签: ios swift sprite-kit uiswipegesturerecognizer skaction

I made a path for my object and it keeps repeating itself all the time. I need my object to stop that action and move at the direction I swipe (up, down, right, left). After stopping and then moving in the swipe direction, I need the object to resume its path when touching it.

This is my code:

 import SpriteKit

    class GameScene: SKScene {

    // The functions that defines what will happen when doing swipes
    // in the different directions.

    // The player.removeActionForKey("trailRunning") stops the
    // SKAction.repeatActionForever which is the path I created
    // for my object. trailRunning is the KeyWord I created to
    // manipulate whether the action should continue or not.

    func swipedRight(sender:UISwipeGestureRecognizer){
        println("swiped right")

        player.removeActionForKey("trailRunning")

    }

    func swipedLeft(sender:UISwipeGestureRecognizer){
        println("swiped left")

        player.removeActionForKey("trailRunning")

    }

    func swipedUp(sender:UISwipeGestureRecognizer){
        println("swiped up")

        player.removeActionForKey("trailRunning")

    }

    func swipedDown(sender:UISwipeGestureRecognizer){
        println("swiped down")

        player.removeActionForKey("trailRunning")

    }

    var bg = SKSpriteNode()
    var player = SKSpriteNode()

    // Created a startPosition for my objects path

    let startPosition = CGPointMake(20, 20)

    // pathRunning is not relevant here, but I'm using it to "Start"
    // the game.

    var pathRunning = 0


    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        // This tells that there is no gravity

        self.physicsWorld.gravity = CGVectorMake(0, 0)

        // Making swipes possible

        var swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
        swipeRight.direction = .Right
        view.addGestureRecognizer(swipeRight)


        var swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedLeft:"))
        swipeLeft.direction = .Left
        view.addGestureRecognizer(swipeLeft)


        var swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:"))
        swipeUp.direction = .Up
        view.addGestureRecognizer(swipeUp)


        var swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
        swipeDown.direction = .Down
        view.addGestureRecognizer(swipeDown)


        // Adding background

        var bgTexture = SKTexture(imageNamed: "bg")
        bg = SKSpriteNode(texture: bgTexture)
        bg.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
        bg.size.height = self.frame.height
        bg.size.width = self.frame.width

        self.addChild(bg)

        // Adding player

        var playerTexture = SKTexture(imageNamed: "player")
        player = SKSpriteNode(texture: playerTexture)
        player.position = startPosition

        // Defining the PhysicsBody, so I will be able to
        // make collision detection. The density will make it 
        // possible for me to use something as applyForce().

        player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
        player.physicsBody?.density = 1.0
        player.physicsBody?.allowsRotation = false

        self.addChild(player)

    }

    // playerPath is a function I made so that the path will be
    // the same on every iPhone size.

    func playerPath() {

        if frame.height == 480 {

            var movePlayerToBottomRight = SKAction.moveTo(CGPointMake(300,20), duration: 1.5)
            var movePlayerToTopRight = SKAction.moveTo(CGPointMake(300,460), duration: 2.5)
            var movePlayerToTopLeft = SKAction.moveTo(CGPointMake(20,460), duration: 1.5)
            var movePlayerToStartPosition = SKAction.moveTo(startPosition, duration: 2.5)

            var playerTrail = SKAction.repeatActionForever(SKAction.sequence([movePlayerToBottomRight, movePlayerToTopRight, movePlayerToTopLeft, movePlayerToStartPosition]))

            player.runAction(playerTrail, withKey: "trailRunning")

        } else if frame.height == 568 {

            var movePlayerToBottomRight = SKAction.moveTo(CGPointMake(300,20), duration: 1.5)
            var movePlayerToTopRight = SKAction.moveTo(CGPointMake(300,548), duration: 2.5)
            var movePlayerToTopLeft = SKAction.moveTo(CGPointMake(20,548), duration: 1.5)
            var movePlayerToStartPosition = SKAction.moveTo(startPosition, duration: 2.5)

            var playerTrail = SKAction.repeatActionForever(SKAction.sequence([movePlayerToBottomRight, movePlayerToTopRight, movePlayerToTopLeft, movePlayerToStartPosition]))

            player.runAction(playerTrail, withKey: "trailRunning")

        } else if frame.height == 667 {

            var movePlayerToBottomRight = SKAction.moveTo(CGPointMake(355,20), duration: 1.5)
            var movePlayerToTopRight = SKAction.moveTo(CGPointMake(355,647), duration: 2.5)
            var movePlayerToTopLeft = SKAction.moveTo(CGPointMake(20,647), duration: 1.5)
            var movePlayerToStartPosition = SKAction.moveTo(startPosition, duration: 2.5)

            var playerTrail = SKAction.repeatActionForever(SKAction.sequence([movePlayerToBottomRight, movePlayerToTopRight, movePlayerToTopLeft, movePlayerToStartPosition]))

            player.runAction(playerTrail, withKey: "trailRunning")

        } else if frame.height == 736 {

            var movePlayerToBottomRight = SKAction.moveTo(CGPointMake(394,20), duration: 1.5)
            var movePlayerToTopRight = SKAction.moveTo(CGPointMake(394,716), duration: 2.5)
            var movePlayerToTopLeft = SKAction.moveTo(CGPointMake(20,716), duration: 1.5)
            var movePlayerToStartPosition = SKAction.moveTo(startPosition, duration: 2.5)

            var playerTrail = SKAction.repeatActionForever(SKAction.sequence([movePlayerToBottomRight, movePlayerToTopRight, movePlayerToTopLeft, movePlayerToStartPosition]))

            player.runAction(playerTrail, withKey: "trailRunning")


        }

    }


    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */

        // Here I tell that I have to press to start the game

        if pathRunning == 0 {
            playerPath()
        }

    }




    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */

    }
    }

0 个答案:

没有答案