cattle.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // 用cc.class 生成一个对象,包含数组皮肤
  2. //在外面定义的cow_skin只能代表一种牛
  3. const cow_skin = cc.Class({
  4. name:"cow_skin",
  5. properties:{
  6. cows:{
  7. default:[],
  8. type:[cc.SpriteFrame]
  9. }
  10. }
  11. });
  12. cc.Class({
  13. extends: cc.Component,
  14. properties: {
  15. cow_set:{
  16. default: [],
  17. type: [cow_skin]
  18. }
  19. },
  20. onLoad () {
  21. //初始化
  22. this.intervalTime = 0;
  23. //获取随机数
  24. this.randomType = Math.floor(Math.random()*3);
  25. },
  26. start () {
  27. },
  28. update (dt) {
  29. //间隔时间
  30. this.intervalTime += dt;
  31. let index = Math.floor(this.intervalTime / 0.2);
  32. //现在获得的index就表示从0开始,每隔0.2秒加1
  33. index = index % 3;
  34. //cc.log(index)
  35. //获取一种牛的类型
  36. //let cowSet = this.cow_set[0];
  37. //将固定下标替换为随机数
  38. let cowSet = this.cow_set[this.randomType];
  39. //获取精灵组件
  40. let sprite = this.node.getComponent(cc.Sprite);
  41. sprite.spriteFrame = cowSet.cows[index];
  42. },
  43. runCallBack(){
  44. // cc.log("一个轮回结束!")
  45. //每次轮回结束以后再重新生成一个随机数
  46. this.randomType = Math.floor(Math.random()*3);
  47. }
  48. });