tag:blogger.com,1999:blog-3936088135561852742024-03-08T11:41:23.437+00:00Jack of all TiradesUnknownnoreply@blogger.comBlogger19125tag:blogger.com,1999:blog-393608813556185274.post-58423670355077947592011-04-01T16:05:00.004+01:002011-04-01T16:16:45.100+01:00Lottery Number FunctionAgain following from a previous discussions on generating unique random numbers within a range, here's a quick function you can use when the range starts from 1, just as most lottery draws do.var getRandomLottoNumbers = function (m:Number, n:Number):Array { m==undefined?m=6:m=m n==undefined?n=49:n=n for(var v=[];m;--n)Math.random()*n>m?0:v[--m]=n;return v}Using it is quite straightforward. Just Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-39035017454160785372009-10-29T15:31:00.006+00:002009-10-29T15:52:08.272+00:00Comparisons with switch/caseThe switch and case statements in Actionscript are a useful way of making nested if statements more readable in your code. However they do have one drawback in that the compiler won't allow you to make comparisons. For example, the following code will always skip over the case statements and return the default value:var myVar:Number = 15;switch (myVar) {case (myVar > 0 && myVar < 11) : trace("Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-84830161045918067402009-09-06T12:02:00.004+01:002009-09-06T12:06:56.300+01:00AnagramsA quick prototype to check if two strings are anagrams of each other:String.prototype.isAnagram = function(string:String):Boolean { return (this.toLowerCase().split('').sort().join('') == string.toLowerCase().split('').sort().join('')) ? true : false;};Use as follows:string_1 = "dear";string_2 = "read";string_3 = "reed";trace(string_1.isAnagram(string_2)); // truetrace(string_1.isAnagram(Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-73040088622699729282009-08-16T17:01:00.007+01:002009-08-16T19:09:30.284+01:00Easter revisitedJust changed the previous function to calculate Easter Day into a Date prototype. The prototype automatically returns a new Date() object for Easter for the current year but it will also accept an argument for any year:Date.prototype.calcEaster = function(year:Number):Date { if(year == null || year == undefined)year = this.getFullYear(); month = Math.floor((((19 * (year % 19) + Math.floor(year /Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-55986824343013940572009-07-17T18:13:00.007+01:002009-08-28T16:25:51.762+01:00Generate unique random numbers within a rangeFollowing on from a previous post on how to generate a list of unique random numbers, this similar function will allow you to do the same...but within a predefined range of numbers:function randListFromRange(firstNo:Number, lastNo:Number, num:Number, shuffled:Boolean):Array { var tempArray:Array = []; for (i = firstNo - 1; i < lastNo && num > 0; ++i) { if (Math.floor(Math.random() * (lastNo - i)Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-393608813556185274.post-65485526816463368802009-07-09T23:13:00.011+01:002009-07-10T00:14:07.924+01:00Change HTML background imageIn a similar vein to changing the background colour of a HTML page, this script will allow you to change the background image. Add the following Actionscript and a movieclip with an instance name of "btn":import flash.external.*;var jsCall:String;btn.onPress = function() { jsCall = String(ExternalInterface.call("changeBgImg", "myNewImg.jpg"));};Add the following Javascript to your HTML code:<Unknownnoreply@blogger.com3tag:blogger.com,1999:blog-393608813556185274.post-87029427095075698112009-04-03T22:56:00.013+01:002009-07-09T23:22:50.967+01:00Change HTML background colourMore External Interface stuff. This snippet of code will enable you to change the background colour of a HTML page. Add the following Actionscript and a movieclip with an instance name of "btn":import flash.external.*;var jsCall:String;btn.onPress = function() { jsCall = String(ExternalInterface.call("changeBgColor", "#fffecb));};Add the following Javascript to your HTML code:<script language=Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-29504178502373089842009-03-01T15:03:00.011+00:002009-03-01T15:55:57.779+00:00Generating a list of unique random numbersHere's a useful function for generating a list of N random numbers from a larger list M:function randList(max:Number, num:Number, shuffled:Boolean):Array { var tempArray:Array = []; for (i = 0; i < max && num > 0; ++i) { if (Math.floor(Math.random() * (max - i)) < num) { tempArray.push(i + 1); num--; } } if (shuffled) { tempArray.sort(function () { return Math.floor(Math.random() * 2) ? Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-393608813556185274.post-68544836034537658732008-12-02T19:25:00.010+00:002009-01-01T23:17:24.474+00:00Accessing HTML page detailsFlash only has a basic in-built property to retrieve details of the HTML page in which an SWF is embedded. You can retrieve the URL of the SWF file itself, for example with the following code:myTxt.text = this._url;Later versions of the Flash player also allow you to retrieve the URL of any JPEGs, GIFs or PNG files by applying the _url property to a movieclip into which any of those filetypes hasUnknownnoreply@blogger.com1tag:blogger.com,1999:blog-393608813556185274.post-59266764850036213342008-11-30T23:03:00.013+00:002008-12-02T00:34:32.595+00:00Rounding numbersPrevious posts touched on the subject of formatting numbers; either as formatting currency or separating with commas and two decimal places. Another question that's frequently asked is how to round to different numbers of decimal places?The standard answer is to use powers of 10 as per the following examples. To round to two decimal places use: roundedNumber = Math.round(yourNumber * 100)/100;Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-69298109527206256972008-08-06T12:51:00.003+01:002008-08-06T12:59:07.941+01:00Formatting currencyFollowing on from the previous entry about formatting numbers, it's a simple task to convert the same function into one that accepts currency symbols:function formatCurrency(num:Number, comma:Boolean, currency:String):String { // return a zero value if num is not valid if (isNaN(num)) { return "0.00"; } // return a blank value if currency is not valid if (currency == undefined) { currency = "";Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-9924809220943348962008-08-06T12:35:00.002+01:002008-08-06T12:50:36.106+01:00Formatting numbersThis one comes up a lot - how to format a number with commas and decimal points? Just pass your number into this function and it will return a formatted string. Including commas is optional.function formatNumbers(num:Number, comma:Boolean):String { // return a zero value if num is not valid if (isNaN(num)) { return "0.00"; } // round num to the nearest 100th num = Math.round(num * 100) / 100;Unknownnoreply@blogger.com3tag:blogger.com,1999:blog-393608813556185274.post-25210383302132654872008-08-04T08:34:00.019+01:002009-06-29T11:57:57.564+01:00Numbers to words in FlashIt is sometimes necessary to convert a given number into its equivalent value in words.At first this might appear to be a daunting and complex task but, fortunately, the method for formulating spoken numbers in English can be broken down into a set of simple rules. These rules are then applicable for any number, regardless of its size:if the number value is zero then the number in words is 'zero'Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-9528127696707241992008-08-02T15:29:00.005+01:002008-08-02T15:41:11.613+01:00Delay and jump to new frameA frequent problem is how to delay the timeline, for a given period of time, and then branch to another frame once the delay is up.A bespoke function and the setTimeout function can be combined to do this with ease:stop();var delay:Number = 1000; var frameNumber:Number = 10;function playFrame(frameNumber:Number){ gotoAndStop(frameNumber);}setTimeout(playFrame, delay, frameNumber);delay is the Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-9260455210930185622008-07-31T04:29:00.006+01:002008-07-31T21:15:43.679+01:00Sorting a numbered listOne task that's quite common is the sorting of data in an array. But there are some pitfalls to bear in mind, particularly when it comes to sorting numbers.For example, take the following simple list and apply a sort:var list:Array = [5, 10, 2, 21, 1, 15];list.sort();trace(list);// outputs [1, 10, 15, 2, 21, 5]Yikes...that didn't go exactly according to plan! So what happened? By default, the Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-23216937275024254792008-07-30T14:47:00.015+01:002008-07-31T12:07:22.334+01:00When the heck is Easter?Thanks to some arcane historical, and religious reasons, Easter has a tendency to slide around the calendar like a drunk man on an ice-rink. Fortunately it is still possible to calculate when it occurs in Flash with the following function:function calcEaster(year) {Months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-24546298128569111542008-07-30T14:30:00.011+01:002009-04-04T01:34:43.183+01:00Ordinal Numbers in FlashWe use ordinal numbers all the time, even though we may not always appreciate what they are, or why we're using them.An ordinal number reflects the rank of that number in a particular order, or its position. So we might use expressions like "he came first in the race", or "that's the third bus to come along in the past hour", or "today is the thirtieth of July". When we write down ordinal numbersUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-83377203766716526102008-07-30T13:56:00.013+01:002009-09-20T22:26:58.597+01:00Calculating the nth Date in FlashOne thing that's not easy to calculate in Flash is the occurrence of floating dates. For example, in the US, Thanksgiving Day occurs on the fourth Thursday of November whereas, in Canada, it occurs on the second Monday of October. But how do you calculate which is the fourth Thursday or the second Monday in any given month?The answer is this function:function nthDay(nth, weekday, month, year) { Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-393608813556185274.post-84170206933123233922008-07-30T13:09:00.034+01:002008-07-30T22:05:40.908+01:00Validating Dates in FlashHaving seen a plethora of functions that attempt to validate dates in Flash, almost all of them involving various methods of splicing strings and a whole heap of conditional if statements to check for leap years, month lengths, etc., I thought it would probably be a lot simpler to let the Date Object itself do the checking:function isValidDate(day, month, year):Boolean {var d:Date = new Date(yearUnknownnoreply@blogger.com0