I’m using cocos2d-js 3.16 (jsbinding)
First a working example of two sprites :
var sprite1 = cc.Sprite.create(res.objectA_png);
sprite1.setPosition(cc.winSize.width * 0.5, cc.winSize.height * 0.5);
this.addChild(sprite1);
var sprite2 = cc.Sprite.create(res.objectB_png);
sprite2.setPosition(cc.winSize.width * 0.5, cc.winSize.height * 0.5 - 200);
this.addChild(sprite2);
And the result is :
And then replace the texture of Sprite1 with the texture of Sprite2, add the two line of codes:
sprite1.setTexture(sprite2.getTexture());
sprite1.setTextureRect(sprite2.getTextureRect());
And it works fine as well :
And then I created a function to generate a sprite using cc.RenderTexture to capture a given node:
generateSprite = function(target) {
var renderTexture = new cc.RenderTexture(target.width, target.height);
renderTexture.setAutoDraw(false);
renderTexture.setKeepMatrix(true);
renderTexture.beginWithClear(255, 0, 0, 100); // <== ==== color for debug
renderTexture.setVirtualViewport(
cc.p(target.x - target.width / 2, target.y - target.height / 2),
cc.rect(0, 0, cc.winSize.width, cc.winSize.height),
cc.rect(0, 0, cc.director.getWinSizeInPixels().width, cc.director.getWinSizeInPixels().height));
target.visit();
renderTexture.end();
//
var cloneSprite = new cc.Sprite(renderTexture.getSprite().getTexture());
cloneSprite.setFlippedY(true);
return cloneSprite;
};
And then I use this function to generate Sprite2 :
var sprite1 = cc.Sprite.create(res.objectA_png);
sprite1.setPosition(cc.winSize.width * 0.5, cc.winSize.height * 0.5);
this.addChild(sprite1);
var sprite2 = this.generateSprite(sprite1);
sprite2.setPosition(cc.winSize.width * 0.5, cc.winSize.height * 0.5 - 200);
this.addChild(sprite2);
And it still works fine :
And the last part, I tried to replace the texture of Sprite1 using the texture of sprite2 just as I did above:
sprite1.setTexture(sprite2.getTexture());
sprite1.setTextureRect(sprite2.getTextureRect());
But this time, after doing this , I get:
No warning or errors shown in the console… I am really not sure what is going wrong here …
Any suggestion will be appreciated, thanks