ccc prefab

时间:2023-03-08 17:41:53

MonsterPrefab.js

var Helpers = require('Helpers');

cc.Class({
extends: cc.Component, properties: {
spriteList: {
default: [],
type: [cc.SpriteFrame]
}
}, // use this for initialization
onLoad: function () { //随机帧数
var randomIdx = Helpers.getRandomInt(0, this.spriteList.length);
var sprite = this.getComponent(cc.Sprite);
sprite.spriteFrame = this.spriteList[randomIdx];
} });

Populate.js

cc.Class({
extends: cc.Component, properties: {
root: {
default: null,
type: cc.Node
},
prefab: {
default: null,
type: cc.Prefab
},
canvas: {
default: null,
type: cc.Canvas
},
numberToSpawn: 0,
spawnInterval: 0
}, // use this for initialization
onLoad: function () {
var self = this;
self.randomRange = cc.p(300, 200);
self.spawnCount = 0;
self.schedule(self.addSpawn, self.spawnInterval);
}, addSpawn: function () {
if (this.spawnCount >= this.numberToSpawn) {
this.clearRepeater();
return;
} //初始化一个prefab
var monster = cc.instantiate(this.prefab);
monster.parent = this.root;
//this.canvas.node.addChild(monster);
monster.position = this.getRandomPosition();
this.spawnCount++;
}, //随机一个位置
getRandomPosition: function() {
return cc.p(cc.randomMinus1To1() * this.randomRange.x, cc.randomMinus1To1() * this.randomRange.y);
}, //清理定时器
clearRepeater: function() {
this.unschedule(this.addSpawn);
},
});