代码如下:
// called every frame, uncomment this function to activate update callback
update: function (dt) {
// cc.log("update dt=%.2f", dt);
// return;
this.curTime = this.curTime - dt;
if (this.curTime < 0) {
this.curTime = Math.random()*2.0 + 1.0;
// this.curTime = 10000;
// 从预制件中创建实例
var tPrefab = cc.instantiate(this.ball_prefab);
tPrefab.parent = this.node;
// 随机X坐标, 通常是-880~880之间
var randomX = (Math.random()*((this.screenWidth - tPrefab.width * 2) / 2));
if (Math.random() > 0.5) {
randomX = randomX * -1.0;
}
// 创建出来的tPrefab是个cc.Node
// cc.log("balls update tPrefab", tPrefab instanceof cc.Node);
// cc.log("this.curTime:%.2f w:%.2f randomX:%.2f", this.curTime, tPrefab.width, randomX);
tPrefab.setPosition(randomX,475);
var destroyPrefabCallback = function(dt) {
// 居然可以这样处理销毁
// cc.log("destroyPrefab");
if ( cc.isValid(tPrefab) ) {
tPrefab.destroy();
} else {
// 真会跑到这里来的
cc.log("balls update not cc.isValid !!! ");
console.log('balls update not cc.isValid !!! ');
}
}
// 定时销毁
this.scheduleOnce(destroyPrefabCallback,10.0);
}// if
},
问题是这样会有内存泄漏,Android设备上内存只涨不跌。
逻辑很简单就是随机位置显示一个球,掉落动画,10秒后删除,没什么特别的东西

