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

运用GamePlayKit的GKEntity及GKComponent 的iOS游戏开发实例的更多相关文章

  1. 开发者经验谈:如何一天时间搞定iOS游戏开发?

    开发者经验谈:如何一天时间搞定iOS游戏开发? 在一天时间里将完成iPhone游戏开发由梦想变为现实? 本文作者给出了从创意转变成现实的详细答案.使用苹果原生游戏引擎SpriteKit,遵循一定的原则 ...

  2. ios游戏开发 Sprite Kit教程:初学者 1

    注:本文译自Sprite Kit Tutorial for Beginners 目录 Sprite Kit的优点和缺点 Sprite Kit vs Cocos2D-iPhone vs Cocos2D- ...

  3. cocos2d-x ios游戏开发初认识(八) 触摸事件与碰撞检測

    玩过植物大战僵尸都知道,要在草坪里放一朵向日葵或者其他的植物仅仅需触摸那个植物将其拖入到想要摆放的位置,这事实上就是这节要写的触摸事件.还能够发现当我们的僵尸出来的时候,我们的小豌豆会发子弹攻击僵尸, ...

  4. iOS游戏开发游戏功能之外的东西

    对于一个游戏的开发,我们除了完毕游戏的功能之外,还有多少东西我们须要考虑呢? 非常多.也非常烦! 但做过一遍之后下一次就会非常easy. 都有什么东西我们想加入到游戏其中呢? (1)分享功能 (2)评 ...

  5. cocos2d-x ios游戏开发初认识(九) 音效、粒子系统与存储

    我们知道.一个游戏少不了声音.一些好听的声音会提起你对游戏的兴趣,当然做好听的声音不是我们要学的,我们的目的是把声音在适当的时候放出来.顺便在这节中会说下简单的粒子系统和文件存储. 一.声音的播放: ...

  6. ios游戏开发--cocos2d学习(2)

    在第一节中简单介绍了2d项目模板HelloWorld的基础代码,并做了一点小小的改变,像触摸接收.旋转.移动和颜色转变序列CCSequence的使用等等,2d本身封装好了很多方便使用的动作,只需要调用 ...

  7. ios游戏开发--cocos2d学习(1)

    学习cocos2d需要一定的编程基础,最好了解objective-c的语法.至于下载和安装的过程网上有很多,这里不多介绍,直接进入项目的学习. 创建一个cocos2d项目,直接运行,效果如图: 左下角 ...

  8. ios游戏开发--cocos2d学习(3)

    ------------继续上一节的内容. “接收触摸事件”: CCLayer类是用来接收触摸输入的.不过你要首先启用这个功能才可以使用它. self.isTouchEnabled = YES;//此 ...

  9. cocos2d-x ios游戏开发初认识(五) CCsprite精灵类

    这次写一下精灵创建的几种类型: 一.通过文件创建: 在原有的基础上加入例如以下代码: //一.通过文件创建精灵 CCSprite *bg =CCSprite::create("map.png ...

随机推荐

  1. px,rem,em的区别

    PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的原因在于其使用了em或rem作为字体单位: 3. Firefox能够调整px和em,rem,但是96%以上 ...

  2. Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法

    Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法 从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性. ...

  3. iOS:文件操作相关(18-03-23更)

    0.iOS文件系统 1.工程内文件 2.文件夹管理 3.文件操作 4.NSCache 附录: 1.沙盒文件夹.文件大小 2.清除沙盒 Library / Cache 下所有数据 3.测试plist 0 ...

  4. Swift_方法

    Swift_方法 点击查看源码 ///方法 class Methods: NSObject { func test() { // self.testInstanceMethods() //实例方法 s ...

  5. Ubuntu更换国内源

    打开终端,输入:sudo gedit /etc/apt/sources.list 在文件最底部输入以下内容: deb http://mirrors.ustc.edu.cn/ubuntu/ xenial ...

  6. zabbix基本监控各指标简解

    监控项目及使用模板 监控http和https: Template App HTTP Service     Template App HTTPS Service 监控cpu,内存,网络等: Templ ...

  7. PHP | 获取数组长度的方法

    一.获取一维数组的长度 count.sizeof 都可以直接统计一维数组长度. 例如:$arr = Array('0','1','2','3','4');       echo count($arr) ...

  8. 如何让tomcat服务器运行在80端口,并且无需输入项目名即可访问项目()

    这个问题最开始遇到的时候是半年前,自己买了个服务器玩,但是域名解析的时候出了问题,我查了查资料才知道腾讯云是默认解析到80端口,而且还改不了. 首先是修改tomcat运行端口号,默认是8080,但是我 ...

  9. Js 手指事件

    1.Touch事件简介   pc上的web页面鼠标会产生onmousedown.onmouseup.onmouseout.onmouseover.onmousemove的事件,但是在移动终端如ipho ...

  10. hadoop生态搭建(3节点)-15.Nginx_Keepalived_Tomcat配置

    # Nginx+Tomcat搭建高可用服务器名称 预装软件 IP地址Nginx服务器 Nginx1 192.168.6.131Nginx服务器 Nginx2 192.168.6.132 # ===== ...