[cocos2d-js]cc.RenderTexture几种用法(数字图片、刮刮乐效果)
【转】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几种用法(数字图片、刮刮乐效果)的更多相关文章
- Js闭包常见三种用法
Js闭包特性源于内部函数可以将外部函数的活动对象保存在自己的作用域链上,所以使内部函数的可以将外部函数的活动对象占为己有,可以在外部函数销毁时依然存有外部函数内的活动对象内容,这样做的好处是可 ...
- JS函数的几种用法
1.正常使用:
- js的this几种用法
1.普通的函数调用 此时指的是全局对象 function aaa(){ this.x=1;}aaa();alert(x) 2.对象内的方法this调用 此时指的是上一级对象 var aaa={ zz: ...
- js 函数arguments一种用法
无意改同事的代码发现的 function toggle(){ var _arguments=arguments; var count=0; $("#more").click(fun ...
- jQuery演示8种不同的图片遮罩层动画效果
效果预览 下载地址 jQuery插件大全 实例代码 <div class="container"> <h1>jQuery图标和文章动画效果</h1&g ...
- js正则表达式中的问号几种用法小结
这篇文章主要介绍了js正则表达式中的问号几种用法,比如+?,*?,{2,3}?可以停止匹配的贪婪模式,感兴趣的朋友可以参考下 在表示重复的字符后面加问号,比如+?,*?,{2,3}?可以停止匹配的贪婪 ...
- js中哈希表的几种用法总结
本篇文章只要是对js中哈希表的几种用法进行了总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 1. <html> <head> <script type=" ...
- JS里设定延时:js中SetInterval与setTimeout用法
js中SetInterval与setTimeout用法 JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操 ...
- js中apply,call的用法
最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web的项目,然后在腾讯实习的时候用 js 写过一些奇怪的程序,自己也用 js 写过几个的网站.但 ...
随机推荐
- html元素中id和name的区别
可以说几乎每个做过Web开发的人都问过,到底元素的ID和Name有什么区别阿?为什么有了ID还要有Name呢?! 而同样我们也可以得到最classical的答案:ID就像是一个人的身份证号码,而Nam ...
- Java数据结构和算法之递归
四.递归 递归是函数调用自身的一种特殊的编程技术,其应用主要在以下几个方面: 阶乘 在java当中的基本形式是: Public void mothed(int n){//当满足某条件时: Mo ...
- C#编程语言与面向对象——核心
面向对象的核心 (1).封装 封装的类=数据+对此数据所进行的操作(即算法) 封装起外界不必需要知道的东西,指向外界展现可供展示的东西. 小到一个简单的数据结构,大到一个完整的软件子系统.静态的如某软 ...
- Bundle
[[NSBundle mainBundle] pathForResource:@"someFileName" ofType:@"yourFileExtension&quo ...
- UVa 10562看图写树(二叉树遍历)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 数据库之mysql存储程序
什么时候会用到存储过程 1.存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般 SQL 语句每执行一次就编译一次,所以使用存储过程可提高数据库执行速度2.当对数据库进行复杂操作时 ...
- POJO类中的任何布尔类型的变量,都不要加is
POJO类中的任何布尔类型的变量,都不要加is,否则部分框架解析会引起序列化错误. 定义为基本数据类型boolean isSuccess:的属性,它的方法也是isSuccess(),HSF框架在反向解 ...
- elasticsearch单机多实例环境部署
elasticsearch的功能,主要用在搜索领域,这里,我来研究这个,也是项目需要,为公司开发了一款CMS系统,网站上的搜索栏功能,我打算采用elasticsearch来实现. elasticsea ...
- Maven进价:Maven构建错误汇总
问题:The method of type must override asuperclass? annotation:@Override的原因 办法:项目右键->build path-> ...
- python---dict字典
Python 字典 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式 ...