Canvas下建立一个空node,然后挂一个脚本。建立一个按钮,绑定drawButton函数。
drawMap函数放到start()和drawButton中完全不会绘制
如果放到update里,过几十秒才绘制。
有木有大佬费心解答下。
@ccclass
export default class Test extends cc.Component {
@property
ctx:cc.Graphics = null;
@property(cc.Sprite)
sprite:cc.Sprite = null;
@property
texture:cc.Texture2D = null;
start () {
// 放按这里,点击后不绘制
// this.drawMap();
}
update(){
// 如果放这里,则等几十秒才能绘制出来
// this.drawMap();
// 下面是为了证明点击按钮后真的创建了控件
let sp = this.node.getComponent(cc.Sprite);
if(sp){
cc.log("node name:", this.node.name);
}
}
drawButton(){
// 放按钮响应里,点击后不绘制
this.drawMap();
}
drawMap() {
let textdata = new Uint8Array(128*128*3);
for(let i = 0; i < 128*128*3; i++){
if(i % 3 == 0){
textdata[i] = 255;
}
// else if(i % 4 == 3){
// textdata[i] = 255;
// }
else{
textdata[i] = 0;
}
}
cc.log("textData:", textdata);
this.texture = new cc.Texture2D();
this.texture.initWithData(textdata, cc.Texture2D.PixelFormat.RGB888, 128,128);
// this.texture.initWithData(textdata, cc.Texture2D.PixelFormat.RGBA8888, 128,128);
this.sprite = this.node.addComponent(cc.Sprite);
this.sprite.spriteFrame = new cc.SpriteFrame(this.texture);
this.node.position = new cc.Vec2(200,200);
}
}