(2)不一样的实业GKEntity亚搏app官方网站
Tagged Tags:

GameplayKit是一个面向对象的框架,为构建游戏提供基础工具和技能。
GameplayKit包罗用于设计有着功用性,可接纳架构的嬉戏的工具,以及用于创设和加强诸如剧中人物移动和对机械钟现的二22日游玩法特征的本领。

 

亚搏app官方网站 1

GamePlayKit

大家那边根本讲GKEntity和GKComponent那三个类;

亚搏app官方网站,GKEntity类(实体):
能够包容非常多组件的器皿,依照自身的急需来加盟相应的Component组件。

GKComponent类(组件):代表一种意义和表现,不相同的零部件代表分化的效果与利益。

实用功能
(1)方便通过集结四种组件,营造复杂的新实体Entity。
(2)不一样的实体GKEntity,通过汇聚分歧类其他零部件GKComponent来促成。
www.yabovip4.com,(3)不必再一次写组件,组件能够复用,也易于增添。
(4)实体能够完毕动态拉长组件,以动态获得也许去除某个职能。

平素通过代码来通晓这几个类:

 

亚搏app官方网站 2

(2)不一样的实业GKEntity亚搏app官方网站。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")
    }
}

(2)不一样的实业GKEntity亚搏app官方网站。Coca ColaComponent 组件:Smart的皮层、大小

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")
    }

}

(2)不一样的实业GKEntity亚搏app官方网站。(2)不一样的实业GKEntity亚搏app官方网站。在气象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的行使实例。

源码传送门:
越来越多游戏教学: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

发表评论

电子邮件地址不会被公开。 必填项已用*标注