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 calleddt (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
// cannot be renamed
void update( float dt );// call the update function every frame
scheduleUpdate( );
void ClassName::update( float dt )
{
// code to run every frame
}// call the update function every frame
this.scheduleUpdate( );
update: function( dt )
{
// code to run every frame
}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.
// function can be renamed
void UpdateFunction( float dt );schedule( schedule_selector( ClassName::UpdateFunction ), 3.0, 5, 1.0 );
void ClassName::UpdateFunction( float dt )
{
// code to run
}this.schedule( this.UpdateFunction, 3.0, 5, 1.0 );
UpdateFunction: function( dt )
{
// code to run
},Schedule a function forever with a set time gap between calls
Example format:
schedule( functionToCall, timeBetweenFunctionCalls );
// function can be renamed
void UpdateFunction( float dt );schedule( schedule_selector( ClassName::UpdateFunction ), 1.0 );
void ClassName::UpdateFunction( float dt )
{
// code to run
}this.schedule( this.UpdateFunction, 1.0 );
UpdateFunction: function( dt )
{
// code to run
},Schedule a custom function to run every frame
Example format:
schedule( functionToCall );
// function can be renamed
void UpdateFunction( float dt );schedule( schedule_selector( ClassName::UpdateFunction ) );
void ClassName::UpdateFunction( float dt )
{
// code to run
}this.schedule( this.UpdateFunction );
UpdateFunction: function( dt )
{
// code to run
},Schedule a function once
Example format:
scheduleOnce( functionToCall, delayBeforeFunctionCall );
// function can be renamed
void UpdateFunction( float dt );scheduleOnce( schedule_selector( ClassName::UpdateFunction ), 1.0 );
void ClassName::UpdateFunction( float dt )
{
// code to run
}this.scheduleOnce( this.UpdateFunction, 1.0 );
UpdateFunction: function( dt )
{
// code to run
},Unscheduling - scheduled functions can be stopped very easily
Unschedule a custom function
unschedule( schedule_selector( ClassName::CustomFunctionName ) );this.unschedule( this.UpdateFunction );Unschedule all functions
unscheduleAllCallbacks( );this.unscheduleAllCallbacks( );Unschedule the update function
unscheduleUpdate( );this.unscheduleUpdate( );Updated 6 months ago
