[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 写过几个的网站.但 ...
随机推荐
- 防止apache下面直接输入目录访问文件
有些项目链接会暴露服务器上面的文件地址,如何访问文件被访问呢 方法一: 在项目入口文件下面新加一个.htaccess文件(apache开启重写模式才会加载这个文件,否则这个文件配置不会生效) 文件中加 ...
- PAT (Basic Level) Practise:1010. 一元多项式求导
[题目链接] 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. ...
- CheckBoxList 用法
<asp:CheckBoxList ID="cblqf" ForeColor="#4d6fc8" runat="server" Rep ...
- kernel/Makefile
## Makefile for the linux kernel.## Note! Dependencies are done automagically by 'make dep', which a ...
- POJ 1094 (TopoSort)
http://poj.org/problem?id=1094 题意:该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序 ...
- Streaming replication slots in PostgreSQL 9.4
Streaming replication slots are a pending feature in PostgreSQL 9.4, as part of the logical changese ...
- ubuntu12.04+proftpd1.3.4a的系统用户+虚拟用户权限应用实践
目录: 一.什么是Proftpd? 二.Proftpd的官方网站在哪里? 三.在哪里下载? 四.如何安装? 1)系统用户的配置+权限控制 2)虚拟用户的配置+权限控制 一.什么是Proftpd? ...
- [java] java解析txt文件
/** * 读取txt文件内容封装为map返回 * @param filePath * @return */ public static String readTxt(String filePath) ...
- SQL Server 2012 配置数据库邮件
发送和接受邮箱不能用QQ邮箱,可以用163网易邮箱,同时要求要发送邮件的计算机能上外网 查看163网易邮箱的发送和接收服务器的方法如下 在数据库的管理中,右击数据库邮件,选择配置数据库邮件 出现对话框 ...
- [DFNews] GetData也出取证软件了
从事计算机取证的应该都听说过MIP(Mount Image Pro).VFC仿真和Recover My Files,上述三个应用比较广泛的软件都是GetData公司的产品.GetData现在也推出了自 ...