Creator 版本号:1.5.0 beta3
运行时目标平台:Web
手机浏览器平台:浏览器
在cocos2dx中,我们有一种换肤需求
有两张大小一样的图片A B.
基于图片A我们创建了很多的精灵,这些精灵分别显示了图片A上的不同区域。
在换肤的时候,我们需要显示图片B上的内容,同时还需要保证当前精灵显示的图片区域不会发生变化
所以在cocos2dx中我们是这么实现的
有一个成员的变量
m_pTexture
所有的精灵都是基于这个Texure创建出来的
auto pSprite1 = Sprite::createWithTexture(m_pTexture, Rect(0,0,100,100));
…
auto pSpriteN = Sprite::createWithTexture(m_pTexture, Rect(N,N,100,100));
当我们需要换肤的时候,我们只需要修改m_pTexture对应的图片,就可以完成换肤功能
m_pTexture->initWithImage(pImage);
此时的pImage已经载入了B图了
这样所有的精灵都会显示图片B上的元素,这样就完成了一套换肤功能
但是
我发现同样的方法在cocos creator似乎就没法生效了
在properties中,我预先定义了精灵
testSprite1: cc.Sprite
testSprite2: cc.Sprite
同样的,依然有一个成员变量用来保存Texture
testTexture: cc.Texture2D
在onLoad中
// creator中,精灵默认的spriteFrame被我删掉了,所以此处new一个
this.testSprite1.spriteFrame = new cc.SpriteFrame()
this.testSprite2.spriteFrame = new cc.SpriteFrame();
this.testSprite1.spriteFrame.setTexture(this.cardTexure);
this.testSprite2.spriteFrame.setTexture(this.cardTexure);
this.testSprite1.spriteFrame.setRect(cc.rect(0,0,90,120));
this.testSprite2.spriteFrame.setRect(cc.rect(0,240,90,120));
在某个按钮点击事件中,载入新的图片
var imageURL = cc.url.raw('resources/123.png');
this.cardTexure = cc.textureCache.addImage(imageURL);
很显然,那么这里的改变,并不会影响到testSprite中的texture
那么我想问下,在cocos中如何实现我们的这个需求?