Cocos Creator学习笔记

API文档地址:

    https://docs.cocos.com/creator/3.1/api/zh/

输出日志到控制台:

    cc.log("22222")

动态创建Sprite:

var node = new cc.Node('Sprite');
var sp = node.addComponent(cc.Sprite);
sp.spriteFrame = this.bk;
node.parent = this.node;

动态创建单色Sprite:

    addSprite: function(x, y, width, Height, R, G, B){
        // 函数体
        cc.log("addSprite")
        var node = new cc.Node('Sprite');
        var sp = node.addComponent(cc.Sprite);
        var spriteFrame = new cc.SpriteFrame();
        var texture = new cc.Texture2D();
        node.setPosition(x, y);

        texture.initWithData(new Uint8Array([R, G, B]), cc.Texture2D.PixelFormat.RGB888, 1, 1, cc.winSize);
        spriteFrame.setTexture(texture);
        spriteFrame.setRect(cc.rect(0, 0, width, Height));
        sp.spriteFrame = spriteFrame;
        node.parent = this.node;

        return node
    }

鼠标(触摸)响应:

    onLoad: function () {
        this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
        this.node.on(cc.Node.EventType.MOUSE_UP, this.onMouseUp, this);
        this.node.on(cc.Node.EventType.MOUSE_MOVE, this.onMouseMove, this);
    },

    onDestroy: function () {
        this.node.off(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
        this.node.off(cc.Node.EventType.MOUSE_UP, this.onMouseUp, this);
        this.node.off(cc.Node.EventType.MOUSE_MOVE, this.onMouseMove, this);
    },

    onMouseDown: function (event) {
        cc.log("onMouseDown")
    },

    onMouseUp: function (event) {
        cc.log("onMouseUp")
    },

    onMouseMove: function (event) {
        cc.log("onMouseMove")
    },   

获取鼠标按键和坐标

        onMouseDown: function (event) {
            let mouseType = event.getButton();
            if (mouseType === cc.Event.EventMouse.BUTTON_LEFT) {
                // 鼠标左键按下
                cc.log("onMouseDown")
                let mousePoint = event.getLocation();
                this.oldPoint = mousePoint
                this.moveing = true
            } 
        },

抠图、截屏:

start: function () {

    let texture = new cc.RenderTexture(cc.visibleRect.width, cc.visibleRect.height, cc.Texture2D.PIXEL_FORMAT_RGBA8888);
    texture.initWithSize(cc.visibleRect.width, cc.visibleRect.height, cc.gfx.RB_FMT_D24S8);
    this.camera.targetTexture = texture;
    this.texture = texture;
},

capture () {
	let width = this.texture.width;
	let height = this.texture.height;

	let canvas = document.createElement('canvas');
	let ctx = canvas.getContext('2d');
	canvas.width = width;
	canvas.height = height;

	this.camera.render();
	let data = this.texture.readPixels();
	
	let rowBytes = width * 4;
	for (let row = 0; row < height; row++) {
		let srow = height - 1 - row;
		let imageData = ctx.createImageData(width, 4);
		let start = srow*width*4;
		for (let i = 0; i < rowBytes; i++) {
			imageData.data[i] = data[start+i];
		}
		ctx.putImageData(imageData, 0, row);
	}

	var dataURL = canvas.toDataURL("image/bmp");
	var img = document.createElement("img");
	img.src = dataURL;
	return img;
},

onBtn: function(send, xxx){    
	cc.log("onBtn")

	var img = this.capture();
	
	let texture = new cc.Texture2D();
	texture.initWithElement(img);
	
	let spriteFrame = new cc.SpriteFrame();
	spriteFrame.setTexture(texture);

	this.bk.spriteFrame = spriteFrame;
}

创建纹理:

    addPic: function(x,y,width, Height, R, G, B){
        // 函数体
        cc.log("addCell")
        var imgWidth = 200;
        var imgHeight = 200;
        var count = imgWidth * imgHeight * 4;
        var imgData =new Uint8Array(count);
        for (var j = 0; j < count; j += 4) {
            imgData[j] =  this.random(0, 255);
            imgData[j + 1] =  this.random(0, 255);
            imgData[j + 2] =  this.random(0, 255);
            imgData[j + 3] = 255;
        }
 
        var myTexture2D =new cc.Texture2D();
        myTexture2D.initWithData(imgData, cc.Texture2D.PixelFormat.RGBA8888, imgWidth, imgHeight);
 
        var node =new cc.Node('测试图片');
        node.width = imgWidth;
        node.height = imgHeight;
        //node.parent = Main.node;
         
        var sprite = node.addComponent(cc.Sprite);
        sprite.spriteFrame =new cc.SpriteFrame();
        sprite.spriteFrame.setTexture(myTexture2D)
        sprite.spriteFrame.setRect(cc.rect(0, 0, imgWidth, imgHeight));
        node.parent = this.rootCell;

        this.myTexture2D = myTexture2D;

        return node
    },

随机数:

    random : function (lower, upper) {
        return Math.floor(Math.random() * (upper - lower+1)) + lower;
        
        },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hucailai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值