AS3.0 instance learns to be familiar with the package of AS3, and the mutual communication between multiple package _flash as

Source: Internet
Author: User
Tags addchild
Familiar with the package of AS3, and the mutual communication between multiple package

Description: A very simple demo, there are 4 buttons, when the mouse across and move away will show a different state, click to become disabled, and then click on the other button, before the Disabled button restore, the button was clicked failed.

Demo: http://www.live-my-life-with-yuyi.com/as3_cases/communicating/

Preparation: Open source file Communicating_ FINAL.FLA, click on the properties of the release settings, click Actionscript3 next to the settings, in the bottom of the classpath, the introduction of the Classes folder path, and then click OK, the preliminary work is ready.

Code:

The code in the source file is simple:

Copy Code code as follows:

Import Todd.interactive.ButtonSet;
var buttons:buttonset = new Buttonset ();
Buttons.addbuttons ([ONE_MC,TWO_MC,THREE_MC,FOUR_MC]);
AddChild (buttons);

Import the Buttonset class, instantiate it, then call one of the methods inside, and finally put it on the stage.

There are two as files in the Todd->interactive folder under the Classes folder, one of which is the buttonset that just called, to see the source code of Buttonset:

Copy Code code as follows:

Package todd.interactive
{
In fact, just load the display and events on it, but a few more load does not affect file size and efficiency
Import flash.display.*;
Import flash.events.*;
Import flash.filters.*;
Import flash.net.*;
Import flash.geom.*;
Import flash.ui.*;
Import flash.utils.*;
Import fl.transitions.*;
Import fl.transitions.easing.*;

public class Buttonset extends MovieClip
{
public Var Buttons:array;

Public Function Buttonset ()
{

}

Public Function AddButtons (buttonset:array): void
{
buttons = Buttonset;
for (var i:int = 0; i < buttons.length; i++)
{
AddChild (Buttons[i]);
}
}
}
}
A class should be placed inside a package, just as money should be put in a wallet. The path to the class is defined after package. Then a series of commonly used classes.
A global variable buttons is defined, and a public is added to the front of the variable. This can be accessed throughout the class.
The class name should be the same as the file name (case-sensitive), and then define a function with the same name that will be invoked when the class is initialized, just like the PhP4 class. It's just a shelf, and there's no concrete content.
Then defines a function addbuttons, its function is to put some MC or sprites in own container (addChild).
There is also a class: Disablingbutton, also located under the Todd->interactive folder, which is also at the heart of this case. Yes, the BaseClass in the linkage of Rectbutton has previously been set to Todd.interactive.DisablingButton.

The code is a little bit longer, and listen to me carefully.
Copy Code code as follows:


Package todd.interactive{
Import flash.display.*;
Import flash.events.*;
Import Todd.interactive.ButtonSet;
public class Disablingbutton extends MovieClip {
var Labels:array;
var thisparent:*;
var thisindex:int;
Public Function Disablingbutton () {
labels = this.currentlabels;
This.addeventlistener (Mouseevent.click, Disablebutton);
This.addeventlistener (Mouseevent.roll_over, over);
This.addeventlistener (mouseevent.roll_out, out);
This.addeventlistener (event.added,setparent);
}
function Disablebutton (event:mouseevent): void {
for (var i:int = 0; i < labels.length; i++) {
if (Labels[i].name = = "Disable") {
This.gotoandplay ("Disable");
}
}
This.removeeventlistener (Mouseevent.click, Disablebutton);
This.removeeventlistener (Mouseevent.roll_over, over);
This.removeeventlistener (mouseevent.roll_out, out);
Enableothers ();
}
function Enablebutton (): void {
This.addeventlistener (Mouseevent.click, Disablebutton);
This.addeventlistener (Mouseevent.roll_over, over);
This.addeventlistener (mouseevent.roll_out, out);
This.gotoandstop (1);
}
function over (event:mouseevent): void {
for (var j:int = 0; J < Labels.length; J + +) {
if (Labels[j].name = "Over") {
This.gotoandplay ("over");
}
}
}
function out (event:mouseevent): void {
for (var k:int = 0; k < labels.length; k++) {
if (Labels[k].name = = "Out") {
This.gotoandplay ("Out");
}
}
}
function SetParent (event:event): void {
if (This.parent is Buttonset) {
Thisparent=this.parent;
For (Var w:int=0 w < thisParent.buttons.length; w++) {
if (this = = Thisparent.buttons[w]) {
Thisindex=w;
}
}
}
}
function enableothers (): void {
for (var z:int=0; z < thisParent.buttons.length; z++) {
if (z!= thisindex) {
Thisparent.buttons[z].enablebutton ();
}
}
}
}
}

After loading two common classes, it also loads the Buttonset class just defined so that we can use some of the buttonset methods.

Note: This class must inherit the MovieClip class because the object of the class is an MC.
Some global variables are then defined (the default is public).
Create destructor disablingbutton,labels = This.currentlabels; The meaning of this sentence is to get the label attribute of the current MC, to return as an array, including attributes such as Label.frame,label.name and so on.
Then monitor your mouse to click, move, and move out of the event.
This.addeventlistener (event.added,setparent) The meaning of this sentence is to call the SetParent function when it is added to a container.
The function of Disablebutton is to change the state of the current MC into disabled, and then cancel the listener event and activate the other buttons.
The role of the Enablebutton function is to activate its own listening events and initialize its own state.
The over and out functions are simple to set their current state.
The ultimate goal of the SetParent function is to capture which MC the Click event occurs on (the gotoAndPlay method will trigger the event.added, so the over and out functions will trigger the SetParent function, which is also a place to be improved).
The enableothers function, by definition, activates other buttons. Because SetParent has remembered, the last click event occurred in which MC, so just traverse the buttons, and then activate the other MC on it.

Case analysis completed.
Packaging files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.