cocos2d js-如何删除精灵【原创】

栏目: 编程语言 · IOS · 发布时间: 9年前

内容简介:cocos2d js-如何删除精灵【原创】

最近在项目中遇到一个很棘手的问题,如何在非当前场景中删除精灵。大家都知道,在cocos2d js 中创建精灵、添加精灵是很常见的,删除精灵也是很常见的,删除精灵主要有两种情况:1.在当前方法中删除精灵;2.在非当前场景、当前方法中删除精灵。我研究之后对解决办法整理了下,下面介绍这两种情况如何删除精灵。

第一种情况很简单,怎么添加精灵的就如何删除精灵:

parent.removeChild(child);

第二种情况,删除精灵:

for(var i=0;i<3;i++){

var left_order_bg = new cc.MenuItemImage(
res.goods_bg,
res.goods_bg,
function () {
this.hidden();
GoodsList[i].actionScene();
},
this);
left_order_bg.setScale(0.7);
left_order_bg.setPosition(cc.p(0,-150*i*0.8));
var menu_left_box = new cc.Menu(left_order_bg);
menu_left_box.attr({
x:85,
y:325
})

// 设置标签
menu_left_box.setTag("leftListMenu");

var GoodsName = new cc.LabelTTF(getHotGoodsList_name[i],"Aril",12);
GoodsName.attr({
x:left_order_bg.getContentSize().width/2,
y:40,
color:(51,51,51,125)
});
left_order_bg.addChild(GoodsName);

var GoodImg = new cc.Sprite(getHotGoodsList_img[i]);
GoodImg.attr({
x:left_order_bg.getContentSize().width/2,
y:100,
scale:0.9
});

var GoodsPrice = new cc.LabelTTF(getHotGoodsList_price[i],"Aril",12);
GoodsPrice.attr({
x:left_order_bg.getContentSize().width/2,
y:20,
color:(51,51,51,125)
})

left_order_bg.addChild(GoodImg);
left_order_bg.addChild(GoodsPrice);
_goodsBox.addChild(menu_left_box);
}

hotCallback:function(){
// 删除精灵
_goodsBox.removeChildByTag("leftListMenu");

}

 

第二种情况删除精灵是在添加精灵的时候通过给该精灵添加标签:

// 设置标签
menu_left_box.setTag("leftListMenu");

然后在hotCallback()方法中删除该精灵:

// 删除精灵
_goodsBox.removeChildByTag("leftListMenu");

由于是自己总结的方法,不一定是最好的解决方法,希望能帮助大家解决问题。

本文永久地址:http://blog.it985.com/20614.html

本文出自IT985博客 ,转载时请注明出处及相应链接。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Pro HTML5 and CSS3 Design Patterns

Pro HTML5 and CSS3 Design Patterns

Michael Bowers / Apress / 2011-11-15 / GBP 35.50

Pro HTML5 and CSS3 Design Patterns is a reference book and a cookbook on how to style web pages using CSS3 and HTML5. It contains 350 ready--to--use patterns (CSS3 and HTML5 code snippets) that you ca......一起来看看 《Pro HTML5 and CSS3 Design Patterns》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具