cocos creator新手求助

weixin_41877189 2018-03-23 11:16:44
最近刚刚开始学习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);
},
...全文
1030 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zack0Fair 2018-05-29
  • 打赏
  • 举报
回复
难道不是应该 新创建预制对象 把预制对象 归属于那个父节点 表明吗?
crazyProg 2018-05-09
  • 打赏
  • 举报
回复
你自己加断点看看啊,或者 console.log 看看你pipeUp.getComponent('Pipe') 是否获取成功了,如果没成功,再看看你的creator是不是弄错了
zerozerg2006 2018-05-02
  • 打赏
  • 举报
回复
很明显的问题,pipeUp.getComponent('Pipe')得到的东西是一个null。 那就很明显了,你确认是否可以通过"Pipe"这个key来拿到你需要拿的东西,我猜你并没有对你的那些控件设置名字

721

社区成员

发帖
与我相关
我的任务
社区描述
Cocos2d-x相关内容讨论专区
社区管理员
  • Cocos2d-x
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧