cocos creator 2.4.2 一些便捷封装

1.在指定位置绘制纯色块

上个项目中需要频繁的绘制纯色背景或半透明黑幕,懒得每次手拖资源:

/**
     * 在指定pos绘制一个纯色块
     * @param pos 注意处理层级关系
     * @param color 
     * @param size 
     * @param par 
     */
    public static drawPureColor(pos: cc.Vec2 = cc.v2(0, 0), color: cc.Vec3 = cc.v3(255, 0, 0), size: cc.Vec2 = cc.v2(20, 20), par: cc.Node | cc.Scene = cc.director.getScene()): cc.Node {
        let texture: cc.Texture2D = new cc.Texture2D();
        let spf: cc.SpriteFrame = new cc.SpriteFrame();
        texture.initWithData(new Uint8Array([color.x, color.y, color.z]), cc.Texture2D.PixelFormat.RGB888, 1, 1);
        spf.setTexture(texture);
        spf.setRect(cc.rect(0, 0, size.x, size.y));
        let node: cc.Node = new cc.Node();
        node.addComponent(cc.Sprite).spriteFrame = spf;
        node.setPosition(pos);
        par.addChild(node);
        return node;
    }

2.截屏

做一个假的转场效果时用到的一个截屏方法

/**
     * 截屏
     * @param mainCameraPos worldPosition of the Main Camera
     */
    public static getScreenCaptureTexture(mainCameraPos: cc.Vec2): cc.RenderTexture {
        let node: cc.Node = new cc.Node();
        node.parent = cc.find("Canvas");
        node.setPosition(node.parent.convertToNodeSpaceAR(mainCameraPos));
        let camera: cc.Camera = node.addComponent(cc.Camera);
        camera.cullingMask = 0xffffffff;
        camera.depth = 2;
        camera.alignWithScreen = true;
        let texture: cc.RenderTexture = new cc.RenderTexture();
        // 如果截图内容中不包含 Mask 组件,可以不用传递第三个参数
        texture.initWithSize(750, G_.wholeViewHeight, cc.RenderTexture.DepthStencilFormat.RB_FMT_S8);
        camera.targetTexture = texture;
        // 渲染一次摄像机,即更新一次内容到 RenderTexture 中
        camera.render();
        node.removeFromParent(true);
        node.destroy();
        return texture;
    }

3.获取实际画布尺寸(存疑

 这一点可能表述不正确,上个项目中实现了一个流体效果(抄自白玉无冰)然后其画布需要同时勾选FillWidth和FillHeight(否则碰撞盒和渲染精灵会“分离”)

该方法是模拟FillWidth的情况下获取画布的实际尺寸(设计尺寸750*1334)

/**
     * 获取实际的屏幕宽高
     * 不一定等于画布尺寸
     */
    public static get wholeViewHeight(): number {
        let frameSize: cc.Size = cc.view.getFrameSize();
        return 750 * frameSize.height / frameSize.width;    //    竖屏,模拟FillWidth
    }

4.置灰

rt,使指定的sprite和label灰显,使用的是内建的材质,置于分包中加载后使用(创建builtin材质相关的方法在2.4.2中本人使用会报错,,未解决,

/**
     * 置灰
     * @param node 
     * @param bGrey 
     * @param bRecursive 是否对子节点递归
     */
    public static setNodeGrey(node: cc.Node, bGrey: boolean = true, bRecursive: boolean = true) {
        let bundle: cc.AssetManager.Bundle = BundleMgr.getBundle_csryw("subRes");
        bundle && bundle.load(bGrey ? g_MaterialUrl.Grey : g_MaterialUrl.Normal, cc.Material, (err, mat: cc.Material) => {
            if (err) {
                cc.error(err);
                return;
            }
            let fn: Function = null;
            fn = (n: cc.Node) => {
                if (!n) return;
                let sp: cc.Sprite = n.getComponent(cc.Sprite);
                sp && sp.setMaterial(0, mat);
                let lb: cc.Label = n.getComponent(cc.Label);
                lb && lb.setMaterial(0, mat);
                if (bRecursive) {
                    n.children.forEach((child: cc.Node) => {
                        fn(child);
                    });
                }
            };
            fn(node);
        });
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值