2008/10/27

how to pass variables from loading swf to loaded swf?

parent/loading swf:
package{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class LoadingSWF extends Sprite{
private var loader:Loader = new Loader();

public function LoadingSWF (){
loader.contentLoaderInfo.addEventListener( Event.INIT, onLoaderInit );
loader.load( new URLRequest( "ExampleB.swf" ) );
}

private function onLoaderInit( event:Event ):void
{
Object( loader.content ).init( "passing" );
}
}
}
/*=======================================*/
child/loaded swf:
package{
import flash.display.Sprite;
public class LoadedSWF extends Sprite{
public function LoadedSWF (){
}
public function init( value:String):void{
trace(value);
}
}
}

2008/10/05

How to See Your AS Code in Flex

Anatomy MXML tag:
//================================================================//
example codes:< id="'btn'" label="Click Me" cornerradius="20" onclick="'....'">
//================================================================//
Tags are Classes: 
import mx.controls.Button; btn:Button=new Button(); addChild(btn);
Attributes are properties:
btn.label='Click Me';
Attributes are styles:
btn.setStyle("cornerRadius", 14);
Attributes are Event Listeners
btn.addEventListener('onclick',someFunction);
//================================================================//

MXML is great for laying out structure, and ActionScript is built for interactivity. In this sample, you see that MXML markup is actually turned into ActionScript by the Flex compiler. Like ColdFusion Meta-Language(CFML) is html-based language transfer java background.

your MXML tag could transfer *.as files in your Flex project by following steps.

First, you open dialog by project>properties.
Second, choose additional compiler arguments under Flex Compiler. then add -keep or  -keep-generated-actionscript parameter at compiler parameters (spearte parameter by space). save the setting.
you could see all *.as used in your project right now in your src/generated folder in Flex Navigator panel.


2008/09/07

Astra─Tree component in AS 3.0

i discover component library in Flash CS3 has no tree component.
And i don't know why Adobe doesn't support it now.
so i search the google .....super!! Finally i found yahoo developer center provides useful components in AS3.0.

http://developer.yahoo.com/flash/

yahoo developer center includes a lot of information like Astra Flash Library Documentation to intro how to use them. even you can download all source to your computer including library document. ha... it is not bad.
although, i not yet use it's componen. but i hope it's component support dynamic showing icons in AS3.0.

2008/9/9
I used tree component and try to show dynamic icons by external file.....But it was not work....Maybe my ability isn't good enough. 
I find someone resolve like kind of problem, but his developing environment is in Flex builder.
i also find that tree component still exists in Flex 3. And there are a lot of components to use in Flex.....> <. I think I will learn Flex developing environment  someday.

2008/09/03

How stage, root, and MainTimeline Fit Together

this article is form a flash forum. i think the article is worth to keep it. so i post it here.

When you create a new ActionScript 3 application/movie, you're working with a main timeline as the "root" of your application. This root that *you* as a developer are working with then gets added to the stage when your main timeline instance is instantiated. This instance represents the root that is a property of DisplayObject. It is your main timeline.

In the property inspector in Flash CS3 you can associate a custom class with your root (aka main timeline) by filling out the document class field (available when nothing on the screen is selected). Whatever class you specify there has to be at least a Sprite, however, since a) it needs to exist as a child of stage and b) because other instances exist within this instance (unless code is used to explicitly move them to stage). If you do not specify a class for the document class Flash will create one for you called MainTimeline. This instance represents the root that is a property of DisplayObject. It is your main timeline.

If you write code in a timeline, you are defining code that will become part of the class definition of that movie clip symbol, or in the case of the main timeline, since it isnt a symbol, definitions specific to the MainTimeline or document class definition. If you both specify a class for the document class AND write code in the main timeline, then you will have to be sure that your document class extends at least MovieClip (instead of Sprite) since you are using frames in that movie clip (to write code) and it will not work for Sprite instances.

In all respects to the developer, the main timeline is the root of the application. Fittingly so, it is also the instance referenced by the root property of DisplayObject instances within that SWF. The root instance, however, is not the absolute root of the display list hierarchy. That title belongs to the stage instance.

The stage instance is an instance of the Stage class which is instantiated when the Flash player starts. It represents the stage or top-most level of the Flash player. SWFs are then loaded into this stage container with their root instances and played as a child of stage. DisplayObject instances have a stage property that reference this Stage instance.

The stage, however, unlike root, cannot be modified by Flash developers. It is always an instance of the Stage class and always the top level of the display hierarchy within the Flash player.

When a SWF is played, it will always start with 2 display objects in the display hierarchy, the stage instance with a single child of the root instance (MainTimeline instance or an instance of your document class if specified). Other instances would be generated by contents of the SWF and are typically children of root (though it is possible to add additional children to stage through ActionScript). Any symbols or graphics in the main timeline in Flash are children of root (as, again, root is the main timeline)

ALL DisplayObject instances have root and stage properties. Each of these properties (when valid) for any display object within a single SWF reference same root and stage instances, there are only one of each (the only exception is the root property of the stage which references the stage). These properties are null if you have an instance who is not a child of a display list within the stage display hierarchy.

For loaded SWFs, the stage properties of its DisplayObject instances will reference the same stage as that referenced in the SWF that loaded it; there can be only one stage. For loaded SWFs, however, the root property will reference that loaded SWF's root, or its main timleine instance, not the root of the SWF that did the loading.

To summarize: one stage, one root per SWF (which is the main timeline) and that root is an instance of a document class or the MainTimeline class if a document class isn't provided

2008/09/01

When to use the Event.ADDED_TO_STAGE?

form:http://www.flepstudio.org/forum/tutorials/775-event-added_to_stage.html

Here we are again with a new trick of the day (even though I do not consider it as a trick but pure extract of OOP). This one could help us more then once when developing application with Flash CS3.

I am talking about the event Event.ADDED_TO_STAGE.

We need a good example to arrive to the point of this tutorial.

Let us suppose that we have a MovieClip on stage named 'container_mc' and a second MovieClip named 'quadrato_mc'.

We would like to attach another MovieClip placed in library (associated to a class named 'clip') inside container_mc. Up till there everything is fine as we know more then once how to do it: take a look at this article to refresh your memory if needed.

My question to you at this point, would be:

do we need to create the class associated to the MovieClip that we want to attach o shall we let Flash do the job for us to create it ?

Personally, I do think that we should always create it ourself.

First of all, doing so, we do not need to write codes inside the MovieClip as we would write everything in the class keeping full control and keep away from unwanted errors.

The second point is that we can maintain the whole hierarchical tree of our application.

I explain myself better:

And if we wanted to retrieve quadrato_mc placed on stage from the attached clip that is found inside container_mc'

We know that the famous _root does not exist anymore and up till now we have used the technique to pass the value of the stage from the Document Class implementing the building function, from the class associated to the attached MovieClip, a receiving parameter inserted in a property.

It is not easy to explain it by words so let us see a concrete example'
Up till now, we have used this technique:

- a FLA named 'main.fla' into which we have a MovieClip placed on stage with an instance name 'container_mc' and a second one named 'quadrato_mc'


- a Document Class named 'Main.as'

Code:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
} } }


- the class, named Clip.as, associated to the MovieClip, named mc_clip, placed in library

Code:
package {
import flash.display.MovieClip;
public class Clip extends MovieClip {
private var _fla:MovieClip;
public function Clip(fla:MovieClip) {
_fla=fla;
} } }

We attach mc_clip inside container_mc from the Document Class the following way:

Code:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
attachClip();
}
private function attachClip():void {
var clip:MovieClip=new Clip(this);
container_mc.addChild(clip);
} } }

and if we wanted to retrieve quadrato_mc placed on stage from Clip.as:

Code:
package {
import flash.display.MovieClip;
public class Clip extends MovieClip {
private var _fla:MovieClip;
public function Clip(fla:MovieClip) {
_fla=fla;
trace(_fla.quadrato_mc);
} } }


Is it possible that from Clip.as there is not a way to retrieve the stage without having to go through the Document Class'!'The answer is using Event.ADDED_TO_STAGE .Let us see how.The same Document Class:


Code:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
} } }


Clip.as becomes as following:


Code:
package {
import flash.display.MovieClip;
public class Clip extends MovieClip {
public function Clip() {
} } }

If I instance Clip from Main:

Code:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
attachClip();
}

private function attachClip():void {
var clip:MovieClip=new Clip();
container_mc.addChild(clip);
} } }

and in the building function of Clip.as I try to retrieve the parent (which would be container_mc)

Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Clip extends MovieClip {
public function Clip() {
trace(parent);
} } }

I obtain a null!When I instance Clip, the building function is looking for the parent to carry out the trace. It can not find it as it has not been inserted yet into container_mc. In fact, container_mc.addChild(clip); is further down the line after I created the instance of Clip. This is why I needed to pass the value of the stage.Instead, if I add to Clip a listener to the event Event.ADDED_TO_STAGE, everything works out fine:

Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Clip extends MovieClip {
public function Clip() {
addEventListener(Event.ADDED_TO_STAGE,go);
}
private function go(evt:Event):void {
removeEventListener(Event.ADDED_TO_STAGE,go);
var _root:MovieClip=parent.parent as MovieClip;
trace(_root.quadrato_mc);
} } }

See you soon !

2008/08/29

Why it's not work when use MouseEvent.DOUBLE_CLICK in AS3?

recently, I use the MouseEvent.DOUBLE_CLICK event. it is very short simple codes.

/*=========================*/
import flash.display.Sprite;
import flash.events.MouseEvent;
/*=========================*/
someInstance.addEventListener(MouseEvent.DOUBLE_CLICK, onSomeFunction);
private function onSomeFunction(event:MouseEvent):void{
trace('double click');
}
/*=========================*/

And the code doesn't work.....finally, i search on net. find someone post the reason. it is cause double click will trigger button mode focus--up over down hitTest twice, so if your someInstance have children like MovieClip or some can focus instances inside. It will cause the focus change when you click it second time. so this event doesn't work. the solution is following:

/*=========================*/
import flash.display.Sprite;
import flash.events.MouseEvent;
/*=========================*/
someInstance.mouseChildren=false;
someInstance.addEventListener(MouseEvent.DOUBLE_CLICK, onSomeFunction);
private function onSomeFunction(event:MouseEvent):void{
trace('double click');
}
/*=========================*/

then it works successfully.

2008/08/22

Yahoo! Developer Network - Flash Developer Center - Articles - Developing with the Display List in ActionScript 3

from:Yahoo! Developer Network - Flash Developer Center - Articles - Developing with the Display List in ActionScript 3

Introduction to the Display List


With Flash Player 9 and ActionScript 3, Adobe created an easy and powerful way to add, remove, and change MovieClips on the screen. It's called the display list. No longer do you need to call strange functions like createEmptyMovieClip() and attachMovie(). Now, like you do with any other data type in ActionScript, you can call new MovieClip() to create a MovieClip object. To display it on screen, pass the MovieClip to the addChild() method of the desired parent.



If you try out the code above, not much will happen. To create something visible, let's draw on the MovieClip we createed. The graphics property on MovieClips, Sprites, and many other DisplayObjects works a lot like the drawing API in AS2. You can set the line and fill styles and draw lines and curves. To make drawing easier, Adobe also added functions like drawRect() and others for drawing simple shapes.



If you've drawn your own graphics in a library symbol, attaching it to the display list is just as easy. First, make sure to go into the symbol's Properties dialog and check Export for ActionScript. Next, just like with MovieClip, you'll be able to create an instance in a new variable and use it like any other object. For the following example, assume that we've created a symbol called MyLibrarySymbol in the library, and we've exported it for ActionScript. After we add it to the display list, we move it slightly so that it doesn't cover the first MovieClip we added.



Depth management has been simplified in ActionScript 3 too. You may need to realign your understanding of it, though. Before, in Flash 8 and below, you could add MovieClips to any depth you desired. If you liked the number 10000, you could add an item at that depth. If you added another item at depth 10000, you'd run into trouble because both objects would be stored at that depth and they wouldn't display correctly. With the new display list, you're now working with something that's a lot like a stack of books or magazines. You can put a new magazine directly on the top of the stack, or can you raise part of the stack up somewhere in the middle and place the new magazine there. However, unless you have 9999 magazines or more, you cannot put a new magazine at position 10000 because it would be defying the laws of gravity! The display list works the same way in that regard. If you have only three children, and you try to add another child at position 10000, you will receive a runtime error.

When we added the item2 to the display list, it was placed at the top of the stack, or directly above item1.

Below, we create another MyLibrarySymbol and add it to the bottom of the display list at index 0, under item1, using addChildAt(). The first two items will move up to index 1 and 2, respectively. Again, we offset the x and y values slightly so that the new item doesn't completely cover our first two items.



In AS2, you could move your MovieClips up or down depths using the swapDepths() function. This was a little limiting because you might need to move many MovieClips to get every one at the correct new depth. In AS3, we now have several useful functions to do the same thing. The functions swapChildren() and swapChildrenAt() work much like you'd expect. The first function takes two different children of a containing display object and swaps them, while the second takes two indices and automatically determines the children to swap at those positions. Another function, setChildIndex(), gives you a bit more power. You can now move a specific child to a specific index, and the other children will be moved logically out of the way.

In the next example, we move item3 above item1 on the display list. item1 is automatically moved below item3 and back to index 0. Of course, item2 is still at the top of the stack, and it doesn't change position.



Once you no longer need a particular MovieClip, you can remove it from the display list too. AS3 provides two functions for this operation. The first, removeChild(), will remove a specific child, and the second, removeChildAt() will remove the child at a specific index. Below, we remove item2 from the display list. If a removed child leaves a gap, the other items above it will move down as needed to fill the empty space. In this case, item2 is at the top of the stack, so the others will not need to move.



Download DisplayList.fla for a closer look. Try commenting out parts of the code to see each step one by one.

Those are the very basics of display list manipulation. It should be enough to get you started, but there may be a few things that could catch you off guard if you're experienced with AS2. Let's look a little deeper.


Changes to root and parent

In ActionScript 2, many developers find it easy to access the root, parents, and grandparents all along the display hierarchy. These developers may be in for a surprise when migrating to AS3. The AS3 compiler is strict, and it's going to throw errors the next time you try to use code that looks something like this:



At Yahoo!, we highly discourage our fellow developers from climbing through the display list through root. Simply put, it's dangerous. It could lead to all sorts of maintenance headaches if you ever need to change your code in the future. If you make a mistake somewhere, you may be plagued with strange bugs that are very hard to track down.

That, of course, doesn't tell you why the compiler throws an error. Though we discourage using root, we understand that developers can still use it. However, accessing children through root requires a little extra effort these days. The best way to explain the error is to ask a question:

What is root?

Obviously, it's the "root" or base of the tree structure that forms the display list, but what does the compiler think of root? According to the AS3 documentation, root is of type DisplayObject. This data type is an important key to understanding the error.

Why is DisplayObject relevant? First, you need to understand the difference between a "dynamic" class and a "sealed" class in ActionScript 3. A dynamic class will allow you to add and delete variables at runtime. The Object type is the most obvious example. You can always add or remove variables on a plain Object.



A sealed class has the opposite behavior. Sealed classes will not allow you to add or remove variables at runtime. This is the default behavior for classes in AS3. In other words, most classes in AS3 are sealed. Sprite is a good example of a sealed class.



In AS2, all classes were dynamic by default. You could add or remove properties on any variable without errors. This is why it was so easy to use _root to find a specific MovieClip.

Hopefully, you'll see where I'm heading with this. We want to reference children of root, but the language defines root as type DisplayObject. DisplayObject is sealed, like most classes in AS3. Thus, when you try root.appContainer, like in the example from earlier, you're accessing a variable named appContainer that doesn't exist on a DisplayObject. The compiler knows this, and it will throw an error.

By default, when you create a new FLA file, the root is a MovieClip. Like Object, MovieClip is one of the rare dynamic classes in AS3, and we can use that to our advantage.

If you must access a child of root, you need to explicitly tell the compiler, "trust me, root is a MovieClip". This is called casting. The following example will fix the error introduced the first time we tried to access root.



Since we know root is a MovieClip by default, we can cast it to the correct type. Now the compiler understands that root is more than a simple DisplayObject.

Download CastingRoot.fla for a closer look at how to use root in AS3. This FLA file contains a basic Flash application that features two of the Astra libraries available on the Yahoo! Flash Developer Center. A set of buttons lets you choose a library of your choice to view a short description. It's not a complex application, but it follows a familiar page structure that you should easily understand. Here's a preview:

To see a live example, please install Adobe Flash Player version 9 or higher.

You may run into problems if you always try to cast root as MovieClip. There's a reason the language defines it as a DisplayObject. If you're using a Document Class, you have the ability to subclass MovieClip, Sprite, UIComponent, or any other type of DisplayObject. Casting as MovieClip is easy for a basic FLA file, but when we use a Document Class, we need to cast root to a more appropriate datatype.

Imagine that our application uses a Document Class called MyApplication. It is a subclass of Sprite. In this situation, trying to cast root as MovieClip will throw a runtime error because a Sprite is not a MovieClip. If we want to reference something on root we should cast root as the Document Class, which we've named MyApplication.



A simple change, but more appropriate than MovieClip in this case. If your project uses a Document Class, you should always cast root using that class instead of MovieClip. This is true even when the Document Class subclasses MovieClip because the AS3 compiler can make your code run faster when it knows about a sealed class.

Much like when you use root, you need to take special care with parent as well. According to the AS3 documentation, the parent property of any DisplayObject is of type DisplayObjectContainer. As you should be starting to understand now, when you need to reference a MovieClip's parent, you may need to cast the parent to the appropriate type so that the compiler understands that you know what you're doing. Once again, DisplayObjectContainer is a sealed class, so it does not automatically get publicly-accessible properties for its children.

We're going to use the same basic example as we used with root. The application is simple enough that we only need to make minor changes to use parent instead of root to navigate the display list. Hopefully, you'll find it easy to understand since you've already encountered most of the code already.

Inside this application, we have an AppContainer symbol, which contains a MenuContainer symbol that holds the buttons and a PageContainer symbol that holds the library descriptions. Inside our MenuContainer, we listen for when the user presses the buttons. Similar to how we used root, we now want to reference parent in our click handler.



As stated above, the parent property in AS3 is defined as an instance of the sealed class DisplayObjectContainer. As a result, the preceding code will throw an error because DisplayObjectContainer doesn't have a property named pageContainer. However, just like we did with root, we can cast parent to tell the compiler, "it's okay, parent is a MovieClip" (or a more appropriate type, if required).



Once again, casting has helped the compiler understand a bit more about the structure you've created on the display list. Remember to cast as the correct type if parent is not a MovieClip. For instance, parent might be a Sprite instead.

Download CastingParent.fla to view the earlier example updated to use parent instead of root.

The preceding examples should help get many AS2 developers up to speed with the new display list in AS3. It may leave some feeling a little frustrated, though. If you're a Flash developer who uses _root and long strings of _parent._parent._parent in your AS2 code often, you're going to find yourself facing the need to cast objects quite a bit with AS3. Some important tips in the following section may help you improve your code, and you shouldn't need to hack the display list in many common situations.

Display List Best Practices

The Yahoo! Flash Platform team works hard to promote best practices when developing with ActionScript 3. Just like we discourage the use of root, we also want to make sure that you're communicating between components and separate parts of your applications effectively and without too many long strings of parent.parent.parent or equally long strings of children, grandchildren, and deeper items on the display list. For the very same reasons that we don't like using root, we've found that hacking through parents and children can lead to maintenance headaches and difficulties finding bugs.

An effective, object-oriented way to limit display list hacking is a method called encapsulation. Encapsulation involves making sure each component or discrete piece of your application knows only what it needs to know. The application I shared above is not properly encapsulated. For example, when the MenuContainer handles a button press, it updates the PageContainer to display the correct description. Ask yourself, "Should the MenuContainer itself need to know anything about the PageContainer and the pages within?"

No. What if we need to use a similar MenuContainer in the future? This new MenuContainer might not need to change pages. Instead, clicking the buttons might open the download pages for the Astra libraries. The MenuContainer functionality has nothing to do with PageContainers at all, so we'd need to recreate MenuContainer with nearly the same functionality. That can lead to a lot of duplicate work. Save yourself time and keep MenuContainer loosely coupled and generic.

To help make MenuContainer and PageContainer communicate without relying directly on each other, we're going to use events in MenuContainer to tell the application that a button has been pressed. The MenuContainer won't care what happens at this point. It simply dispatches an event that says that "Web APIs" or another option was chosen. The listener(s) can take care of all the details.

First, let's create our new event class, MenuContainerEvent. We start by subclassing flash.events.Event. If you've used Flash's built-in events, like KeyboardEvent or MouseEvent, you know that we need to pass an event type to the constructor. Let's create a new String constant so that the type can be specified using MenuContainerEvent.MENU_ITEM_SELECT. Next, we need a way to include the user's selection with the event, so let's add a selectedItem property to our new event and allow this value to be set in the constructor as well. Finally, we should always override the clone() method when we create a new event class. If you don't, you may encounter some strange runtime errors.



This class is saved in a file named MenuContainerEvent.as in the same directory as the FLA file.

Now, in our MenuContainer symbol, let's change the button click event handlers to dispatch our new event rather than accessing the PageContainer directly.



In our AppContainer, we should now listen for the new MenuContainerEvent and do something when we receive it.



Listening to the event only takes one line, and we will soon create a new function to handle the event, but how should we change the pages in the PageContainer? Calling this.pageContainer.page1.visible = true; would be another example of bad encapsulation. We don't need to know the little details of how the PageContainer works internally. Instead, our PageContainer should have a property to helps to hide the details of how it changes pages. Let's create a selectedPage property on PageContainer so that AppContainer can change this value in the MENU_ITEM_SELECT event handler.



Now, with the selectedPage property in place, we can write our event handler in AppContainer.



Download EncapsulationEvents.fla and MenuContainerEvent.as to view the application with encapsulation using events.

You may need to spend a few minutes studying all the details of the updated application. For someone new to the idea of encapsulation, it can be a bit confusing because it requires building an application or website from a different perspective. I've found that it's much easier to create and maintain Flash applications that are designed with encapsulation from the beginning. You may notice that the updated application doesn't include any of the casting I described in previous sections, and the display list manipulations never go deeper than one level. Each individual part is more independent and reusable!

If we wanted, we could probably go a little bit further with best practices and encapsulation. For instance, I would probably create classes for MenuContainer, PageContainer, and AppContainer. I'm not a big fan of timeline coding because it can be easy to forget where you placed all the code. Individual classes to associate with each symbol would help to make it easier to find your code, and you can edit them in your favorite text editor too. Timeline code doesn't have that flexibility.

Are you a display list master now?

The display list in ActionScript 3 differs greatly from the way we used MovieClips in previous versions of Flash. However, it offers many useful ways to help to make our everyday development easier. The biggest challenge to learning to use the new display list is to unlearn the tricky shortcuts and bad practices that were so prevalent in the days of AS2. Things have changed in many cases, and you should take some time to understand these changes and the reasons why Adobe decided to make them.

For more advanced developers, the last section on encapsulation is probably a healthy review. For someone who might not have any formal training in software engineering, it might be a bit hard to grasp immediately. That's okay, and I certainly understand. I remember back when I first started working with Flash. I was still finishing up my degree, and it took me a while to understand how Object-Oriented Programming (OOP) and best practices could make my job easier. If you want to start using best practices in your code, my strongest suggestion is to simply take your time. There's no rush as long as you're sufficiently motivated.