Reuseing code instead of copying it

15 views
Skip to first unread message

Ben Ward

unread,
Aug 23, 2012, 3:26:43 PM8/23/12
to c2-deve...@googlegroups.com
Sometimes I'm tempted to duplicate entire sections of code to a new area, especially for keyboard support or if the code is only slightly different somewhere else.

In these cases, it's better to put the code somewhere that can be accessed at any time. I like to use a boolean value as such:
   
 

Jacques Leemans

unread,
Aug 23, 2012, 3:30:40 PM8/23/12
to c2-deve...@googlegroups.com
Hi Ben,

There is also a Function Plugin, which is better to use:

http://www.scirra.com/forum/plugin-function_topic46104_post288742.html

--
 
 

Fernando Coelho

unread,
Aug 23, 2012, 3:43:37 PM8/23/12
to c2-deve...@googlegroups.com
The function plugin is very useful, specially with the timeline/Scenario plugin. I´ve been using a playSound function with a parameter that is the name of the sound needed to be played.
The scenario plugin allows you to make a list of actions that will be executed in order after a certain amount of time like this:

tag,action_01 //this is the tag, you can call an specific tag using the Start Scenario event
0,buttonToggle // this is a simple function without parameters that toggles the Active boolean in the buttons
0,playSound,'intro' // Calls the playSound function with the parameter intro
3,playSound,'intro_02'
0,buttonToggle // Toggles the button again
exit,, // Exits the tag

The functions look something like this:
on function playSound > play sound by name "function.param(0)" // Plays the first parameter introduced in the function.
on function buttonToggle > Toggle boolean "Active" on family "Buttons"

It´s very useful and makes the code a lot cleaner. You can make functions to play certain animations per example.


2012/8/23 Jacques Leemans <jjle...@gmail.com>
--
 
 

Reply all
Reply to author
Forward
0 new messages