作业
本节内容讲解节点的计划任务,计划任务在游戏中或许是在常见不过的内容了,我们之所以能看到游戏画面中各种精灵的跳动、图层的移动等,都要归功于节点的计划任务,比如蓝天背景中的白云在飘,偶尔还飞过一只小鸟,比如天上偶尔掉下一块金子(当然现实中不会有这种好事。。。),这一切游戏中活物,都是节点的计划任务的功劳,想象下,如果没有计划任务,那么游戏世界一片死寂。
API
废话不多说,直接了解节点计划任务的API,虽然课程中只讲了两个API,schedule和unschedule,但搜索cocosjs的文档后,可以了解,其实他还有其他强大的用法,总结一下计划任务的API如下:
1 2 3 4 5 6 7 8 9 10 11
   | var node = new cc.Node();
  node.scheduleUpdate();
  node.schedule(callback, interval, repeat, delay);
  node.scheduleOnce(callback, delay);
  node.unschedule(callback);
  node.unscheduleAllCallbacks();
   | 
 
作业
不同游戏的不同需求,不同应用场景,可以灵活使用计划任务,让游戏活起来。课后的作业是使用节点的计划任务让节点不断的自我旋转,其代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   |                  var logo=new cc.Sprite(res.HelloWorld_png);         this.addChild(logo);         logo.setTag(111);         logo.setPosition(cc.winSize.width/2,cc.winSize.height/2);                  var itemstart=new cc.MenuItemFont("启动",this.start,this);         var itemstop=new cc.MenuItemFont("停止",this.stop,this);         itemstop.setPositionY(50);         var menu=new cc.Menu(itemstart,itemstop);         menu.setAnchorPoint(0,0);         menu.setPosition(100,100);         this.addChild(menu);                                    start:function(){                          this.schedule(this.update);         },                  stop:function(){             this.unschedule(this.update);         },                  update:function(){             var nowlogo=this.getChildByTag(111);             nowlogo.setRotation(nowlogo.getRotation()+5);         }
   | 
 
最终运行效果
http://www.cocoscvp.com/usercode/2016_04_18/ac5da37fd647d075cb9c4e67062a368cc469a636/