恢复对象路径而不是重启路径,怎么样?

时间:2015-06-28 05:46:44

标签: ios swift sprite-kit

所以我创建了一个碰撞检测器功能,它在检测到碰撞时重新启动玩家对象路径。 我正在试图找出如何恢复该路径 - 从玩家对象击中的点再次开始路径。 我应该采用什么方法来实现这一目标?

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {


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

var topBoarder = SKSpriteNode()
var bottomBoarder = SKSpriteNode()
var rightBoarder = SKSpriteNode()
var leftBoarder = SKSpriteNode()

// Created a startPosition for my objects path.

let startPosition = CGPointMake(30, 30)

// The default value is 0, which means the game is not active.

var pathRunning = 0

// Defining different groups to be able to make them interact.

let boarderLineGroup:UInt32 = 1
let playerGroup:UInt32 = 2


// 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.

// applyImpulse makes it possible to, in this case, swipe the player
// object in a direction.

func swipedRight(sender:UISwipeGestureRecognizer){

    player.removeActionForKey("trailRunning")
    pathRunning = 0
    player.physicsBody?.applyImpulse(CGVectorMake(10, 0))
}

func swipedLeft(sender:UISwipeGestureRecognizer){

    player.removeActionForKey("trailRunning")
    pathRunning = 0
    player.physicsBody?.applyImpulse(CGVectorMake(-10, 0))
}

func swipedUp(sender:UISwipeGestureRecognizer){

    player.removeActionForKey("trailRunning")
    pathRunning = 0
    player.physicsBody?.applyImpulse(CGVectorMake(0, 10))
}

func swipedDown(sender:UISwipeGestureRecognizer){

    player.removeActionForKey("trailRunning")
    pathRunning = 0
    player.physicsBody?.applyImpulse(CGVectorMake(0, -10))
}



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


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


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

    bg.zPosition = 1
    self.addChild(bg)


    // Adding boarder lines

    var topBoarderTexture = SKTexture(imageNamed: "topBoarder")
    topBoarder = SKSpriteNode(texture: topBoarderTexture)
    topBoarderPosition()

    topBoarder.physicsBody = SKPhysicsBody(rectangleOfSize: topBoarder.size)
    topBoarder.physicsBody?.dynamic = false
    topBoarder.physicsBody?.allowsRotation = false
    topBoarder.physicsBody?.categoryBitMask = boarderLineGroup
    topBoarder.physicsBody?.collisionBitMask = playerGroup
    topBoarder.physicsBody?.contactTestBitMask = playerGroup

    topBoarder.zPosition = 2
    self.addChild(topBoarder)

    var bottomBoarderTexture = SKTexture(imageNamed: "bottomBoarder")
    bottomBoarder = SKSpriteNode(texture: bottomBoarderTexture)
    bottomBoarder.position = CGPoint(x: CGRectGetMidX(self.frame), y: 5)

    bottomBoarder.physicsBody = SKPhysicsBody(rectangleOfSize: bottomBoarder.size)
    bottomBoarder.physicsBody?.dynamic = false
    bottomBoarder.physicsBody?.allowsRotation = false
    bottomBoarder.physicsBody?.categoryBitMask = boarderLineGroup
    bottomBoarder.physicsBody?.collisionBitMask = playerGroup
    bottomBoarder.physicsBody?.contactTestBitMask = playerGroup

    bottomBoarder.zPosition = 2
    self.addChild(bottomBoarder)

    var leftBoarderTexture = SKTexture(imageNamed: "leftBoarder")
    leftBoarder = SKSpriteNode(texture: leftBoarderTexture)
    leftBoarder.position = CGPoint(x: 5, y: CGRectGetMidY(self.frame))

    leftBoarder.physicsBody = SKPhysicsBody(rectangleOfSize: leftBoarder.size)
    leftBoarder.physicsBody?.dynamic = false
    leftBoarder.physicsBody?.allowsRotation = false
    leftBoarder.physicsBody?.categoryBitMask = boarderLineGroup
    leftBoarder.physicsBody?.collisionBitMask = playerGroup
    leftBoarder.physicsBody?.contactTestBitMask = playerGroup

    leftBoarder.zPosition = 2
    self.addChild(leftBoarder)

    var rightBoarderTexture = SKTexture(imageNamed: "rightBoarder")
    rightBoarder = SKSpriteNode(texture: rightBoarderTexture)
    rightBoarderPosition()

    rightBoarder.physicsBody = SKPhysicsBody(rectangleOfSize: rightBoarder.size)
    rightBoarder.physicsBody?.dynamic = false
    rightBoarder.physicsBody?.allowsRotation = false
    rightBoarder.physicsBody?.categoryBitMask = boarderLineGroup
    rightBoarder.physicsBody?.collisionBitMask = playerGroup
    rightBoarder.physicsBody?.contactTestBitMask = playerGroup

    rightBoarder.zPosition = 2
    self.addChild(rightBoarder)


    // 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?.dynamic = true
    player.physicsBody?.density = 1.0
    player.physicsBody?.allowsRotation = false
    player.physicsBody?.categoryBitMask = playerGroup
    player.physicsBody?.collisionBitMask = boarderLineGroup
    player.physicsBody?.contactTestBitMask = boarderLineGroup

    player.zPosition = 10
    self.addChild(player)

}

func didBeginContact(contact: SKPhysicsContact) {

    if contact.bodyA.categoryBitMask == boarderLineGroup || contact.bodyB.categoryBitMask == boarderLineGroup {

        println(">> Contact <<")
        if pathRunning == 0 {

            playerPath()
            pathRunning = 1

        }

    }
}

// 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(290,30), duration: 1.5)
        var movePlayerToTopRight = SKAction.moveTo(CGPointMake(290,450), duration: 2.5)
        var movePlayerToTopLeft = SKAction.moveTo(CGPointMake(30,450), 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(290,30), duration: 1.5)
        var movePlayerToTopRight = SKAction.moveTo(CGPointMake(290,538), duration: 2.5)
        var movePlayerToTopLeft = SKAction.moveTo(CGPointMake(30,538), 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(345,30), duration: 1.5)
        var movePlayerToTopRight = SKAction.moveTo(CGPointMake(345,637), duration: 2.5)
        var movePlayerToTopLeft = SKAction.moveTo(CGPointMake(30,637), 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(384,30), duration: 1.5)
        var movePlayerToTopRight = SKAction.moveTo(CGPointMake(384,706), duration: 2.5)
        var movePlayerToTopLeft = SKAction.moveTo(CGPointMake(30,706), 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")


    }

}

func topBoarderPosition() {

    if frame.height == 480 {

        topBoarder.position = CGPoint(x: CGRectGetMidX(self.frame), y: 475)

    } else if frame.height == 568 {

        topBoarder.position = CGPoint(x: CGRectGetMidX(self.frame), y: 563)

    } else if frame.height == 667 {

        topBoarder.position = CGPoint(x: CGRectGetMidX(self.frame), y: 662)

    } else if frame.height == 736 {

        topBoarder.position = CGPoint(x: CGRectGetMidX(self.frame), y: 731)

    }

}

func rightBoarderPosition() {

    if frame.height == 480 {

        rightBoarder.position = CGPoint(x: 315, y: CGRectGetMidY(self.frame))

    } else if frame.height == 568 {

        rightBoarder.position = CGPoint(x: 315, y: CGRectGetMidY(self.frame))

    } else if frame.height == 667 {

        rightBoarder.position = CGPoint(x: 370, y: CGRectGetMidY(self.frame))

    } else if frame.height == 736 {

        rightBoarder.position = CGPoint(x: 409, y: CGRectGetMidY(self.frame))

    }

}

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

        // pathRunning set to 1 to show that the game is now active.

        pathRunning = 1
    }

}




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

}
}

0 个答案:

没有答案