引擎Bug反馈一:cc.Enum(cc.macro.KEY)序列化显示报错

import BaseBehaviour from "../../core/BaseBehaviour";

const{ccclass,property}=cc._decorator;

/** 方向键盘输入 */
@ccclass
export default class DirectionKeyboardInput extends BaseBehaviour{
    
    private static readonly LEFT:number =0x0001;
    private static readonly RIGHT:number=0x0002;
    private static readonly UP:number   =0x0004;
    private static readonly DOWN:number =0x0008;
    
    @property({type:cc.Enum(cc.macro.KEY),visible:true})
    private _leftKey:cc.macro.KEY=cc.macro.KEY.a;
    
    @property({type:cc.Enum(cc.macro.KEY),visible:true})
    private _rightKey:cc.macro.KEY=cc.macro.KEY.d;
    
    @property({type:cc.Enum(cc.macro.KEY),visible:true})
    private _upKey:cc.macro.KEY=cc.macro.KEY.w;
    
    @property({type:cc.Enum(cc.macro.KEY),visible:true})
    private _downKey:cc.macro.KEY=cc.macro.KEY.s;
    
    private _pressKeyFlags:number=0;//用于记录按下方向键的标记(一个二进制位表示一个方向键是否按下)
    private _directionNormalized:cc.Vec2=cc.Vec2.ZERO;
    
    public get directionNormalized():cc.Vec2{ return this._directionNormalized; }
    
    protected onEnable():void{
        super.onEnable();
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);
    }
    
    private onKeyDown(event:cc.Event.EventKeyboard):void{
        switch (event.keyCode) {
            case this._leftKey:
                this._pressKeyFlags|=DirectionKeyboardInput.LEFT;
                break;
            case this._rightKey:
                this._pressKeyFlags|=DirectionKeyboardInput.RIGHT;
                break;
            case this._upKey:
                this._pressKeyFlags|=DirectionKeyboardInput.UP;
                break;
            case this._downKey:
                this._pressKeyFlags|=DirectionKeyboardInput.DOWN;
                break;
            default:
                break;
        }
        this.updateDirectionNormalized();
    }
    
    private onKeyUp(event:cc.Event.EventKeyboard):void{
        switch (event.keyCode) {
            case this._leftKey:
                this._pressKeyFlags&=~DirectionKeyboardInput.LEFT;
                break;
            case this._rightKey:
                this._pressKeyFlags&=~DirectionKeyboardInput.RIGHT;
                break;
            case this._upKey:
                this._pressKeyFlags&=~DirectionKeyboardInput.UP;
                break;
            case this._downKey:
                this._pressKeyFlags&=~DirectionKeyboardInput.DOWN;
                break;
            default:
                break;
        }
        this.updateDirectionNormalized();
    }
    
    private updateDirectionNormalized():void{
        let x:number=0,y:number=0;
        if((this._pressKeyFlags&(DirectionKeyboardInput.LEFT|DirectionKeyboardInput.RIGHT))>0){
            x=(this._pressKeyFlags&DirectionKeyboardInput.LEFT)>0?-1:1;
        }
        if((this._pressKeyFlags&(DirectionKeyboardInput.UP|DirectionKeyboardInput.DOWN))>0){
            y=(this._pressKeyFlags&DirectionKeyboardInput.DOWN)>0?-1:1;
        }
        this._directionNormalized.x=x;
        this._directionNormalized.y=y;
    }
    
    protected onDisable():void{
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);
        this._pressKeyFlags=0;
        this.updateDirectionNormalized();
        super.onDisable();
    }
    
}

不许侮辱我Cocos !

张嘴就来?

换成 type:cc.macro.KEY

1赞

:+1: 不过稍微有点标题党

不行,还有错。

我建议施行劝退措施 能力不行就欲盖拟彰 害怕

// import BaseBehaviour from "../../core/BaseBehaviour";

const{ccclass,property}=cc._decorator;

/** 方向键盘输入 */
@ccclass
export default class DirectionKeyboardInput extends cc.Component{
    
    private static readonly LEFT:number =0x0001;
    private static readonly RIGHT:number=0x0002;
    private static readonly UP:number   =0x0004;
    private static readonly DOWN:number =0x0008;
    
    @property({type: cc.macro.KEY, visible:true})
    private _leftKey:cc.macro.KEY=cc.macro.KEY.a;
    
    @property({type: cc.macro.KEY, visible:true})
    private _rightKey:cc.macro.KEY=cc.macro.KEY.d;
    
    @property({type:cc.macro.KEY, visible:true})
    private _upKey:cc.macro.KEY=cc.macro.KEY.w;
    
    @property({type:cc.macro.KEY, visible:true})
    private _downKey:cc.macro.KEY=cc.macro.KEY.s;
    
    private _pressKeyFlags:number=0;//用于记录按下方向键的标记(一个二进制位表示一个方向键是否按下)
    private _directionNormalized:cc.Vec2=cc.Vec2.ZERO;
    
    public get directionNormalized():cc.Vec2{ return this._directionNormalized; }
    
    protected onEnable():void{
        super.onEnable();
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);
    }
    
    private onKeyDown(event:cc.Event.EventKeyboard):void{
        switch (event.keyCode) {
            case this._leftKey:
                this._pressKeyFlags|=DirectionKeyboardInput.LEFT;
                break;
            case this._rightKey:
                this._pressKeyFlags|=DirectionKeyboardInput.RIGHT;
                break;
            case this._upKey:
                this._pressKeyFlags|=DirectionKeyboardInput.UP;
                break;
            case this._downKey:
                this._pressKeyFlags|=DirectionKeyboardInput.DOWN;
                break;
            default:
                break;
        }
        this.updateDirectionNormalized();
    }
    
    private onKeyUp(event:cc.Event.EventKeyboard):void{
        switch (event.keyCode) {
            case this._leftKey:
                this._pressKeyFlags&=~DirectionKeyboardInput.LEFT;
                break;
            case this._rightKey:
                this._pressKeyFlags&=~DirectionKeyboardInput.RIGHT;
                break;
            case this._upKey:
                this._pressKeyFlags&=~DirectionKeyboardInput.UP;
                break;
            case this._downKey:
                this._pressKeyFlags&=~DirectionKeyboardInput.DOWN;
                break;
            default:
                break;
        }
        this.updateDirectionNormalized();
    }
    
    private updateDirectionNormalized():void{
        let x:number=0,y:number=0;
        if((this._pressKeyFlags&(DirectionKeyboardInput.LEFT|DirectionKeyboardInput.RIGHT))>0){
            x=(this._pressKeyFlags&DirectionKeyboardInput.LEFT)>0?-1:1;
        }
        if((this._pressKeyFlags&(DirectionKeyboardInput.UP|DirectionKeyboardInput.DOWN))>0){
            y=(this._pressKeyFlags&DirectionKeyboardInput.DOWN)>0?-1:1;
        }
        this._directionNormalized.x=x;
        this._directionNormalized.y=y;
    }
    
    protected onDisable():void{
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);
        this._pressKeyFlags=0;
        this.updateDirectionNormalized();
        super.onDisable();
    }
    
}

我稍微修改了一下,在本地测试没问题呢。

1赞

重新加载引擎时报错,重新打开CocosCreator或菜单Developer->Reload的时候

辣鸡引擎害我生气毁我青春:joy:

我刚试了,我这里也出错了

辣鸡何必为难辣鸡。。

辣鸡何必为难辣鸡。。


你的这个错误并不影响实际代码,只是看起来不好看.
错误原因是 KEY 枚举中声明了几个相同的枚举值. 比如:


而别人为了防止你自定义枚举时出错, 加了error 日志. 被你这个垃圾货瞎几巴用, 还能被你喷. 你干脆自己写引擎玩去吧. 凑什么热闹.

对象引用的问题。下面参考这个代码解决:

// import BaseBehaviour from "../../core/BaseBehaviour";

const{ccclass,property}=cc._decorator;
var KEY = cc.Enum({});
Object.assign(KEY, cc.macro.KEY);
/** 方向键盘输入 */
@ccclass
export default class DirectionKeyboardInput extends cc.Component{
    
    private static readonly LEFT:number =0x0001;
    private static readonly RIGHT:number=0x0002;
    private static readonly UP:number   =0x0004;
    private static readonly DOWN:number =0x0008;
    
    @property({type: KEY})
    private a = KEY.a

    start () {
        console.log(this.a);
    }
}
1赞

小兄弟这会大写的尴尬:innocent:

辣鸡何必为难辣鸡。。

火气不要这么大啊都:joy:

不支持这样使用,cc.macro.KEY 只是一个普通的字典,不能用于枚举的定义。否则引擎直接定义成枚举就好了,干嘛要开发者自己组装?

另外如果你真的觉得很垃圾,还是不要为难自己比较好,欢迎出门左转 laya/egret

3赞

奥利给~~