cocos creator 画矩形,怎么一直报错?

参考了组件http://docs.cocos.com/creator/manual/zh/render/graphics/rect.html

我建了一个脚本game.js,绑定canvas
然后在onload方法里面

var ctx = this.getComponent(cc.Graphics);
ctx.rect(20,20,150,100);
ctx.stroke();

一直提示 错误
Uncaught TypeError: Cannot read property ‘rect’ of null

请问应该怎么改呢

說明ctx是空的。

应该是你canvas 上没有 Graphics组件吧。

你检查下 需要把脚本game.js 放在 Graphics组件所在节点

应该是 this.node.getComponent(cc.Graphics)吧

1赞

确实是这样,非常感谢