-
Creator 版本:2.4
-
目标平台:
-
重现方式:
-
首个报错:
-
之前哪个版本是正常的:
-
手机型号:
-
手机浏览器:
-
编辑器操作系统:
-
重现概率:
新手学习,使用回调函数的时候一直显示TypeError: Cannot read property ‘default’ of null,问一下应该怎么解决
这个是管理类
const {ccclass, property} = cc._decorator;
//对游戏的操作都应该写在管理器里面 加分 游戏结束
@ccclass
export default class NewClass extends cc.Component {
//小鸟预设体
@property(cc.Prefab)
birdPre: cc.Prefab;
//1s出现一只小鸟
time: number = 1;
//分数
score: number = 0;
start () {
//每隔1s创建一个小鸟,除了此法还可以用之前学的计时器
this.node.runAction(cc.repeatForever(cc.sequence(cc.delayTime(this.time), cc.callFunc(()=>{
//创建预设体
let bird = cc.instantiate(this.birdPre);
//设置父物体
bird.setParent(this.node);
bird.y = this.node.y;
bird.x = Math.random() * 220 - 110;
//飞
bird.getComponent(BirdControl).fly();
//加分回调
bird.getComponent(BirdControl).addScoreCallBack = ()=>{
this.score += 100;
console.log("分数" + this.score);
};
//游戏结束回调
bird.getComponent(BirdControl).dieCallBack = () =>{
this.node.destroyAllChildren();
//停止继续创建小鸟
this.node.stopAllActions();
console.log("死亡");
};
}))))
}
// update (dt) {}
}
这个是他的子节点上的脚本
const {ccclass, property} = cc._decorator;
@ccclass
export default class BirdControl extends cc.Component {
//生命值
hp: number =1;
//目标位置 +-110 , 186
targetPos: cc.Vec2 = null;
//速度
spped: number = 50;
//游戏结束回调
dieCallBack: Function;
//加分回调
addScoreCallBack: Function;
@property(cc.Label)
label: cc.Label = null;
@property
text: string = 'hello';
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start () {
this.fly();
}
fly(){
//生成随机目标点
this.targetPos = cc.v2(Math.random() * 220 - 110, 90);
//翻转精灵
if(this.targetPos.x > this.node.x){
this.node.scaleX = -1;
}
//移动 速度*时间=距离 第一个参数是时间,第二个是目的地
let move = cc.moveTo((this.targetPos.y - this.node.y) / this.spped, this.targetPos);
let seq = cc.sequence(move,cc.callFunc(()=>{
//游戏结束
this.dieCallBack();
}));
this.node.runAction(seq);
//如果飞出屏幕
//如果触摸精灵
this.node.on(cc.Node.EventType.TOUCH_START,(event)=>{
//如果还活着
if (this.hp > 0){
//血量减少
this.hp -- ;
//停止飞翔动作
this.node.stopAllActions();
//向下掉落
this.getComponent(cc.Animation).play("birddie");
let move_die = cc.moveTo(this.node.y/(this.spped * 2), cc.v2(this.node.x,0));
this.node.runAction(cc.sequence(move_die,cc.callFunc(()=>{
//销毁自身
this.node.destroy();
})));
//加分
this.addScoreCallBack();
}
})
}
// update (dt) {}
}
回调函数一个是addScoreCallBack(),一个是dieCallBack();
诚恳请教大佬们这个问题出在哪里