Code Snippets for Beginners – AS3

Hello EveryOne !!¬† ūüôā

When we are working with Flash Games or Applications at the time some small functional codes are repeating like random number generation , string handling  and Array handling.  So creating library of reusable code snippets is a great way to speed up development.

I given below some small repository of actionscript code.

  • Generate Random Numbers

function randomRange(minNum:Number, maxNum:Number):Number
{
    return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}

  • Find the Angle Between Two Points

function getAngle (x1:Number, y1:Number, x2:Number, y2:Number):Number
{
   var dx:Number = x2 - x1;
   var dy:Number = y2 - y1;
   return Math.atan2(dy,dx);
}

  • Convert Degrees To Radians And Radians To Degrees

var degrees = 90;    // Test Value

var radians = degreesToRadians(degrees);

degrees = radiansToDegrees(radians);

function degreesToRadians(degrees:Number):Number
{
   return degrees * Math.PI / 180;
}

function radiansToDegrees(radians:Number):Number
{
   return radians * 180 / Math.PI;
}

  • ¬†Generate Random String

function generateRandomString(strlen:Number):String
{
   var chars:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   var num_chars:Number  = chars.length - 1;
   var randomChar:String = "";

   for (var i:Number = 0; i < strlen; i++)
   {
      randomChar += chars.charAt(Math.floor(Math.random() * num_chars));
   }

   return randomChar;
}

   trace(" Generated String :: "generateRandomString(5));

  • Remove The Whitespace From String
var str:String = "action scri pt";

function removeWhiteSpace(str:String):String
{
  var newstring = str.split(" ").join("");
  return newstring;
}

trace(removeWhiteSpace(str));
  • Count The No Of¬† Occurrences of a Character In a String
var str:String = "Moorthy";

var char:String = "o";

function countOccurences(str:String, char:String):Number
{
    var count:Number = 0;
    for(var i=0; i < str.length; i++)
    {
      if (str.charAt(i) == char)
      {
        count++;
      }
    }
   return count;
}

trace(countOccurences(str, char));
  • Search And Replace Characters In String

var str:String = "AS3 in Flash!!";
var search:String = "AS3";
var replace:String = "Actionscript 3";

function strReplace(str:String, search:String, replace:String):String
{
    return str.split(search).join(replace);
}

trace(strReplace(str, search, replace));

  • Reverse A String

var myString:String = "Moorthy";

var resultStr:String= myString.split("").reverse().join("");

trace(resultStr);

  • Find Min Value /Max¬† Value Of Array Of Integers
var min:Number;

var max:Number;

var numbers:Array =  new Array(3, 5, 2, 1, 4);

function findMinValue(array:Array):Number
{
   min = array[0];
   for (var i = 0; i < array.length; i++)
   {
      if (array[i] < min)
      {
         min = array[i];
      }
   }
  return min;
}

function findMaxValue(array:Array):Number
{
   max = array[0];
   for (var i = 0; i < array.length; i++)
   {
      if (array[i] > max)
      {
         max = array[i];
      }
   }
   return max;
}

trace(findMinValue(numbers));
trace(findMaxValue(numbers));
  • Time Format
function formatTime (time:Number):String
{
   var remainder:Number;
   var hours:Number = time / ( 60 * 60 );
   remainder = hours - (Math.floor ( hours ));
   hours = Math.floor ( hours );

   var minutes = remainder * 60;
   remainder = minutes - (Math.floor ( minutes ));
   minutes = Math.floor ( minutes );

   var seconds = remainder * 60;
   remainder = seconds - (Math.floor ( seconds ));
   seconds = Math.floor ( seconds );

   var hString:String = hours < 10 ? "0" + hours : "" + hours;
   var mString:String = minutes < 10 ? "0" + minutes : "" + minutes;
   var sString:String = seconds < 10 ? "0" + seconds : "" + seconds;

   if ( time < 0 || isNaN(time)) return "00:00";
   if ( hours > 0 )
   {
     return hString + ":" + mString + ":" + sString;
   }else
   {
     return mString + ":" + sString;
   }
}

trace("...."+ formatTime(100));

My hope this is useful for beginners, and you can see more useful snippet from here Snipplr

Cheers,

Kutty

Posted in Actionscript 3 | Tagged , , , , , , , , , , , , , , , , , , | 1 Comment

Distributing Custom Components – AS3

Hello Everyone ūüôā

Recently i am¬† posted “Custom Components in AS3 ‚ÄstScrollBar” later on , I realized that, after building custom components , i want to give them to other people, So i am decided to make the MXP custom components files for Flash.

MXP files are packages for components and are used to install extensions via the Adobe Extension Manager. If you don’t have the Extension Manager, you can get it here: http://www.adobe.com/exchange/em_download/ . Extension Manager is use to install the .mxp package files.

Before create the MXP file , we need to create 2 files one is SWC and another one is MXI.

Steps for Create SWC file:

Step 1: Download my previous custom scrollbar¬† and save it to your desktop.¬† ¬† ¬† ¬† ¬† ¬†¬† Step2: Open CustomScrollBar.fla in Flash CS 5 or above.¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† Step3: Open the Library (Ctrl/Cmd+L). You should see “ScrollbarAssets”¬† movieclip in¬†¬†¬†¬†¬† the library , (as shown in below Image_1). When you package your own components, be sure to use easy-to-understand names.

Step4: Right-click on the movie clip and choose Export SWC File ( as shown in Image_2).  The Export File dialog box appears.

Step5: In the dialog box, name your file scrollbar.swc and save it to your desktop.

After create the SWC file , we need to create the MXI (Macromedia Extension Information) file. The MXI file is simply a set of XML based instructions that tells the Extension Manager where to install the packaged files. It also  including its name, version number, type, add author information,description of the extension and where to put component file in the Flash installation directory.

Steps for Create MXI file:

Step1: Open a text editor. we can use Notepad or textedit for creating MXI files.¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† Step2: Add the following XML to a new file and save it as “scrollbar.mxi” to your desktop. make sure you should save it as “.mxi” extension.

 <macromedia-extension name="ScrollBar" version="1.0" type="Flash Component (SWC)">
<author name="Moorthy, Kutty" />
<products>
<product name="Flash" version="9" primary="true" />
</products>
<description>
<![CDATA[
Scrollbar for Flash Player 9 above.
]]>
</description>
<ui-access>
<![CDATA[
This component can be accessed from the Components panel.
]]>
</ui-access>
<files>
<file name="scrollbar.swc" destination="$flash\Components\CustomScrollbar" />
</files>
</macromedia-extension>
 

Then need to package the MXI and SWC files into the MXP file, we need to complete only a few more steps.

Steps for create MXP file:

Step1: Create a folder on your desktop named scrollbar¬† and drag the MXI and SWC files into it. When packaging components, it’s necessary to make sure that all the files that will be packaged are in the same place.¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† Step2: Launch the Adobe¬† Extension Manager.¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† Step3: Choose File > Package Extension.¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† Step4: In the Select Extension to Package dialog box, browse to the¬†scrollbar folder on your desktop and choose the scrollbar.mxi file.¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† Step5 : In the Save Extension Package As dialog box, name the file scrollbar.mxp and click OK. Save it to the¬†desktop scrollbar folder.¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† Step6: When the packaging process is complete, a message appears that reads “The extension package was successfully created,” (as shown in Image_3). Click OK to close the message window.

Now MXP package file is ready , we can install the extension.

Step1: Open the scrollbar folder.Where u save the mxp file. and double-click the scrollbar.mxp file icon.¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† Step2: Choose Accept to accept the Extension Disclaimer. When you see the message that says “the scrollbar extension has successfully been¬† installed”,¬†¬†clickOK.¬†¬† ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† Step3: Close and restart the Flash. Choose from menubar Window — > Components. The Components panel¬† show the list of available components.You should now see the name CustomScrollbar in the Components panel, (as seen in Image_4).

Now you can add and use the custom components through flash components panel.

You can download my custom mxp componet file from here scrollbar.mxp

and one more pdf file link for to lean more about MXI and MXP component Click Here.

Cheers,

Kutty

Posted in Custom Components, Flash | Tagged , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Custom Components in AS3 – ScrollBar

Hello Everyone,

Many times when i am developing games or web apps in Flash, I am used some repeated common custom components like scrollbar, drop down box and pre-loader components. So i am posted here my previous developed custom components. you can download and customize easily it’s very helpful to beginner users.

Custom Scrollbar in AS3:

The scrollbar component is one of the reusable component. we can use the UI scrollbar or ScrollPane component in the components set in flash IDE but in both those cases, the look is really difficult to customize.So i am created one of the simple and smooth scrollbar component. When you are creating scrollable content in your project you can use this components as per project requirement i mean you can customize the scrollbar skins and size.

Have a look at the demo scrollbar ūüôā

You can download the source files here:  CustomScrollBar

Cheers,

Kutty

Posted in Actionscript 3, Custom Components | Tagged , , , , , , , , , , | 7 Comments

Getting started with Adobe Flash CS6

Hello Everybody,

Today i did download a free trial of Flash Professional CS6 and start using. Really it’s cool.

It’s have some nice features like Exporting Assets, Mobile Content Simulater , Debugging¬† and publishing options and Flash CS6 also has the ability to export to the HTML5 library CreateJS ( toolkit for CreateJS)¬† you can export animations, images, and sounds, both from the stage and from the library, into clean JavaScript format.

My hope the following links are very useful for u !!!

 Adobe Flash Pro CS6    ( Free trial of Flash Professional CS6)

¬†CreateJS¬†¬†¬†¬†¬†¬† (JS API’s)

If you need reference  for how to use the Flash Professional CS6 ? and more features check the following links,

New Support for HTML5 in Flash Professional CS6

Getting started with the Flash Professional Toolkit for CreateJS

Now i am trying  export Flash to HTML5  one small game using toolkit for CreateJS.

I will post soon with more details and sample files ūüôā

Keep in touch !! ūüôā

Cheers,

Kutty

Posted in Flash | Tagged , , , , , , , , , , | Leave a comment

My First Blog Post

Hello Everyone,

This is a first installment of my blog.¬† ūüôā¬† I‚Äôm excited about it and nervous. Will it be useful? Will I be able to keep up with it? ūüė¶

I started this blog because I wanted to share my knowledge with other coders and help to beginners for progrmming  in Flash Platform.

Cheers,

Kutty

Posted in Actionscript 3 | Tagged , , , | 5 Comments