引擎版本 cocos2d-js-3.0-rc2 只有这个版本合并了颜色设置
cc.Sprite 扩展了一个setBorder(边框效果) 方法
/**
* 给node 添加border
* @param cr r
* @param cg g
* @param cb b
* @param ca a
* @param r 边框宽度
* @param f 边框精细度 0 < f < r; default = 1
* @returns {cc.RenderTexture}
*/
cc.Sprite.prototype.setBorder = function(cr, cg, cb, ca , r, f){
var _dfc = cc.color.WHITE;
r = r || (arguments.length == 2 ? cg : 5);
f = f || 1;//精细度
switch (arguments.length){
case 0: cr = _dfc.r; cg = _dfc.g; cb = _dfc.b; ca = _dfc.a; break;
case 1:
case 2: cg = cr.g; cb = cr.b; ca = cr.a; cr = cr.r; break;
case 3: ca = _dfc.a; break;
}
var that = this,
x = that.x, y = that.y,
w = that.width, h = that.height,
dc = cc.size(w + r, h + r);
that.removeBorder();
var mTxt = new cc.RenderTexture(dc.width, dc.height),
cx = 0, ox = dc.width / 2, oy = dc.height / 2;
var visitSprite = function(xx, yy){
if(cc.sys.isNative){
var spr = new cc.Sprite(that.getTexture());
spr.attr({x : xx, y : yy, width : w, height : h});
spr.visit();
} else {
that.setPosition(xx, yy);
that.visit();
}
};
mTxt.beginWithClear(0, 0, 0, 0);
for(var rd = 0; rd <= r; rd += f){
cx = Math.sqrt(Math.pow(r, 2) - Math.pow(rd, 2));
visitSprite(ox + rd, oy + cx);
visitSprite(ox - rd, oy + cx);
visitSprite(ox - rd, oy - cx);
visitSprite(ox + rd, oy - cx);
}
mTxt.end();
that.setPosition(x, y);
var nTxt = new cc.RenderTexture(dc.width, dc.height);
nTxt.setPosition(w / 2, h / 2);
mTxt.setPosition(ox, oy);
mTxt.getSprite().setBlendFunc(cc.ZERO, cc.SRC_ALPHA);
nTxt.beginWithClear(cr, cg, cb, ca);
mTxt.visit();
nTxt.end();
that.addChild(that.borderTex = nTxt, -1);
return nTxt;
};
cc.Sprite.prototype.removeBorder = function(){
this.borderTex && this.borderTex.removeFromParent(true);
};
```
使用代码
var testLayer = cc.Layer.extend({
ctor : function(){
var spr = new cc.Sprite("xxxxxx.png");//图片随便
var sprBorder = spr.setBorder();//默认边框
spr.attr({x : cc.winSize.width / 2, y : cc.winSize.height / 2});
this.addChild(spr);
setTimeout(function(){
//5秒钟后改变边框颜色
sprBorder.setColor(cc.color.BLACK);//改变后无效果
sprBorder.getSprite().setColor(cc.color.BLACK);//还是无效果
}, 5000);
}
});
```
效果如下. 白色边框是初始化的边框. 后改变颜色无效果





