对于纹理的合理使用,可以降低IO的消耗,因此通过这节课后,以后加载精灵,最好都使用TextureCache.addImage();这样下次使用时将直接返回该纹理。引用之前加载的纹理可以减少 GPU 与 cpu 的内存消耗。顺便复习了一下上周学习的内容单点触摸和动作。附上API链接供参考:http://api.cocos.com/cn/de/d33/classcocos2d_1_1_texture_cache.html#details
代码如下:
var HelloWorldLayer = cc.Layer.extend({
sprite:null,
ctor:function () {
this._super();
var size = cc.winSize;
var bg_texture = cc.textureCache.addImage("res/bg.jpg");
var hero1_texture = cc.textureCache.addImage("res/h2.png");
var bg = new cc.Sprite(bg_texture);
this.addChild(bg,0);
bg.setPosition(size.width/2,size.height/2);
var hero = new cc.Sprite(hero1_texture);
hero.setTag(100);
this.addChild(hero,1);
hero.setScale(0.1);
var act1 = new cc.RotateTo(2,180);
var act2 = new cc.FadeTo(2,1);
var act3 = new cc.FadeIn(2);
var act4 = new cc.RotateTo(2,360);
hero.runAction(cc.sequence(act1,act2,act3,act4));
hero.setPosition(200,200);
return true;
},
onEnter:function(){
this._super();
cc.eventManager.addListener({
event:cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches:true,
onTouchBegan:this.touchbegan.bind(this),
onTouchMoved:this.touchmoved,
onTouchEnded:this.touchended,
},this);
},
touchbegan:function(touch,event){
var nowhero = event.getCurrentTarget().getChildByTag(100);
var act = new cc.moveTo(2,touch.getLocationX(),touch.getLocationY());
nowhero.runAction(act);
}
});
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
}
});
最后附上作业链接:
http://www.cocoscvp.com/usercode/2016_04_22/9a5d3f6b39c64a368256ecc5b90124f54625a576/
原文链接:https://www.f2er.com/cocos2dx/339518.html