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. 【题解】洛谷P4145 花神游历各国(线段树)

    洛谷P4145:https://www.luogu.org/problemnew/show/P4145 思路 这道题的重点在于sqrt(1)=1 一个限制条件 与正常线段树不同的是区间修改为开方 那么 ...

  2. Css animation 与 float 、flex 布局问题

    1. 有这样一段css html 代码 <div class="container"> <div class="float-left"> ...

  3. Knowledge Point 20180305 补位的两种方式

    我们都知道Java的基本数据类型内存中都有一个固定的位数(内存分配空间),如byte占8位,int占32位等.正因如此,当把一个低精度的数据类型转成一个高精度的数据类型时,必然会涉及到如何扩展位数的问 ...

  4. Oracle子查询之高级子查询

    Oracle 高级子查询 高级子查询相对于简单子查询来说,返回的数据行不再是一列,而是多列数据. 1,多列子查询 主查询与子查询返回的多个列进行比较 查询与141号或174号员工的manager_id ...

  5. 工具 | Axure基础操作 No.2

    不废话了,直接如之前一样上操作图才是正道. 1.设置文本类型为密码或者文件类型 可以在属性中也选择最大长度制定长度. 如果设置类型为文件,在浏览器中就会自动变成选择本地文件的按钮. 2.文本框提示文字 ...

  6. 搜索 水题&&错误集锦

    引子: 本以为搜索的题目老师也不会检查,结果今天早上loli慢悠悠的说:“请同学们提交一下搜索的题目~”,顿时心旌摇曳,却也只能装作镇定自若的样子,点了点头.. 然后就开始了今天的疯狂做题,虽说题目都 ...

  7. springboot-自定义起步依赖

    自定义起步依赖步骤: 1.  添加configuration注解文件 -          指定什么情况下加载配置 -          使用enableconfigurationProperties ...

  8. layui 图片与表单一起提交 + layer.photos图片层预览

    HTML基本结构: <form class="layui-form" action="" id="feedBackForm"> ...

  9. js滚动监听

    下边代码,是监听滚动条只要移动,下方的返回顶部的div显示与隐藏的代码 ? 1 2 3 4 5 6 7 8 window.onscroll = function () {  var t = docum ...

  10. PHP Fatal error: Call to undefined function think\finfo_open()

    PHP Fatal error:  Call to undefined function think\finfo_open() php.ini      extension=php_fileinfo. ...