Thursday, April 9, 2009

Flash Movie to send multiple, user defined values

Most of the time everybody works with the standard override values, ON - OFF or some setpoint value, but what if we need something more. What do you do if you made you own variable that does 3 or more functions. Plus what if we need to change the output based on the type of equipment. We will focus on a movie that has 3 buttons and will send a different user defined value depending on which button is clicked. In addition I will show you how to add and use EventListener's.

First start a new movie, drop a connector component and 3 buttons. Give everybody an instance name and assign the ID to the connector.

con.ID = PointID; // Assign the point ID to work with

Then we will add some code to change the button labels just incase the defaults don't jive with what we are tring to do.

//This runs to find if any params called names exist if it does then use it to change the names of the buttons
//names must be a comma delimited string of names
if (names != undefined){//param names exist
var button_names:Array = names.split(',');//split the string by comma's
button1.label = button_names[0];//assign first object
button2.label = button_names[1];//assign second object
button3.label = button_names[2];//assign third object
}

Now we need to make a variable to hold our output values then fill it up with some values

var output_values:Array = new Array();//create an array to hold the output values

If the prameter outputvalue exist then parse it and fill the array

if (outputvalue != undefined){
var outputValuesTmpArray:Array = outputvalue.split(',');
output_values[0] = outputValuesTmpArray[0];
output_values[1] = outputValuesTmpArray[1];
output_values[2] = outputValuesTmpArray[2];
}
else{//no user defined output values so assign some default values
output_values[0] = "0";
output_values[1] = "1";
output_values[2] = "2";
}

Your default values can be anything I would suggest your most common used values, so you don't have to override them as much.
Next all that is need is the code to send the value when the button is clicked.

button1.addEventListener("click",button1_listener);//create a listening function for the click event of button 1
function button1_listener(evt){
con.sendOverride(PointID,output_values[0]);//when button 1 is clicked send the 1st value of the output_value array
}

button2.addEventListener("click",button2_listener);//create a listening function for the click event of button 2
function button2_listener(evt){
con.sendOverride(PointID,output_values[1]);//when button 2 is clicked send the 2nd value of the output_value array
}

button3.addEventListener("click",button3_listener);//create a listening function for the click event of button 3
function button3_listener(evt){
con.sendOverride(PointID,output_values[2]);//when button 3 is clicked send the 3rd value of the output_value array
}

stop();//stop at this frame


Normally I would have you place the con.sendOverride function inside the button using the on(click) method and use the _parent scope, but I thought this would be a good opertunity to show you EventListeners. All objects(buttons, text, graphics...) fire events when they do things (mouse click|up|down|over, change,resize...) and if you what to do something when this happens you can attach a listener function to fire when the event happens. In the case of button1, we attached the function button1_listener to the "click" event, so when the "click" event fires button1_listener will "hear" it and then run. When the button1_listener function runs we send out the value of output_value[instance 0]. This can be used to make graphics into buttons or do something while typing...

Next just place the movie on your web page and set the PointID then names value with a comma delimited string (ie names=hand,off,auto) if you need, and do the same with outputvalue (ie outputvalue=ST_LOW,ST_MED,ST_HIGH or outputvalue=100.0 1,0.0 0,0.0 -1). If you need more then 3 options you can just add more buttons and use the next array value instance or maybe use a combobox or different type of UI to scale it up.

You can download the source and compiled here.

3_Button.rar

Labels: , ,

0 Comments:

Post a Comment

<< Home