【转】http://blog.csdn.net/realcrazysun1/article/details/42393629

本文基于cocos2d-js 3.0版本引擎开发

RenderTexture用法1:数字图片

通过这张图片实现任意数字

//数字图片精灵
var PictureNumber = cc.Sprite.extend({
m_Number:null,
m_NumberTexture:null,
ctor:function(){
this._super(); },
buildNumber:function(paramNumber, paramTexture)
{
this.setNumber(paramNumber);
this.setNumberTexture(cc.textureCache.addImage(paramTexture));
return this.build();
},
build:function(){ var iNumCount = (this.m_Number+"").length; //取得字符个数
var stSize = this.m_NumberTexture.getContentSize(); //取得纹理大小,要求纹理中每个数字都是等宽等高,并依照0123456789排列 var iNumWidth = parseInt( stSize.width / 10); //纹理中每个数字的宽度
var iNumHeight = parseInt( stSize.height); //纹理中每个数字的高度 var pRT = new cc.RenderTexture(iNumWidth * iNumCount, iNumHeight); //创建渲染纹理对象,并数字确定宽度 pRT.begin();
for (var i = 0; i < iNumCount; i++)
{
var pSprite = new cc.Sprite(); //创建精灵对象,用于绘制数字
pSprite.setAnchorPoint(0, 0);
pSprite.setTexture(this.m_NumberTexture);
var iNumber = (this.m_Number+"")[i];
//设置要显示数字的纹理区域,这个区域是指参数中paramTexture中区域
var stRect = new cc.rect(iNumber * iNumWidth, 0, iNumWidth, iNumHeight);
pSprite.setTextureRect(stRect, false, cc.size(stRect.width, stRect.height));
pSprite.setPosition(i * iNumWidth, 0); //计算显示的偏移位置
pSprite.visit(); //渲染到pRT中
}
pRT.end();
//取得生成的纹理
this.setTexture(pRT.getSprite().getTexture());
//设置显示的内容
var stRect = new cc.rect(0, 0, iNumWidth * iNumCount, iNumHeight);
this.setTextureRect(stRect, false, cc.size(stRect.width, stRect.height));
//默认的情况下,通过CCRenderTexture得到的纹理是倒立的,这里需要做一下翻转
this.setFlippedY(true);
}, setNumber:function(paramNumber){
this.m_Number = paramNumber;
},
getNumber:function(){
return this.m_Number;
},
setNumberTexture:function(paramTexture)
{
this.m_NumberTexture = paramTexture;
}
});

 

使用方法:

var pNum = new PictureNumber();
pNum.buildNumber(1234567, "res/number.png");
pNum.setPosition(200, 200);
pNum.setAnchorPoint(0, 0);

RenderTexture用法2:刮刮乐效果

主要代码如下:

var HelloWorldLayer = cc.Layer.extend({
sprite:null,
pEraser:null,
pRTex:null, ctor:function () {
//////////////////////////////
// 1. super init first
this._super(); var size = cc.winSize; // add a "close" icon to exit the progress. it's an autorelease object
var closeItem = new cc.MenuItemImage(
res.CloseNormal_png,
res.CloseSelected_png,
function () {
cc.log("Menu is clicked!");
}, this);
closeItem.attr({
x: size.width - 20,
y: 20,
anchorX: 0.5,
anchorY: 0.5
}); var menu = new cc.Menu(closeItem);
menu.x = 0;
menu.y = 0;
this.addChild(menu, 1); /////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
var helloLabel = new cc.LabelTTF("Hello World", "Arial", 38);
// position the label on the center of the screen
helloLabel.x = size.width / 2;
helloLabel.y = size.height / 2;
// add the label as a child to this layer
this.addChild(helloLabel, 5); // hello world 背景图片
this.sprite = new cc.Sprite(res.HelloWorld_png);
this.sprite.attr({
x: size.width / 2,
y: size.height / 2,
});
this.addChild(this.sprite, 0); //橡皮擦
this.pEraser = new cc.DrawNode();
this.pEraser.drawDot(cc.p(0, 0), 20, cc.color(255, 255, 255, 0));
this.pEraser.retain(); //通过pRTex实现橡皮擦
this.pRTex = new cc.RenderTexture(size.width,size.height);
this.pRTex.setPosition(size.width/2, size.height/2);
this.addChild(this.pRTex, 10); //加载等待被擦除的图片
var pBg = new cc.Sprite(res.dirt_png);
pBg.setPosition(size.width/2, size.height/2);
this.pRTex.begin();
pBg.visit();
this.pRTex.end(); cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan:function(touches, event){
cc.log("start");
var target = event.getCurrentTarget();
return true;
},
onTouchMoved:function (touch, event) {
var target = event.getCurrentTarget();
target.pEraser.setPosition(touch.getLocation());
target.eraseByBlend();
}
}, this); return true;
}, eraseByBlend :function()
{
this.pEraser.setBlendFunc(cc.GL_ONE_MINUS_SRC_ALPHA, cc.ZERO);
this.pRTex.begin();
this.pEraser.visit();
this.pRTex.end();
} });

运行效果如下:

 

[cocos2d-js]cc.RenderTexture几种用法(数字图片、刮刮乐效果)的更多相关文章

  1. Js闭包常见三种用法

        Js闭包特性源于内部函数可以将外部函数的活动对象保存在自己的作用域链上,所以使内部函数的可以将外部函数的活动对象占为己有,可以在外部函数销毁时依然存有外部函数内的活动对象内容,这样做的好处是可 ...

  2. JS函数的几种用法

    1.正常使用:

  3. js的this几种用法

    1.普通的函数调用 此时指的是全局对象 function aaa(){ this.x=1;}aaa();alert(x) 2.对象内的方法this调用 此时指的是上一级对象 var aaa={ zz: ...

  4. js 函数arguments一种用法

    无意改同事的代码发现的 function toggle(){ var _arguments=arguments; var count=0; $("#more").click(fun ...

  5. jQuery演示8种不同的图片遮罩层动画效果

    效果预览 下载地址 jQuery插件大全 实例代码 <div class="container"> <h1>jQuery图标和文章动画效果</h1&g ...

  6. js正则表达式中的问号几种用法小结

    这篇文章主要介绍了js正则表达式中的问号几种用法,比如+?,*?,{2,3}?可以停止匹配的贪婪模式,感兴趣的朋友可以参考下 在表示重复的字符后面加问号,比如+?,*?,{2,3}?可以停止匹配的贪婪 ...

  7. js中哈希表的几种用法总结

    本篇文章只要是对js中哈希表的几种用法进行了总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 1. <html> <head> <script type=" ...

  8. JS里设定延时:js中SetInterval与setTimeout用法

     js中SetInterval与setTimeout用法 JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操 ...

  9. js中apply,call的用法

    最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web的项目,然后在腾讯实习的时候用 js 写过一些奇怪的程序,自己也用 js 写过几个的网站.但 ...

随机推荐

  1. Python12期培训班-day1-三级菜单代码分享

    #!/usr/bin/env python3 import sys import os zonecode = { '广东省': {'广州市':['越秀区','海珠区','荔湾区','天河区'], '深 ...

  2. nat转换

    实验目的: (1)     了解nat转换 (2)     了解nat转换配置命令 (3)     了解哪些是私有ip地址哪些不是私有ip地址 实验工具: 华为eNSP模拟器和Wireshar 实验拓 ...

  3. Codeforces Round #161 (Div. 2)

    A. Beautiful Matrix 即相当于求1到中心位置\((2,2)\)的曼哈顿距离. B. Squares 排序,取倒数第\(k\)个即可. C. Circle of Numbers 固定\ ...

  4. 职责链模式(chain of responsibility Pattern)

    职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理他为止. •Handler: 抽象处理者:定义出一个 ...

  5. ps颜色模式

    HSB(hue.saturation.bright)  基于人眼 RGB 基于光 CMYK 基于色 LAB 基于大自然颜色库(理论)

  6. lsm-tree

    https://www.quora.com/How-does-the-Log-Structured-Merge-Tree-work http://blog.cloudera.com/blog/2012 ...

  7. ls

    -a, –all 列出目录下的所有文件,包括以 . 开头的隐含文件 -h, –human-readable 以容易理解的格式列出文件大小 (例如 1K 234M 2G) -l 除了文件名之外,还将文件 ...

  8. Windows 7 安装 .netfx 4 卡住

    net stop wuauserv rename \windows\SoftwareDistribution SoftwareDistribution_old net start wuauserv

  9. 升级NC6.3

    2014-04-23 江苏建工&用友公司会谈提纲 1,合同规定江苏建工用友NC在实施成功之后三年免服务费(2010年增补了资金管理,如果以2010年作为软件最终实施完成,那么2010-2013 ...

  10. 【uTenux实验】写在开始实验之前

    1.使用的uTenux内核代码:http://www.uloong.cc/cn/download/uTenux_V1.6.00r180.zip 2.uTenux的特性: 1.微内核  2.开放源码.完 ...