新手求助,找不到类属性

第一次发帖,可能格式会有点问题,以下是我的代码,定义了类属性 cur_loc,分别在update和mouse_down中使用,update中的代码能正常运行,但是mouse_down触发就会报错。

请问下是什么原因呢,或者有没有相应的文档指下路呢,感谢

import { _decorator, Component, EventKeyboard, EventMouse, Input, input, KeyCode, Node, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('gameRoot')
export class gameRoot extends Component {    
    cur_loc = new Vec2();

    @property(Node) player: Node;
    @property(Node) hensRoot: Node;
    @property(Node) eggsRoot: Node;


    start() {
        input.on(Input.EventType.KEY_DOWN, this.key_board_down);
        input.on(Input.EventType.MOUSE_DOWN, this.mouse_down);
    }

    key_board_down(event: EventKeyboard) {
        switch(event.keyCode){
            case KeyCode.KEY_A:
                console.log("a");
                break;
            case KeyCode.KEY_D:
                console.log("d");
                break;
        }

    }

    mouse_down(event: EventMouse) {
        console.log(event.getUILocation(this.cur_loc));    
        console.log(event.getLocation(this.cur_loc));
    }

    update(deltaTime: number) {
        console.log(this.cur_loc);
    }
}


报什么错你倒是贴出来啊 :joy:

因为你没有this指针 input.on(Input.EventType.KEY_DOWN, this.key_board_down,this);,这样去修改下你的监听代码.

2赞

抓到不认真看视频的粉丝一枚 :laughing:

image

1赞

哦哦,感谢

哈哈,大佬好,看完视频想自己写一遍,总是遗漏一些细节 :joy:

新手都会经历这一步的 :joy: