[COCOS2D-JS]CC. Rendertexture several uses (digital picture, scratch effect)

Source: Internet
Author: User
Tags addchild

"Turn" http://blog.csdn.net/realcrazysun1/article/details/42393629

This article is based on the COCOS2D-JS 3.0 version engine development

Rendertexture usage 1: Digital pictures

Use this image to achieve any number

// Digital Picture Wizard
varPicturenumber =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(){        varInumcount = ( This. m_number+ ""). Length;//get the number of characters        varStsize = This. M_numbertexture.getcontentsize ();//gets the texture size, requires that each number in the texture is equal to the width, and is arranged according to 0123456789                varInumwidth = parseint (STSIZE.WIDTH/10);//the width of each number in the texture        varInumheight = parseint (stsize.height);//the height of each number in the texture        varPRT =NewCc. Rendertexture (Inumwidth * inumcount, inumheight);//create a rendered texture object and number to determine the widthPrt.begin ();  for(vari = 0; i < Inumcount; i++)        {            varPsprite =NewCc. Sprite ();//Create a Sprite object for drawing numbersPsprite.setanchorpoint (0, 0); Psprite.settexture ( This. m_numbertexture); varInumber = ( This. m_number+ "") [i]; //sets the texture area to display the number, which refers to the area in the paramtexture of the parameter            varStrect =NewCc.rect (Inumber * inumwidth, 0, Inumwidth, inumheight); Psprite.settexturerect (Strect,false, Cc.size (Strect.width, strect.height)); Psprite.setposition (i* inumwidth, 0);//calculate the offset position of the displayPsprite.visit ();//rendering into PRT} prt.end (); //get the resulting texture         This. SetTexture (Prt.getsprite (). GetTexture ()); //set what is displayed        varStrect =NewCc.rect (0, 0, Inumwidth *Inumcount, inumheight);  This. Settexturerect (Strect,false, Cc.size (Strect.width, strect.height)); //By default, the texture obtained by ccrendertexture is inverted, which needs to be flipped         This. Setflippedy (true); }, Setnumber:function(paramnumber) { This. M_number =Paramnumber; }, GetNumber:function(){        return  This. M_number; }, Setnumbertexture:function(paramtexture) { This. M_numbertexture =paramtexture; }});

How to use:

var New Picturenumber ();        Pnum.buildnumber (1234567, "Res/number.png");        Pnum.setposition (a);        Pnum.setanchorpoint (0, 0);

Rendertexture Usage 2: Scratch effect

The main code is as follows:

varHelloworldlayer =cc. Layer.extend ({sprite:NULL, Peraser:NULL, Prtex:NULL, ctor:function () {        //////////////////////////////        //1. Super init First         This. _super (); varSize =cc.winsize; //add a "close" icon to exit the progress. It's an Autorelease object        varCloseitem =Newcc. 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        }); varmenu =Newcc.        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        varHellolabel =NewCc. Labelttf ("Hello World", "Arial", 38); //position the label on the center of thehellolabel.x = SIZE.WIDTH/2; Hellolabel.y= SIZE.HEIGHT/2; //Add the label as a child to this layer         This. AddChild (Hellolabel, 5); //Hello World background Image         This. Sprite =Newcc. Sprite (Res.        Helloworld_png);  This. sprite.attr ({x:size.width/2, Y:size.height/2,        });  This. AddChild ( This. Sprite, 0); //Rubber Eraser         This. Peraser =Newcc.           Drawnode ();  This. Peraser.drawdot (CC.P (0, 0), Cc.color (255, 255, 255, 0));  This. Peraser.retain (); //using Prtex to implement Eraser         This. Prtex =Newcc.        Rendertexture (Size.width,size.height);  This. Prtex.setposition (SIZE.WIDTH/2, SIZE.HEIGHT/2);  This. AddChild ( This. Prtex, 10); //load a picture waiting to be erased        varPBg =Newcc.        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"); vartarget =Event.getcurrenttarget (); return true; }, Ontouchmoved:function(Touch, event) {vartarget =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 (); }    });

The results are as follows:

[COCOS2D-JS]CC. Rendertexture several uses (digital picture, scratch effect)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.