運用GamePlayKit的GKEntity及GKComponent 的iOS遊戲開發執行個體

來源:互聯網
上載者:User

標籤:log   物件導向   images   案例   form   不同的   ted   seq   實用   

GameplayKit是一個物件導向的架構,為構建遊戲提供基礎工具和技術。 GameplayKit包含用於設計具有功能性,可重用架構的遊戲的工具,以及用於構建和增強諸如角色移動和對手行為的遊戲玩法特徵的技術。

 GamePlayKit

我們這裡主要講GKEntity和GKComponent這二個類;

GKEntity類(實體): 可以容納很多組件的容器,根據自己的需求來加入相應的Component組件。

GKComponent類(組件):代表一種功能和行為,不同的組件代表不同的功能。

實用功能
(1)方便通過彙總多種組件,構建複雜的新實體Entity。
(2)不同的實體GKEntity,通過彙總不同種類的組件GKComponent來實現。
(3)不必重複寫組件,組件可以複用,也易於擴充。
(4)實體可以實現動態添加組件,以動態獲得或者刪除某些功能。

直接通過代碼來理解這二個類:

 Flying Penguin

PenguinEntity.swift 是用來管理 Component三個組件的

import SpriteKitimport GameplayKitclass PenguinEntity:GKEntity {        var spriteComponent:SpriteComponent! // 屬性 大小 texture    var moveComponent:MoveComponent!     // 移動組件功能;    var animationComponent:AnimationComponent! //拍打翅膀的組件;        init(imageName:String) {        super.init()        let texture = SKTexture(imageNamed: imageName)        spriteComponent = SpriteComponent(entity: self, texture: texture, size: texture.size())        addComponent(spriteComponent)        // 加入上下飛動的組件        moveComponent = MoveComponent(entity: self)        addComponent(moveComponent)                // 加入拍打翅膀的動畫        let textureAltas = SKTextureAtlas(named: "penguin")        var textures = [SKTexture]()        for i in 1...textureAltas.textureNames.count {            let imageName = "penguin0\(i)"            textures.append(SKTexture(imageNamed: imageName))        }        animationComponent = AnimationComponent(entity: self, textures: textures)        addComponent(animationComponent)    }     // Add Physics    func addPhysics(){        let spriteNode = spriteComponent.node        spriteNode.physicsBody = SKPhysicsBody(texture: spriteNode.texture!, size: spriteNode.frame.size)        spriteNode.physicsBody?.categoryBitMask  = PhysicsCategory.Player        spriteNode.physicsBody?.contactTestBitMask = PhysicsCategory.Coin | PhysicsCategory.Obstacle | PhysicsCategory.Floor    }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }}

SpriteComponent 組件:精靈的皮膚、大小

import SpriteKitimport GameplayKitclass SpriteComponent :GKComponent {    let node:SKSpriteNode    init(entity:GKEntity,texture:SKTexture,size:CGSize) {        node = SKSpriteNode(texture: texture, color: SKColor.clear, size: size)        node.entity = entity        super.init()    }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }}

MoveComponent 上下飛動

import Foundationimport GameplayKitimport SpriteKitclass MoveComponent:GKComponent {    // The node on which animations should be run for this animation component.// 引入SpriteComponent 就不用每次都需要像在SpriteComponent裡建立精靈的屬性了// node = SKSpriteNode(texture: texture, color: SKColor.clear, size: size)    let spriteComponent: SpriteComponent          init(entity:GKEntity) {        self.spriteComponent = entity.component(ofType: SpriteComponent.self)!        super.init()    }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }    // 組件 上下飛動;    func startWobble(){        let moveUp   = SKAction.moveBy(x: 0, y: 50, duration: 0.5)        moveUp.timingMode = .easeInEaseOut        let moveDown = moveUp.reversed()        let sequence = SKAction.sequence([moveUp,moveDown])        let repeatWobble = SKAction.repeatForever(sequence)        spriteComponent.node.run(repeatWobble, withKey: "Wobble")    }    }

AnimationComponent 拍打翅膀

import GameplayKitimport SpriteKitclass AnimationComponent:GKComponent {    let textures:[SKTexture]    let spriteComponent: SpriteComponent        init(entity:GKEntity,textures:[SKTexture]) {        self.spriteComponent = entity.component(ofType: SpriteComponent.self)!        self.textures = textures        super.init()    }    // 翅膀拍動    func startAnimation(){        let flyAction = SKAction.animate(with: textures, timePerFrame: TimeInterval(0.02))        let repeatAction = SKAction.repeatForever(flyAction)        spriteComponent.node.run(repeatAction)    }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }   }

在情境GameScene加入管理器Entity對象

func setupEntityComponent(){        let penguin = PenguinEntity(imageName: "penguin01") // 企鵝屬於worldNode的子層級;        let penguinNode = penguin.spriteComponent.node        penguinNode.position = CGPoint(x: 320, y: 500)        penguinNode.zPosition = 5        worldNode.addChild(penguinNode)       // penguin有移動的功能        penguin.moveComponent.startWobble()       // 有拍打翅膀的功能        penguin.animationComponent.startAnimation()    }

以上就是應用GKEntity來管理三個組件GKComponent的運用執行個體。

源碼傳送門:https://github.com/apiapia/FlyingPenguinSpriteKitGameTutorial
更多遊戲教學:http://www.iFIERO.com

補充:英文夠好的話,建議上蘋果的官網看看 GameplayKit的案例代碼:

    • Boxes: GameplayKit Entity-Component Basics

    • Dispenser: GameplayKit State Machine Basics

    • Pathfinder: GameplayKit Pathfinding Basics

    • AgentsCatalog: Using the Agents System in GameplayKit

    • FourInARow: Using the GameplayKit Minmax Strategist for Opponent AI

    • DemoBots: Building a Cross Platform Game with SpriteKit and GameplayKit

運用GamePlayKit的GKEntity及GKComponent 的iOS遊戲開發執行個體

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.