{"_id":"547b1c907eec0f080099e1e3","version":{"_id":"5463e333447e0a100024a08a","project":"5463e333447e0a100024a087","__v":6,"createdAt":"2014-11-12T22:46:11.486Z","releaseDate":"2014-11-12T22:46:11.486Z","categories":["5463e333447e0a100024a08b","54645c365871e90800f50151","547b1c5a4366a708001b3b50","547b1cdf4366a708001b3b5a","54e3bef8464a9c3700f7ca67","54ec651d7ab3172d00c01ab7"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"project":"5463e333447e0a100024a087","user":"54639d06447e0a1000249f7f","category":{"_id":"5463e333447e0a100024a08b","project":"5463e333447e0a100024a087","pages":["5463e334447e0a100024a08d","54646a285871e90800f50174","54667e7af2b6390800dfd82a","54679c8c98b6840800268982","54679d8498b6840800268987","546a2ed0394c861a00c3a413","546b6a4862515a14007ebbba","547b1c2e7eec0f080099e1dd","55d33d90b9ef4435006dec70","56570c764d1c720d001f51bb"],"version":"5463e333447e0a100024a08a","__v":10,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-11-12T22:46:11.951Z","from_sync":false,"order":1,"slug":"documentation","title":"Documentation"},"__v":12,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-11-30T13:33:04.962Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"try":true,"basic_auth":false,"auth":"never","params":[],"url":""},"isReference":false,"order":13,"body":"Scheduling is a very powerful feature that Cocos offers. Allows you to abstract code and call it when you need to and as often you need to.\n\nNote: *Every scheduled function that is on this page has a variable called **dt (delta time)** which is the time between frames. This can be used to achieve framerate-independent gameplay.*\n\nNote: *The usual standard for where to put all the schedule methods is in the constructor.*\n\n##**Schedule Update** - calls the update function every frame\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// cannot be renamed\\nvoid update( float dt );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Header\"\n    },\n    {\n      \"code\": \"// call the update function every frame\\nscheduleUpdate( );\\n\\nvoid ClassName::update( float dt )\\n{\\n    // code to run every frame\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Implementation\"\n    },\n    {\n      \"code\": \"// call the update function every frame\\nthis.scheduleUpdate( );\\n\\nupdate: function( dt )\\n{\\n    // code to run every frame\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\n##**Scheduling Functions** - there are several different methods for scheduling functions\n\n**Schedule a function a set number of times with a set time gap between calls**\n\n*Example format:*\n\n*schedule( functionToCall, timeBetweenFunctionCalls, numberOfTimesToCallFunction, firstFunctionCallDelay );*\n\nNote:* **numberOfTimesToCallFunction** is the number time to repeat the function call so a value of 5 would call the function a total of 6 times, once then repeat it 5 times.*\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// function can be renamed\\nvoid UpdateFunction( float dt );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Header\"\n    },\n    {\n      \"code\": \"schedule( schedule_selector( ClassName::UpdateFunction ), 3.0, 5, 1.0 );\\n\\nvoid ClassName::UpdateFunction( float dt )\\n{\\n    // code to run\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Implementation\"\n    },\n    {\n      \"code\": \"this.schedule( this.UpdateFunction, 3.0, 5, 1.0 );\\n\\nUpdateFunction: function( dt )\\n{\\n\\t\\t// code to run\\n},\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\n**Schedule a function forever with a set time gap between calls**\n\n*Example format:*\n\n*schedule( functionToCall, timeBetweenFunctionCalls );*\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// function can be renamed\\nvoid UpdateFunction( float dt );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Header\"\n    },\n    {\n      \"code\": \"schedule( schedule_selector( ClassName::UpdateFunction ), 1.0 );\\n\\nvoid ClassName::UpdateFunction( float dt )\\n{\\n    // code to run\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Implementation\"\n    },\n    {\n      \"code\": \"this.schedule( this.UpdateFunction, 1.0 );\\n\\nUpdateFunction: function( dt )\\n{\\n\\t\\t// code to run\\n},\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\n**Schedule a custom function to run every frame**\n\n*Example format:*\n\n*schedule( functionToCall );*\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// function can be renamed\\nvoid UpdateFunction( float dt );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Header\"\n    },\n    {\n      \"code\": \"schedule( schedule_selector( ClassName::UpdateFunction ) );\\n\\nvoid ClassName::UpdateFunction( float dt )\\n{\\n    // code to run\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Implementation\"\n    },\n    {\n      \"code\": \"this.schedule( this.UpdateFunction );\\n\\nUpdateFunction: function( dt )\\n{\\n\\t\\t// code to run\\n},\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\n**Schedule a function once**\n\n*Example format:*\n\n*scheduleOnce( functionToCall, delayBeforeFunctionCall );*\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// function can be renamed\\nvoid UpdateFunction( float dt );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Header\"\n    },\n    {\n      \"code\": \"scheduleOnce( schedule_selector( ClassName::UpdateFunction ), 1.0 );\\n\\nvoid ClassName::UpdateFunction( float dt )\\n{\\n    // code to run\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Implementation\"\n    },\n    {\n      \"code\": \"this.scheduleOnce( this.UpdateFunction, 1.0 );\\n\\nUpdateFunction: function( dt )\\n{\\n\\t\\t// code to run\\n},\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\n##**Unscheduling** - scheduled functions can be stopped very easily\n\n**Unschedule a custom function**\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"unschedule( schedule_selector( ClassName::CustomFunctionName ) );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    },\n    {\n      \"code\": \"this.unschedule( this.UpdateFunction );\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\n**Unschedule all functions**\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"unscheduleAllCallbacks( );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    },\n    {\n      \"code\": \"this.unscheduleAllCallbacks( );\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\n**Unschedule the update function**\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"unscheduleUpdate( );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    },\n    {\n      \"code\": \"this.unscheduleUpdate( );\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]","excerpt":"This page will cover how to schedule functions.","slug":"scheduling-methods","type":"basic","title":"Scheduling"}

Scheduling

This page will cover how to schedule functions.

Scheduling is a very powerful feature that Cocos offers. Allows you to abstract code and call it when you need to and as often you need to. Note: *Every scheduled function that is on this page has a variable called **dt (delta time)** which is the time between frames. This can be used to achieve framerate-independent gameplay.* Note: *The usual standard for where to put all the schedule methods is in the constructor.* ##**Schedule Update** - calls the update function every frame [block:code] { "codes": [ { "code": "// cannot be renamed\nvoid update( float dt );", "language": "cplusplus", "name": "Cocos2d-x C++ Header" }, { "code": "// call the update function every frame\nscheduleUpdate( );\n\nvoid ClassName::update( float dt )\n{\n // code to run every frame\n}", "language": "cplusplus", "name": "Cocos2d-x C++ Implementation" }, { "code": "// call the update function every frame\nthis.scheduleUpdate( );\n\nupdate: function( dt )\n{\n // code to run every frame\n}", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] ##**Scheduling Functions** - there are several different methods for scheduling functions **Schedule a function a set number of times with a set time gap between calls** *Example format:* *schedule( functionToCall, timeBetweenFunctionCalls, numberOfTimesToCallFunction, firstFunctionCallDelay );* Note:* **numberOfTimesToCallFunction** is the number time to repeat the function call so a value of 5 would call the function a total of 6 times, once then repeat it 5 times.* [block:code] { "codes": [ { "code": "// function can be renamed\nvoid UpdateFunction( float dt );", "language": "cplusplus", "name": "Cocos2d-x C++ Header" }, { "code": "schedule( schedule_selector( ClassName::UpdateFunction ), 3.0, 5, 1.0 );\n\nvoid ClassName::UpdateFunction( float dt )\n{\n // code to run\n}", "language": "cplusplus", "name": "Cocos2d-x C++ Implementation" }, { "code": "this.schedule( this.UpdateFunction, 3.0, 5, 1.0 );\n\nUpdateFunction: function( dt )\n{\n\t\t// code to run\n},", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] **Schedule a function forever with a set time gap between calls** *Example format:* *schedule( functionToCall, timeBetweenFunctionCalls );* [block:code] { "codes": [ { "code": "// function can be renamed\nvoid UpdateFunction( float dt );", "language": "cplusplus", "name": "Cocos2d-x C++ Header" }, { "code": "schedule( schedule_selector( ClassName::UpdateFunction ), 1.0 );\n\nvoid ClassName::UpdateFunction( float dt )\n{\n // code to run\n}", "language": "cplusplus", "name": "Cocos2d-x C++ Implementation" }, { "code": "this.schedule( this.UpdateFunction, 1.0 );\n\nUpdateFunction: function( dt )\n{\n\t\t// code to run\n},", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] **Schedule a custom function to run every frame** *Example format:* *schedule( functionToCall );* [block:code] { "codes": [ { "code": "// function can be renamed\nvoid UpdateFunction( float dt );", "language": "cplusplus", "name": "Cocos2d-x C++ Header" }, { "code": "schedule( schedule_selector( ClassName::UpdateFunction ) );\n\nvoid ClassName::UpdateFunction( float dt )\n{\n // code to run\n}", "language": "cplusplus", "name": "Cocos2d-x C++ Implementation" }, { "code": "this.schedule( this.UpdateFunction );\n\nUpdateFunction: function( dt )\n{\n\t\t// code to run\n},", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] **Schedule a function once** *Example format:* *scheduleOnce( functionToCall, delayBeforeFunctionCall );* [block:code] { "codes": [ { "code": "// function can be renamed\nvoid UpdateFunction( float dt );", "language": "cplusplus", "name": "Cocos2d-x C++ Header" }, { "code": "scheduleOnce( schedule_selector( ClassName::UpdateFunction ), 1.0 );\n\nvoid ClassName::UpdateFunction( float dt )\n{\n // code to run\n}", "language": "cplusplus", "name": "Cocos2d-x C++ Implementation" }, { "code": "this.scheduleOnce( this.UpdateFunction, 1.0 );\n\nUpdateFunction: function( dt )\n{\n\t\t// code to run\n},", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] ##**Unscheduling** - scheduled functions can be stopped very easily **Unschedule a custom function** [block:code] { "codes": [ { "code": "unschedule( schedule_selector( ClassName::CustomFunctionName ) );", "language": "cplusplus", "name": "Cocos2d-x C++" }, { "code": "this.unschedule( this.UpdateFunction );", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] **Unschedule all functions** [block:code] { "codes": [ { "code": "unscheduleAllCallbacks( );", "language": "cplusplus", "name": "Cocos2d-x C++" }, { "code": "this.unscheduleAllCallbacks( );", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] **Unschedule the update function** [block:code] { "codes": [ { "code": "unscheduleUpdate( );", "language": "cplusplus", "name": "Cocos2d-x C++" }, { "code": "this.unscheduleUpdate( );", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block]