// 用cc.class 生成一个对象,包含数组皮肤 //在外面定义的cow_skin只能代表一种牛 const cow_skin = cc.Class({ name:"cow_skin", properties:{ cows:{ default:[], type:[cc.SpriteFrame] } } }); cc.Class({ extends: cc.Component, properties: { cow_set:{ default: [], type: [cow_skin] } }, onLoad () { //初始化 this.intervalTime = 0; //获取随机数 this.randomType = Math.floor(Math.random()*3); }, start () { }, update (dt) { //间隔时间 this.intervalTime += dt; let index = Math.floor(this.intervalTime / 0.2); //现在获得的index就表示从0开始,每隔0.2秒加1 index = index % 3; //cc.log(index) //获取一种牛的类型 //let cowSet = this.cow_set[0]; //将固定下标替换为随机数 let cowSet = this.cow_set[this.randomType]; //获取精灵组件 let sprite = this.node.getComponent(cc.Sprite); sprite.spriteFrame = cowSet.cows[index]; }, runCallBack(){ // cc.log("一个轮回结束!") //每次轮回结束以后再重新生成一个随机数 this.randomType = Math.floor(Math.random()*3); } });