最近刚刚开始学习cocos creator。按照教程写了一个flappy bird的脚本。在实现管道预制体的时候遇到了一个问题。我在管道脚本里写的init()方法无法正常运行,在Chrome下运行时显示Uncaught TypeError: Cannot read property 'init' of null
以下是生成管道预制体的方法,程序在第二句报错了,求大神解答
spawnPipes:function()
{
//生成上管道实例
var pipeUp = cc.instantiate(this.pipePrefabs[0]);
//定义为上端类型
pipeUp.getComponent('Pipe').init(0);
//获取管道高度
var pipeHeight = pipeUp.getComponent('cc.Sprite').spriteFrame.getRect().height;
//设置上端管道的横向起始位置
pipeUp.x = this.size.width/2 + this.pipeMaxOffsetX;
//设置上端管道的纵向起始位置
pipeUp.y = Math.floor(Math.random()*this.pipeMaxOffsetY)+pipeHeight/2;
//生成下管道实例(以下同理)
var pipeDown = cc.instantiate(this.pipePrefabs[1]);
pipeDown.getComponent('Pipe').init(1);
pipeDown.x = this.size.width/2 + this.pipeMaxOffsetX;
var pipeGap = Math.floor(math.random()*(this.pipeMaxGap-this.pipeMinGap))+this.pipeMinGap;
this.pipesNode.addChild(pipeUp);
this.pipesNode.addChild(pipeDown);
this.pipes.push(pipeUp);
this.pipes.push(pipeDown);
},