Group Group Group Group Group Group Group Group Group

SKAction can't work well on Enity-Component

#1

According to Flappy-Felipe Video Guide

it use the Entity-Component like this

class PlayerEntity:GKEntity {
//引入对象内的组件
var spriteComponent : SpriteComponent! // 对象组件
var movementComponent: MovementComponent! // 运动组件
var animationComponent: AnimationComponent! // 自身动作组件

var movementAllowed = false  // 是否处于运行当中;

// 初始化
init(imageName:String) {
     super.init()
    let texture = SKTexture(imageNamed: imageName)
    // 1.对象组件;
    spriteComponent = SpriteComponent(entity: self, texture: texture, size: texture.size())
    addComponent(spriteComponent)
    // 2.运动组件;
    movementComponent = MovementComponent(entity: self)
    addComponent(movementComponent)
    // 3.自身动作组件; 斗篷舞动
    // 导入多帧动画; 运用atlas
    var playerTextures = [SKTexture]()
    let playerAtlas = SKTextureAtlas(named: "penguin")
    for i in 1...playerAtlas.textureNames.count {
        let imageName = "penguin0\(i).png" // 获得图片名称
        playerTextures.append(SKTexture(imageNamed: imageName))
    }
    animationComponent = AnimationComponent(entity: self, textures: playerTextures)
    addComponent(animationComponent)
    
    // 节点的物理状态 spriteComponent内的node变量
    let spriteNode = spriteComponent.node
    // 建立物理体
    spriteNode.physicsBody = SKPhysicsBody(texture: spriteNode.texture!, size: spriteNode.size)
    // 监测碰撞
    // 1.标识
    spriteNode.physicsBody?.categoryBitMask = PhysicsCategory.Player
    // 2.会和谁相撞
    spriteNode.physicsBody?.contactTestBitMask = PhysicsCategory.Ground | PhysicsCategory.Obstacle
    // 3.相撞后会弹开吗
    spriteNode.physicsBody?.collisionBitMask = PhysicsCategory.None
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

The SpriteComponent works,but the SKAction in AnimationComponent can’t work,it’s pretty weird…

import SpriteKit
import GameplayKit

class AnimationComponent:GKComponent {
    // 引用已经建好的组件
    let spriteComponent:SpriteComponent
    // 数组变量
    var textures:[SKTexture]
    
    // 因为有自身的动作,如flapping拍打翅膀、斗篷舞动、上下舞动;
    // init时,多初始化一个变量 texture,以便接收外部的图片
    init(entity:GKEntity,textures:[SKTexture]){
        self.spriteComponent = entity.component(ofType: SpriteComponent.self)!
        self.textures = textures   // 初始化数组变量
        super.init()
    }
   
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func update(deltaTime seconds: TimeInterval) {
        // 是不是PlayerEntity组件
        if let player = entity as? PlayerEntity {
            if player.movementAllowed {
                startAnimation()
                startWobble()
            }else {
                stopAnimation("Flap")
            }
        }
    }
    // 开始拍打翅膀+斗篷
    func startAnimation(){
       
        let playerAnimation = SKAction.animate(with: textures, timePerFrame: 0.1)
        let repeatAction = SKAction.repeatForever(playerAnimation)
        //哪个节点执行action
        spriteComponent.node.run(repeatAction, withKey: "Flap")

    }
    
    func stopAnimation(_ name:String){
        spriteComponent.node.removeAction(forKey: name)
    }
    // 上下飞动 wobbling
    func startWobble(){
       
        let moveUp = SKAction.moveBy(x: 0, y: 80, duration: 0.4)
        moveUp.timingMode = .easeInEaseOut
        let moveDown = moveUp.reversed()
        spriteComponent.node.run(SKAction.sequence([moveUp,moveDown]))
     
        spriteComponent.node.run(SKAction.repeatForever(SKAction.animate(with: textures, timePerFrame: 0.07)))
    }
    
    func stopWobble(){
        stopAnimation("Wobble")
        stopAnimation("WobbleFlap")
    }
}
#2

@apiapia What error are you getting exactly?

closed #3

This topic was automatically closed after 166 days. New replies are no longer allowed.