本节课的学习内容是节点的计划任务。我通过翻阅API。
var schedule(
var selector,
var interval,
var repeat,
var delay
)
指定一个以秒为单位的时间间隔并定义一个自定义选择器。本节课的作业是通过点击一个按钮实现一个让node进行旋转和暂停的实验。我的代码如下:
var HelloWorldLayer = cc.Layer.extend({
sprite:null,
ctor:function () {
this._super();
var logo=new cc.Sprite(res.HelloWorld_png);
logo.setAnchorPoint(0,0);
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);
return true;
},
start:function()
{
this.schedule(this.update);
},
stop:function(){
this.unschedule(this.update);
},
update:function(){
var nowlogo=this.getChildByTag(111);
nowlogo.setRotation(nowlogo.getRotation()+1);
}
});
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
}
});
最重要的就是this.schedule(this.方法名称);来实现定时器的加载.停止是用unschedule。最后附上作业链接: