【问题标题】:Cocos Creator and TypeScript use enum as propertyCocos Creator 和 TypeScript 使用枚举作为属性
【发布时间】:2018-08-17 18:09:48
【问题描述】:

这个问题与 TypeScript 密切相关。如果我在 Cocos Creator 中有基于 JavaScript 的 Cocos 项目,我遇到的问题可以完美运行。

我在名为 enums 的文件中有以下枚举。

export const enum CollisionType {
Static=    0,
Dynamic=   1,
Solid=     2,
SemiSolid= 3
}

在我的 game.ts 文件中,我有以下内容

import {CollisionType} from "./Enums";

export default class PlayerControl extends cc.Component {

@property (CollisionType)
collisionType:CollisionType = CollisionType.Static;

但不幸的是,这不起作用。我在 CollisionType 下得到一条红线并出现以下错误:

Argument of type 'typeof CollisionType' is not assignable to parameter of type 'string | number | boolean | Function | any[] | { type?: any; visible?: boolean | (() => boolean); displayName?: string; tooltip?: string; multiline?: boolean; readonly?: boolean; min?: number; max?: number; ... 7 more ...; animatable?: boolean; } | ValueType'.

如果我不将@property 设置为我的变量,它可以正常工作,但是无法在 Cocos Creator 编辑器中编辑此值。

有什么想法吗?

【问题讨论】:

    标签: typescript cocoscreator


    【解决方案1】:

    试试下面的代码

    @property ({type:Enum(CollisionType)})
    private collisionType:CollisionType = CollisionType.Static;
    

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2020-10-26
      相关资源
      最近更新 更多