[cocos2d-js]cc.RenderTexture几种用法(数字图片、刮刮乐效果)

前端之家收集整理的这篇文章主要介绍了[cocos2d-js]cc.RenderTexture几种用法(数字图片、刮刮乐效果)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

RenderTexture用法1:数字图片

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

  1. //数字图片精灵
  1. var PictureNumber = cc.Sprite.extend({
  2. m_Number:null,m_NumberTexture:null,ctor:function(){
  3. this._super();
  4. },buildNumber:function(paramNumber,paramTexture)
  5. {
  6. this.setNumber(paramNumber);
  7. this.setNumberTexture(cc.textureCache.addImage(paramTexture));
  8. return this.build();
  9. },build:function(){
  10.  
  11. var iNumCount = (this.m_Number+"").length; //取得字符个数
  12. var stSize = this.m_NumberTexture.getContentSize(); //取得纹理大小,要求纹理中每个数字都是等宽等高,并依照0123456789排列
  13. var iNumWidth = parseInt( stSize.width / 10); //纹理中每个数字的宽度
  14. var iNumHeight = parseInt( stSize.height); //纹理中每个数字的高度
  15.  
  16. var pRT = new cc.RenderTexture(iNumWidth * iNumCount,iNumHeight); //创建渲染纹理对象,并数字确定宽度
  17. pRT.begin();
  18. for (var i = 0; i < iNumCount; i++)
  19. {
  20. var pSprite = new cc.Sprite(); //创建精灵对象,用于绘制数字
  21. pSprite.setAnchorPoint(0,0);
  22. pSprite.setTexture(this.m_NumberTexture);
  23. var iNumber = (this.m_Number+"")[i];
  24. //设置要显示数字的纹理区域,这个区域是指参数中paramTexture中区域
  25. var stRect = new cc.rect(iNumber * iNumWidth,iNumWidth,iNumHeight);
  26. pSprite.setTextureRect(stRect,false,cc.size(stRect.width,stRect.height));
  27. pSprite.setPosition(i * iNumWidth,0); //计算显示的偏移位置
  28. pSprite.visit(); //渲染到pRT中
  29. }
  30. pRT.end();
  31. //取得生成的纹理
  32. this.setTexture(pRT.getSprite().getTexture());
  33. //设置显示内容
  34. var stRect = new cc.rect(0,iNumWidth * iNumCount,iNumHeight);
  35. this.setTextureRect(stRect,stRect.height));
  36. //默认的情况下,通过CCRenderTexture得到的纹理是倒立的,这里需要做一下翻转
  37. this.setFlippedY(true);
  38. },setNumber:function(paramNumber){
  39. this.m_Number = paramNumber;
  40. },getNumber:function(){
  41. return this.m_Number;
  42. },setNumberTexture:function(paramTexture)
  43. {
  44. this.m_NumberTexture = paramTexture;
  45. }
  46. });
使用方法
  1. var pNum = new PictureNumber();
  2. pNum.buildNumber(1234567,"res/number.png");
  3. pNum.setPosition(200,200);
  4. pNum.setAnchorPoint(0,0);
运行如下:

RenderTexture用法2:刮刮乐效果

主要代码如下:
  1. var HelloWorldLayer = cc.Layer.extend({
  2. sprite:null,pEraser:null,pRTex:null,ctor:function () {
  3. //////////////////////////////
  4. // 1. super init first
  5. this._super();
  6.  
  7. var size = cc.winSize;
  8.  
  9. // add a "close" icon to exit the progress. it's an autorelease object
  10. var closeItem = new cc.MenuItemImage(
  11. res.CloseNormal_png,res.CloseSelected_png,function () {
  12. cc.log("Menu is clicked!");
  13. },this);
  14. closeItem.attr({
  15. x: size.width - 20,y: 20,anchorX: 0.5,anchorY: 0.5
  16. });
  17.  
  18. var menu = new cc.Menu(closeItem);
  19. menu.x = 0;
  20. menu.y = 0;
  21. this.addChild(menu,1);
  22.  
  23. /////////////////////////////
  24. // 3. add your codes below...
  25. // add a label shows "Hello World"
  26. // create and initialize a label
  27. var helloLabel = new cc.LabelTTF("Hello World","Arial",38);
  28. // position the label on the center of the screen
  29. helloLabel.x = size.width / 2;
  30. helloLabel.y = size.height / 2;
  31. // add the label as a child to this layer
  32. this.addChild(helloLabel,5);
  33.  
  34. // hello world 背景图片
  35. this.sprite = new cc.Sprite(res.HelloWorld_png);
  36. this.sprite.attr({
  37. x: size.width / 2,y: size.height / 2,});
  38. this.addChild(this.sprite,0);
  39. //橡皮擦
  40. this.pEraser = new cc.DrawNode();
  41. this.pEraser.drawDot(cc.p(0,0),20,cc.color(255,255,0));
  42. this.pEraser.retain();
  43. //通过pRTex实现橡皮擦
  44. this.pRTex = new cc.RenderTexture(size.width,size.height);
  45. this.pRTex.setPosition(size.width/2,size.height/2);
  46. this.addChild(this.pRTex,10);
  47. //加载等待被擦除的图片
  48. var pBg = new cc.Sprite(res.dirt_png);
  49. pBg.setPosition(size.width/2,size.height/2);
  50. this.pRTex.begin();
  51. pBg.visit();
  52. this.pRTex.end();
  53.  
  54. cc.eventManager.addListener({
  55. event: cc.EventListener.TOUCH_ONE_BY_ONE,onTouchBegan:function(touches,event){
  56. cc.log("start");
  57. var target = event.getCurrentTarget();
  58. return true;
  59. },onTouchMoved:function (touch,event) {
  60. var target = event.getCurrentTarget();
  61. target.pEraser.setPosition(touch.getLocation());
  62. target.eraseByBlend();
  63. }
  64. },this);
  65. return true;
  66. },eraseByBlend :function()
  67. {
  68. this.pEraser.setBlendFunc(cc.GL_ONE_MINUS_SRC_ALPHA,cc.ZERO);
  69. this.pRTex.begin();
  70. this.pEraser.visit();
  71. this.pRTex.end();
  72. }
  73. });
运行效果如下:



猜你在找的Cocos2d-x相关文章