【转】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. 学习笔记:The Log(我所读过的最好的一篇分布式技术文章)

    前言 这是一篇学习笔记. 学习的材料来自Jay Kreps的一篇讲Log的博文. 原文很长,但是我坚持看完了,收获颇多,也深深为Jay哥的技术能力.架构能力和对于分布式系统的理解之深刻所折服.同时也因 ...

  2. 讨论贴:在sp_executesql 中生成的临时表的可见性

    首先创建数据表 IF object_id('TestTable') IS NOT NULL DROP TABLE TestTable GO ,),Info )) GO INSERT TestTable ...

  3. C语言解决八皇后问题

    #include <stdio.h> #include <stdlib.h> /* this code is used to cope with the problem of ...

  4. java中,去除空白的方法

    有时候,我们页面传过来的值,或者做excel导入时填入的值都需要去掉像空格一样的一些特殊字符,下面这个方法可去掉像制表符,换行键,回车,空格或者不在ACSII中 的特殊字符 /** * 去除字符串开始 ...

  5. [NOIP2011] 计算系数(二项式定理)

    题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别为 a ,b ,k , ...

  6. Android——配置文件的保存SharedPreferences进行数据存储

    很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友.对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果 ...

  7. [原创]cocos2d-x研习录-第二阶 概念类之布场层类(CCLayer)

    上面说场景CCScene相当于一个大容器,那么布景层类CCLayer就是大容器里的若干个小容器.每个游戏场景CCScene会有很多层CCLayer,每一层CCLayer负责各自的任务.看一下CCLay ...

  8. Extjs各版本的下载链接

    Extjs的版本繁多,本文收集了Extjs各个版本的下载链接,包括官网和非官网的,以及各种汉化版api,欢迎大家下载分享. Extjs最新版下载链接:http://www.sencha.com/pro ...

  9. android 4.4.2 开发环境

    1.设置环境变量 set "JAVA_HOME=C:\Program Files\Java\jdk1.7.0_75" set "ANT_HOME=D:\tools\and ...

  10. Eclipse *下载

    简单了解,Eclipse是绿色软件,下载下来是个压缩包,只需要解压,加上jdk就可以运行了. 相比MyEclipse而言,它是免费的,后者是收费的.各有侧重吧 有很多人用Eclipse,也有很多人用M ...