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,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();
}
});运行效果如下: