2017-04-11 53 views
1

enter image description here雪碧套件:保持內部邊框創建了瓷磚編輯

我測試的瓷磚編輯自帶的Xcode 8(8.2.2)的可能性。我已經創建了一個類似PacMan的地圖,如上所示。在矩形的左上角有一個遊戲角色。我想知道是否有一種讓遊戲角色留在藍色邊框內的簡單方法?到目前爲止,我已經通過場景編輯器在左側設置了(紅色)牆。我有以下幾行代碼。

struct PhysicsCategory { 
    static let None: UInt32 = 0 
    static let Player: UInt32 = 0b1 // 1 
    static let Edge: UInt32 = 0b10 // 2 
    static let Wall: UInt32 = 0b100 // 4 
} 

class GameScene: SKScene { 
    // MARK: - Variables 
    var background: SKTileMapNode! // background 
    var player: SKNode! // player 

    // MARK: - DidMove 
    override func didMove(to view: SKView) { 
     setupNodes() 
    } 

    func setupNodes() { 
     background = self.childNode(withName: "World") as! SKTileMapNode 
     background.physicsBody = SKPhysicsBody(edgeLoopFrom: background.frame) 
     background.physicsBody?.categoryBitMask = PhysicsCategory.Edge 

     let wall = self.childNode(withName: "Wall") 
     wall?.physicsBody = SKPhysicsBody(rectangleOf: (wall?.frame.size)!) 
     wall?.physicsBody?.isDynamic = false 
     wall?.physicsBody?.categoryBitMask = PhysicsCategory.Wall 

     player = self.childNode(withName: "Player") 
     player.physicsBody = SKPhysicsBody(circleOfRadius: 32) 
     player.physicsBody?.categoryBitMask = PhysicsCategory.Player 
     player.physicsBody?.collisionBitMask = 4 
     player.physicsBody?.allowsRotation = false 
    } 
} 

用戶將通過CoreMotion控制玩家位置。目前,遊戲角色確實尊重左邊緣。但如果地圖變得複雜,我可能會在這裏和那裏放置很多牆。這種殺死很有趣,因爲這可能會很費時。那麼,是否還有一種使遊戲角色與地圖邊界相撞的簡單方法?

enter image description here

回答

0

看看GameplayKit的尋路類,特別是GKGridGraph和GKGridGraphNode它允許你指定只有這些類型的直線路徑圖。

有蘋果在這裏一個很好的教程:https://developer.apple.com/library/content/documentation/General/Conceptual/GameplayKit_Guide/Pathfinding.html

及示範應用程式中:https://developer.apple.com/library/content/samplecode/Pathfinder_GameplayKit/Introduction/Intro.html#//apple_ref/doc/uid/TP40016461

+0

感謝。這聽起來像一個有用的提示。我不知道如何將尋路的東西實際應用到SKTileMapNode。 Apple的Pathfinder項目不使用SKTileMapNode。 –