Developers > AS2 Learning Center > Advanced Tutorial
Requirements
- Adobe Flash 8
- yourminis API mxp (If you don't have this installed yet, follow the steps found here)
Steps 1. Create a New Document
The first step in this tutorial is to open Adobe Flash 8. Once the Flash IDE is open, create a new "Flash Document".
2. Initialize an Instance of The API Object
The constructor for the yourminis API class takes the following five optional parameters:
- Listener - A reference to the MovieClip to which you wish class events to be broadcast. Defaults to the _root of your swf.
- apiListener - A reference to the MovieClip that contains your mini. Defaults to the _parent of your swf.
- initWidth - The default width of the mini. Defaults to 240 pixels
- initHeight - The default height of the mini. Defaults to 200 pixels
- initColor - The default theme color of the mini. Defaults to black (0x000000)
In this example, and this holds true in just about all cases, we'll leave the first two parameters alone and play with the last three. Enter the following line of code in the Actions panel:
3. Logging
Probably the most helpful API function is the ability to log messages to the debug console. To log a message to the debug console type something like this:
4. Resize Events
The yourminis API allows you to specify a function to get called when the widget is resized. This is useful for handling the layout of your content once the widget is resized. There are two ways to set the onResize function. You can either set the function directly like this:
Add a new TextField to the stage at x = 0 and enter the text, "Hello, World!". Set the text to be centered and change the instanceName to "myText". In our resize function, well center the text in the mini. Well also test out the logging functionality.
ym_api.log( "doResize", "w: " + w + " h: " + h );
myText._width = w;
myText._y = ( h+25 - myText._height)/2;
}
5. Creating The Drop Down Menu An essential part of every yourminis widget is the drop down menu. The drop down menu is rebuilt every time you click on the drop down button. This allows for a lot of flexibility when it comes to displaying pertinent information in the drop down menu. When the drop down button is pressed, the onDropDownLoaded event is fired. It's in this function that you should put the code to build your drop down menu.
In order to create a drop down menu item you will need to call the addDropDownItem function which takes the following parameters:
- Label - The text to be displayed in the dropdown item.
- Data (optional) - an object to be passed to the callback function.
- Callback (optional) - a function that gets called when the dropdown item is selected. The function will be passed the data stored in the item's data object as a parameter.
- SecondaryIndex (optional) - the menu item to which this item is added as a second level menu (first index of menu is 0).
In this example we'll create a drop down menu to change the size of the widget which also incorporates another yourminis API function, setSize which takes the following parameters:
- Width - the new width of the widget
- Height -he new height of the widget
ym_api.log( "doDropDownLoaded", "" );
ym_api.addDropDownItem( "Resize" );
ym_api.addDropDownItem( "230x80", {w:230,h:80}, changeSize, 0 );
ym_api.addDropDownItem( "400x250", {w:400,h:250}, changeSize, 0 );
}
ym_api.setSize( theData.w, theData.h );
}
6. Locking
Widgets on the yourminis platform can be locked. Locking widgets is useful when you want to preserve the layout of a public page and prevent people from changing any of the settings. In this example, you'll remove the ability to change the widget size from the dropdown menu when the widget is locked.
ym_api.log( "doMiniLock", "isUnlocked: " + isUnlocked );
isWidgetLocked = !isUnlocked;
}
function doDropDownLoaded():Void {
if(!isWidgetLocked) {
ym_api.log( "doDropDownLoaded", "" );
ym_api.addDropDownItem( "Resize" );
ym_api.addDropDownItem( "230x80", {w:230,h:80}, changeSize, 0 );
ym_api.addDropDownItem( "400x250", {w:400,h:250}, changeSize, 0 );
}
}
7. Minimal View The minimal view for a widget removes all of the chrome. In this example we'll add a yourminis logo to the widget and have it only display when the widget is not in the minimal view.
Add this logo to the stage at around x = 4 and y = 1. Convert it to a MovieClip and set the instanceName to "logo". Next add the following code:
ym_api.log( "doViewChange", "isFullView: " + isFullView );
logo._visible = isFullView;
}
8. Color Changes When a user changes the color of the widget using the color picker, an onChooseColor event is fired. In this example we'll change the color of the "Hello, World!" text to lighter tint of the color that was chosen. The code for the getTintedColor is included with the source code of the example.
ym_api.log( "doChangeColor", "color: " + color );
myText.textColor = getTintedColor( color, 150 );
}
9. Disable Resizing
Although this is not included in this example, it is possible to disable the ability for a user to resize your widget using the following code:
10. Web Proxies Also not included in this example is the use of the proxy functions since they're not really needed until the widget is uploaded to the yourminis website.
Because of Flash security restrictions, data not located on the same domain as the swf file must be proxyed to be accessed. For this reason we've created two methods to handle the proxying for you.
The first function, getWebProxyUrl, takes the url of the site you want to load and converts it to a url that uses our proxy service. The second function, getRssProxyUrl, does essentially the same thing; however, we will cache these feeds for a short period of time to improve speed and efficiency.
The following is an example of how to use the getRssProxyUrl method:
var url:String = "http://digg.com/rss/index.xml"
var proxyUrl = ym_api.getRssProxyUrl( url );
var xml:LoadVars = new LoadVars();
xml.onData = function( data:String ) {
ym_api.log( "data", data );
}
xml.load( proxyUrl );
11. Getting and Setting Data Getting and setting data is very simple to do and only requires one step for each action. To save data use the setSetting function.
The getSetting function takes the two following parameters:
- settingName - The name of the setting to be stored on the server.
- Value - Value to be stored on the server and associated with the settingName for this individual instance of the mini.
To retrieve data from the server use the getSetting function. The getSetting takes the two following parameters:
- settingName - The name of the setting stored on the server.
- defaultValue - If the setting is not stored on the server already, the value of this object will be returned.
var myValue:String = ym_api.getSetting( "myName", "John" );
ym_api.log( "myName", myValue ); // displays "John" (assuming the value has not yet been set previously)
ym_api.setSetting( "myNumber" , 123 );
var myNumber:Number = Number( ym_api.getSetting( "myNumber", "1" ) );
ym_api.log( "myNumber", myNumber ); //displays 123... note value is cast from a String to a Number
Conclusion That concludes the Advanced Tutorial. We've covered all of the features of the yourminis API. At this point you should be able to create just about any widget you can imagine. If you have any questions or comments, use the Discussion feature of this group.
yourminis