rt,我想请问下,如果已经创建好了图片精灵,当点击图片精灵后需要将改图片换成另外一张图片,请问下如何替换?重新创建一个精灵覆盖我想应该不用这么麻烦吧
var newtexture = cc.textureCache.addImage(newimageUrl)
sprite.setTexture(newtexture)
谢谢了,我刚才试了下,调用精灵的setTexture()方法,参数直接写图片资源就行了。
是的 setTexture方法里实现了cc.textureCache.addImage
你好,我想问下,图片在更换之前缩放过,但是更换后的图片不会和之前一样缩放,这怎么办好呢
没这个需求,不知道
大概给你几个方案
1、如果知道缩放比例,直接sp.scale = 你知道的值就行
2、如果知道缩放的w,h,但不知道目标图片的w.h
2.1
var self = this;
if(imgUrl){
cc.loader.loadImg(imgUrl, { }, function(err, img){
var texture2d = new cc.Texture2D();
texture2d.initWithElement(img);
texture2d.handleLoadedTexture();
var sp = new cc.Sprite(texture2d);
self.addChild(sp);
sp.x = winSize.width / 2;
sp.y = winSize.height / 2;
//sp.scale = …
});
}2.2 据说3.0RC2支持 <pre class="brush:js; toolbar: true; auto-links: false;">cc.textureCache.addImage(imageurl, function(texture){ if(texture) //todo something })2.3可以重写setTexture方法,增加俩参数scaleW,scaleH,如下
_p.setTexture = function (texture,scaleW,scaleH) { var _t = this; if(texture && (typeof(texture) === "string")){ texture = cc.textureCache.addImage(texture); _t.setTexture(texture); //TODO var size = texture.getContentSize(); _t.setTextureRect(cc.rect(0,0, size.width, size.height)); _t.scaleX = scaleW / size.width; _t.scaleY = scaleH / size.height; return; } // CCSprite: setTexture doesn't work when the sprite is rendered using a CCSpriteSheet cc.assert(!texture || texture instanceof cc.Texture2D, cc._LogInfos.CCSpriteBatchNode_setTexture); if (_t._texture != texture) { if (texture && texture.getHtmlElementObj() instanceof HTMLImageElement) { _t._originalTexture = texture; } _t._texture = texture; } }; ```
你是想像动画一样切换纹理吗?
你可以用SpriteFrame来管理动画帧,
然后调用Sprite的setSpriteFrame来切换。 setTexture是直接切换纹理,如果传入的是cc.Texture2D对象,将不会重新设置textureRect。
:867:大神
多谢了,用sp.scale就可以了,我之前是因为比例用的是替换前的图片比例,所以一直不合适。
可以直接在当前的sprite调用一下initWithTexture(texture)这样就会重新计算图片大小了。
— Begin quote from ____
引用第9楼wzm618于2014-08-27 21:37发表的 :
可以直接在当前的sprite调用一下initWithTexture(texture)这样就会重新计算图片大小了。 http://www.cocoachina.com/bbs/job.php?action=topost&tid=225079&pid=1040305
— End quote
initWithTexture 这个不错
想问一下,如果图片被压缩成PVR格式,应该怎么使用setTexture方法呢