Tutorial - Program Structure

Sprite Basic has 3 predefined methods, called directly by the system. You don't have to call them in your code. Those methods are OnInit(), OnUpdate(), OnCollision(). Outside those methods is the Global Section, where you declare you global variables (variables available throughout all methods of a scene) and the data to be read with 'read' basic command.
You can also define your own methods. Those won't be called explicitly by the program, you'll have to call them from OnInit, OnUpdate or OnCollision

Global Section

Global Section is the part of code outside any methods.
In this section, all variables declared are of scope Global, are available in any methods and retain their values between method calls.
At the opposite, variables declared within a method is local to that method, you cannot read their values from another method and they don't retain their values between method calls.

// insert your global variables here number count = 0 // hold number of iteration of OnUpdate. Retains its value between calls as it is a global variable object renderer // the renderer allowing to draw sprites, text etc... on screen object updateText1 object updateText2 label restorePoint data 1,2,3,4,5,6,7,8,9,10

OnInit

OnInit is function with a void return type (it doesn't return any value). The function has no arguments.
It is called only once, at the launch of scene. It allows you do make scene set up, like variables initializations, world and/or renderer set up, decor drawing etc...

// this method is called on scene start // if you have multiples files per scene, only one of this method is allowed method void OnInit () { renderer=app.createRenderer() // create a renderer, now we are able to draw text on screen renderer.drawText("OnInit() called","Arial",50,50,32,"red") // draw text using arial font, at position 50,50 , font size 32, red color }

OnUpdate

OnUpdate is also a function with a void return type (it doesn't return any value). The function has no arguments.
It is called at each loop iteration, before graphics are renderered to the screen. It allows you defines the gameplay, like moving monsters, computers spaceships etc as well as updating player position, shooting in reaction to its touuch screen/mouse/keyboard inputs..

// this method is called each frame // if you have multiples files per scene, only one of this method is allowed method void OnUpdate () { count = count + 1 // increase counter if updateText1 !=null // if text has been previously drawned renderer.remove(updateText1) // remove old text, otherwise it will remain displayed. Text is just a special kind of Sprite endif updateText1=renderer.drawText("OnUpdate() call "+count,"Arial",50,150,32,"yellow") // write the current number of iterations if updateText2 !=null // if text has been previously drawned renderer.remove(updateText2) // remove old text, otherwise it will remain displayed. Text is just a special kind of Sprite endif updateText2=renderer.drawText("Current value read : "+getReadValue(),"Arial",50,250,32,"green") // write the current number of iterations by calling the defined method }

OnCollision

OnCollision is also a function with a void return type (it doesn't return any value). The function is called when on or more collisions between bodies happen during a frame and has as argument a list of Collision object (that may hold only one collision).
This method is only useful if you use the physic engine, i.e. you have set up a world and added bodies to it, as well as the collision-detection behaviour. More on this later when we will talk about body collision in a a next tutorial.

// this method is called each time one or more collisions are detected on bodies that you registered collision-detection behavior // if you have multiples files per scene, only one of this method is allowed method void OnCollision(list collisions) { }

User-defined method

This kind of method is defined by the user. You have to specify the data type of the returned value between the keyword 'method' and the name of the method. If you want the method is called with parameter, you have to specify between brackets, with their data types, for example : method number Mult(number x, number y) {return x*y}
You can return a value with 'return' keyword although this is not mandatory. If you don't want to return a value, you have to specify the returned data type as 'void'

//method defined by user, called explicetely in OnUpdate method number getReadValue() { number n // n is local to the method read n if n=10 // if last value, we have to restore the data to the beginning, otherwise no more data to read -> error restore restorePoint endif return n }

You can try the code in TUTO-PROGRAM-STRUCTURE project on the Guest Account
You can find reference of the functions used in the Documentation