logo
Currently Browsing: Flex 3

Flash Primitive Data Types

logo

ActionScript 3.0, like many other programming languages, contains a number of basic data types commonly known as primitives. ActionScript 3.0 contains five primitive data types, which is far less than other languages like .NET or Java.

Data Type

Function

String

A character or series of characters

Boolean

A value that is either true or false

int

Positive or negative whole numbers

uint

Positive whole numbers

Number

Positive and negative whole and real numbers

Strings

Strings are a very important data type in programming languages that support them. For instance, string value can represent a sentence, a phrase, a word, a letter, or a directory location in an application, all of which are vital to many applications developed.

  1. //declare a String, and assign a value to it
  2. var str:String = "This is a string.";
  3.  
  4. //trace the value of the String
  5. trace(str);

In the above example, when the value of String str is traced, the output will read This is a string.

Booleans

Booleans are a great way to evaluate whether a statement in code is true or not. A developer can program an application to do something whether or not a statement is true. For example:

  1. //declare an int with a value of 5 and boolean
  2. var five:int = 5;
  3. var bool:Boolean;
  4.  
  5. if (five > 6)
  6. {
  7.         bool = true;
  8. }
  9. else
  10. {
  11.         bool = false;
  12. }
  13.  
  14. //when comparing booleans, always make sure to
  15. //use two ‘=’ signs, otherwise you’ll set the
  16. //value of the variable to what you are trying
  17. //to compare. This is called a logical operator.
  18. if (bool == true)
  19. {
  20.         trace("This should actually be false. The value is 5, " +
  21.                   "greater than 6.")
  22. }
  23. else
  24. {
  25.         trace("The value of the int is 5.");
  26. }

The above code will set the Boolean value to false because the value of five is not greater than 6. After that, the second if statement checks to see whether or not the Boolean’s value is set to true or false. Seeing that the Boolean is false, it will return the trace statement in the else statement.

Integers, Unsigned Integers, and Numbers

Flash comes equipped with a small set of numeric data types that allow the flexibility for almost any developer. If a variable needs to store positive whole numbers, then one would use an unsigned integer to store the values. For whole numbers that are positive and negative, using an integer or Number would be ideal. The difference between integers and Numbers is that Numbers can also store real number values whereas integers only store whole numbers. Also, Number data types can store a headache inducing amount of data compared to integers and unsigned integers. Here’s a breakdown of numeric data type values in Flash

Data Type

Minimum Value

Maximum Value

integer

-2,147,483,648

2,147,483,647

unsigned integer

0

4,294,967,295

Number

4.9406564584124654e-324

1.79769313486231e+308

As you can see, Numbers are great for not only storing real numbers, but also for REALLY HUGE values. Remember, when programming, choose the numeric data type that’s the most ideal for the situation. For example, if you plan on storing numeric whole number values (positive or negative) that aren’t going to contain decimals, then you’re probably best off using integers. Any time decimals or real numbers in general are involved, use Numbers. When you are developing any type of application or animation, you have to keep system resources in mind to the best of your ability. By doing so, this ensures optimal performance on most computers.

As always, source code is available here.

How To Check for Dock Icon or System Tray Icon Support in Adobe AIR

logo

When creating Adobe AIR applications, one of the more important (and easily overlooked) things that people like is System Tray Icons (Windows) or Dock Icons (Mac) for simple user commands such as changing preferences in the application or exiting entirely.

One of the many advantages of using Adobe AIR is the cross platform support, but how does an application know if it’s running on a Windows or Mac platform in order to accommodate a System Tray or Dock icon for each respective environment? The development team behind Adobe AIR have conquered this problem for developers. Within the NativeApplication Class there’s two boolean properties to resolve this problem.  The boolean property supportsDockIcon is used to detect whether or not the native operating system supports Dock Icons (Mac) while the other, supportsSystemTrayIcon, is used for Windows based systems.

What can be done, is that an if-else if statement can be written to compensate for both environments.  In most cases, you’d want the logic in both parts of the statement to be similar, with the exception of the size of the icon.  This way, continuity and consistency is maintained between both platforms.

  1. if(NativeApplication.supportsDockIcon)
  2. {
  3.         //supports Dock Icons for Mac
  4.         //insert logic here
  5.         //examples: icon, menu options
  6. }
  7. else if(NativeApplication.supportsSystemTrayIcon)
  8. {
  9.         //supports System Tray Icons for Windows
  10.         //insert logic here
  11.         //examples: icon, menu options
  12. }

As you can see, the idea is pretty simple. If the boolean returns true, then the respective statement will execute.  If you would like this code expanded on, please let me know. I’d be more than happy to either update the post or write a follow up post on this topic.

Happy coding! icon smile How To Check for Dock Icon or System Tray Icon Support in Adobe AIR

How to Remedy Flex Error #2148

logo

One of the most common (and least favorite) errors I see classmates encounter when they initially start out using Adobe Flex technologies is error #2148.  Here is the latest run-time error read out I receive:

SecurityError: Error #2148: SWF file file:///C:/Documents and Settings/Brett/Adobe Flash Builder Beta 2/Hello World Flex4/bin-debug/main.swf cannot access local resource file:///C:/Documents and Settings/Brett/Adobe Flash Builder Beta 2/Hello World Flex4/bin-debug/textLayout_4.0.0.10485.swf. Only local-with-filesystem and trusted local SWF files may access local resources.

From the looks of it, that’s a lot to take in.  To simplify, what’s happening is that when the application is compiled or tested, it is being accessed by the bin folder, which is a special directory in this instance.  One of the first things I do is I access the Global Security Settings Panel to make sure that I have added my work folders to the Always Trust list.

Adobe Flash Player Global Security Settings How to Remedy Flex Error #2148

My Adobe Flash Player Global Security Settings Panel

You’ll notice that both my Flex 3 and Flash Builder workspaces have been added to the list.  You can add yours by selecting the appropriate option under the Edit locations… dropdown menu.

An On the Fly Alternative

If you are looking for an on the fly, or project specific alternative to solving this issue is to add the argument -use-network=false to the compiler properties.  This can be done by going under Project > Properties > Flex Compiler

Flex Compiler Properties How to Remedy Flex Error #2148

Flex Compiler Properties with argument addition highlighted

I usually don’t recommend this latter option, but if for some reason security settings are really tight on your development machine and you cannot get the previous method to work, this will result in a quick to your problem.

Comments and questions are always welcome. icon smile How to Remedy Flex Error #2148

Happy coding!

How to Create a Hello World Application in Adobe Flex 3

logo

What is Flex?

Adobe Flex is a powerful and user friendly platform for developing and deploying rich internet applications (RIAs) for Adobe’s overarching Flash platform.  One of the strengths of Flex is its ability to integrate with many web services such as ColdFusion, J2EE, and LiveCycle Data Services.  For more information, please visit Adobe’s Flex 3 site. If you have any questions about Flex, please feel free to contact me as I would like to focus mainly on the code for the Hello World application in this post.

Creating a Hello World Application

As you can see, there are actually two examples of a basic Hello World application in the above Flex project.  That’s because I created the example using two different approaches of how Flex handles events within code.  Below is a screenshot for the source code. You can download the source here.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
  3.         width="436" height="248" creationComplete="mainFunction()">
  4.         <mx:Script>
  5.                 <![CDATA[
  6.                         import mx.controls.Alert;
  7.                        
  8.                         //main function to run at application startup
  9.                         /* adds event listener to the 'hello2' button
  10.                          * that listens for a user mouseclick. */
  11.                         private function mainFunction(): void
  12.                         {
  13.                                 this.hello2.addEventListener(MouseEvent.CLICK, helloFunction);
  14.                         }
  15.                        
  16.                         //event function that fires an alert window on click
  17.                        private function helloFunction(): void
  18.                        {
  19.                          Alert.show('Hello World!', 'Message 2');
  20.                        }
  21.                 ]]>
  22.         </mx:Script>
  23.        
  24.         <!– Hello World Example 1 utilizing in line MXML click events –>
  25.         <mx:Label y="36" text="Hello World Example 1" color="#FFFFFF"
  26.                 fontWeight="bold" horizontalCenter="0"/>
  27.         <mx:Button y="62" label="Hello World 1"
  28.                 click="Alert.show(‘Hello World!’, ‘Message’)"
  29.                 id="hello1" color="#000000" horizontalCenter="0"/>
  30.        
  31.         <!– Hello World Example 2 using event handlers in AS3 –>
  32.         <mx:Button y="125" label="Hello World 2"
  33.                 id="hello2" horizontalCenter="0"/>
  34.         <mx:Label y="99" text="Hello World Example 2" color="#FFFFFF"
  35.                 fontWeight="bold" horizontalCenter="0"/>
  36. </mx:Application>

EDIT: For some reason, the plugin I use adds an odd string in the helloFunction() Alert text. Ignore the string of text http://brettwidmann.com.nyud.net/2010/03/ as this could be a conflict with my CDN.

The most important concepts to understand in this example is the relationship between Flex’s MXML markup and ActionScript 3.0.  As you can see, the first example fires an Alert window event using the click attribute, which also contains the formatting for the window.  The second example demonstrates how one would code the same thing using AS3 event handlers. You’ll notice the second approach is more verbose than the first, but keeps MXML and AS3 separate from each other.

Finally, notice that in the Application tag, I have the creationComplete attribute firing a function called “mainFunction”.  The purpose of this function is to create the event listener on the second button, so that when the button is clicked, the listener tells the button to fire the function called “helloFunction”.

If you have any questions or comments, please feel free to leave them on here. icon smile How to Create a Hello World Application in Adobe Flex 3

EDIT: For a Flash Builder version of this post, click here.

Events in Flex 3

logo

The following tutorial demonstrates how events can be handled in Adobe Flex Builder 3 using an event listener or listening through the MXML code.  When finished with the code, you should see an Alert window pop up, and two events being fired simultaneously when a button is clicked.  You’ll also notice an event being fired to clear the text when a button is clicked.

Unfortunately, I cannot give out the source code for this project, as it is intended to be a tutorial for students at UWSP.

Happy coding!

To view a higher resolution video, click here

logo
Powered by Wordpress. Copyright 2009 Brett Widmann.