Just a wee code snippet in case I need it again one day. Adding stylesheet functionality I'd usually use external CSS for from within the actionscript. Pretty easy: static private function setStyleSheet():void { styleSheet = new StyleSheet(); // create object for each style //links var a:Object = { color: "#FF0000" } styleSheet.setStyle("a", a); var hover:Object... Continue Reading →
PrintJob in AS3
Basic printing in AS3: var my_pj:PrintJob = new PrintJob(); if (my_pj.start()) { try { my_pj.addPage([params]); } catch(e:Error) { // handle error } my_pj.send(); } Where you replace [params] with the Sprite you want to print. If you want to print the provided clip to the print page width, you can resize it to the right... Continue Reading →
SharedObject to save data to local drive AS3
SharedObjects are awesome, store variables locally on the users machine if you're unable to use a database etc. to store it. Here's some code from something using it to increase / store a viewCount variable. public function createSo():void { //create the local Shared Object try { cookie = SharedObject.getLocal(String(moduleID)); // direct reference to the moduleID's... Continue Reading →
Date and Time and Days with AS3
Some useful snippets of code for dealing with the date function in Flash. Needed this stuff when using a database provided start date for a user using an application, so as to know how many days had elapsed since they'd 'started' so had access to new stuff. var then:Number = Date.parse("Oct 25 2010"); var now:Number... Continue Reading →
Stopping Flash caching everything
To stop Flash caching every URLRequested file you pull in, you need to trick flash into thinking the file is different. You can do this by changing the URL with a date/time reference, so the URL is different every time. Try sticking the below on the end of the url to load: "&nocache=" + new... Continue Reading →
Random item from an array
Just a silly wee snippet - often one might forget the -.5! var randomIndex:Number = Math.round(Math.random() * myArray.length-.5);
Switches (case) in AS3
function topicNavHandler(event:MouseEvent):void { switch(event.type) { case MouseEvent.ROLL_OVER: trace("rollover!"); break; case MouseEvent.ROLL_OUT: trace("ROLLOUT!"); break; }
draw gradient dynamically with AS3
Found this gem of an example for drawing dynamic gradients: /**************************** Import Classes ****************************/ import flash.display.*; import flash.geom.*; /**************************** Define Variables ****************************/ //Type of Gradient we will be using var fType:String = GradientType.LINEAR; //Colors of our gradient in the form of an array var colors:Array = [ 0xF1F1F1, 0x666666 ]; //Store the Alpha Values in... Continue Reading →
Hide UI scrollbar when there isn’t enough text
If you only want the scrollbar to show when there is enough text to warrant a scrolling, use the following: vScrollBar.visible = contentText.maxScrollV > 1; contentText.addEventListener(Event.CHANGE, function():void{ vScrollBar.visible = contentText.maxScrollV > 1; }); src: http://www.kirupa.com/forum/showthread.php?t=296865
ScrollBarUI not scrolling on text change
If the flash component scrollbar isn't moving when you change the text inside it, you can manually call the update function on the scrollbar after you resize / change the text so it'll know to update again. scrollbar1.update(); Massive!
htmlText missing lines of text – autosize – AS3
If you've got htmlText, imported from, say, xml - you might lose some lines in bullet lists and things, just some real odd stuff like that. You can sort that out by assigning autosize to the textField, AFTER you've assigned the text to it. coursesField.autoSize = TextFieldAutoSize.LEFT; coursesField.wordWrap = true; coursesField.htmlText = whatever;
Character codes for ECMA based languages
\f matches form-feed. \r matches carriage return. \n matches linefeed. \t matches horizontal tab. \v matches vertical tab. \0 matches NUL character. [\b] matches backspace. \s matches whitespace (short for [\f\n\r\t\v\u00A0\u2028\u2029]). \S matches anything but a whitespace (short for [^\f\n\r\t\v\u00A0\u2028\u2029]). \w matches any alphanumerical character (word characters) including underscore (short for [a-zA-Z0-9_]). \W matches any... Continue Reading →