運用GamePlayKit的GKEntity及GKComponent 的iOS游戲開發實例

来源:https://www.cnblogs.com/apiapia/archive/2018/07/28/9383837.html
-Advertisement-
Play Games

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


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

  GamePlayKit

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

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

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

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

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

  Flying Penguin

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


import SpriteKit
import GameplayKit

class 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 SpriteKit
import GameplayKit

class 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 Foundation
import GameplayKit
import SpriteKit

class 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 GameplayKit
import SpriteKit

class 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的案例代碼:


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 人應該自信點,因為在某個方面,你無人可取代。做事,做人都要有底線,一件事的底線是什麼,做人的底線是什麼,做事的底線要符合做人的底線。這些事都要清楚。 工作要努力,對你最直接的回饋,就是努力工作所應得的報酬。做人要積極上進,(欲望驅使,興趣驅使,職業規劃,人生態度,生活態度驅使等等) 我今年的計劃,是 ...
  • 在使用 RabbitMQ 的時候,作為消息發送方希望杜絕任何消息丟失或者投遞失敗場景。RabbitMQ 為我們提供了兩個選項用來控制消息的投遞可靠性模式。 rabbitmq 整個消息投遞的路徑為: producer->rabbitmq broker cluster->exchange->que... ...
  • 對於我們開發者來說,設計與實現REST API似乎已經成為了我們的日常生活。API現在已經成為了系統間互通的預設方式。AMAZON就是一個有效的使用API進行系統間溝通的最好的例子。在這篇文章中,我將重點討論如何幫助你設計更好的API並避免一些常見的誤區。 ...
  • 首先點擊File-àNew-àWeb [roject-à在Projcet Name里寫項目名-à點擊finish-à會出來一個框,選擇NO,一個javaweb項目就創建好了。具體請看下圖! 配置伺服器連接: 找到Server-à下麵的Tomcat 7.x點擊右鍵-à點擊Configure Serve ...
  • 想著寫一篇hibernate的博文,於是準備從頭開始,從官網下了最新的穩定版本來做講述。 結果利用hibernate自動建表的時候發生下麵這個問題。 我很納悶,之前用低版本一點的沒有發生這個問題啊。 於是,我把必要文件都拷到之前那個hibernate版本是5.0.7的工程中,結果並沒有發生問題。 所... ...
  • [TOC] 翻譯自《Demo Week: Tidy Forecasting with sweep》 原文鏈接:www.business science.io/code tools/2017/10/25/demo_week_sweep.html 時間序列分析工具箱——sweep 的用途 正如 包之於 ...
  • Python名片管理系統 找了很久的 bug,最後發現是 input 沒轉換類型…… ...
  • <! done 本節內容 Python介紹 發展史 Python 2 or 3? 安裝 Hello World程式 變數 用戶輸入 模塊初識 .pyc是個什麼鬼? 數據類型初識 數據運算 表達式if ...else語句 表達式for 迴圈 break and continue  表達式wh ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...