Flash MX 2004 New features example learning two

Source: Internet
Author: User
Tags define constructor copy empty new features variables reserved variable

instance three, customizing context Menu

I. Characteristics involved

This example reflects a significant programmatic improvement in Flash MX 2004. This includes the application of as file, the _global of the system, the invocation of the ContextMenu (menufun) function, the invocation of the Contextmenuitem () function, The use of variables has been MovieClip.prototype.menu. With these functions and variables, you can easily manipulate the menus in Flash Player.

Second, the production process

1, create a new action Script File, named "Clipboard.as", the contents are as follows:

/* Copyright 2003 Macromedia, Inc. All rights reserved.

The following is Sample Code and are subject to all restrictions

On such code as contained in the end User License agreement

Accompanying this product.

*/

Class ClipBoard extends object{//NOTE 1

static Var $contents: Object; NOTE 2

static Var $operation: String; Note 3

function ClipBoard () {}//Note 4

static public function cut (obj) {//Note 5

Obj._alpha = 50; Note 6

$contents = obj; Note 7

$operation = "Cut"; Note 8

}

static public function copy (obj) {//Note 9

$contents = obj;

$operation = "Copy";

}

static public Function paste () {//Note 10

if ($operation = = "Cut") {//Note 11

$contents. _x = _root._xmouse; Note 12

$contents. _y = _root._ymouse;

$contents. _alpha = 100; Note 13

$contents = undefined; Note 14

$operation = ""; Note 15

else if ($operation = = "Copy") {//Note 16

var newdepth = $contents. _parent.getnexthighestdepth (); Note 17

var newname = $contents. _name + newdepth; Note 18

$contents. Duplicatemovieclip (NewName, newdepth); Note 19

$contents. _parent[newname]._x = _root._xmouse; Note 20

$contents. _parent[newname]._y = _root._ymouse;

$contents. _alpha = 100;

$contents. _parent[newname]._alpha = 100;

} else {

Return Note 21

}

}

Public Function IsEmpty (): Boolean {//Note 22

if ($contents!= undefined) {//Note 23

return false;

} else {

return true;

}

}

Public function Handlemenucommand (obj, item): Void {//Note 24

Switch (item.caption) {//Note 25

Case "Cut Object"://Note 26

Cut (obj);

Break

Case "Copy Object"://Note 27

Copy (obj);

Break

Case "Paste Object"://NOTE 28

Paste ();

Break

}

}

}

This example has a lot of programming knowledge, just contact will have the feeling of not able to start, the following to explain the details of the things involved.

Annotation 1:class means to define a class, extends is the base class that represents the defined class is object. This involves programming object-oriented, may be more difficult to understand, you can see the object is defined as the class of the parent class, the parent class has methods and attributes, in subclasses are all.

Note 2: Define a variable contents, type Object. The variables are defined in Flash MX 2004. The variables that are defined here are available throughout the class.

Note 3: Define a variable operation, type string.

Note 4: This is the constructor of the class, and the so-called constructor is the function that is called when the class is instantiated. For example, the instantiation of this class can be as follows: ClipBoard cb = new ClipBoard ();

Note 5: Define a cut function, the incoming argument is obj, and its invocation scope is public, and that is, any class can be invoked.

Note 6: Set the Alpha property of incoming obj to 50.

Note 7: Setting the value of the variable defined in note 2 is obj.

Note 8: Set the value of the variable defined in note 3 as "cut".

Note 9: Define a copy function, the argument passed is obj, and the call scope is public.

Note 10: Define a paste function with no arguments passed in and the call scope is public.

Note 11: Determine if the value of the defined operation is "cut".

Note 12: Set the XY coordinates of the contents to the XY coordinates of the mouse.

Note 13: Set the alpha of contents to 100.

Note 14: After using the contents value, reset the contents value to undefined.

Note 15: Empty the value of the operation.

Note 16: Determine if the value of the defined operation is "copy".

Note 17: Define a variable newdepth, set its value to the depth of the contents defined in the class.

Note 18: Define a variable newname, set its value to the contents instance name plus the depth of the location.

Note 19: Copy a movie Clip, the parameters are newdepth and newname. This enables the functionality of replication.

Note 20: Set the location of the copied movie clip with Aplha.

Note 21: If operation is not "cut" or "copy", return directly.

Note 22: Define a Function IsEmpty (), the returned type is Boolean, and the scope of the call is public.

Note 23: Do the appropriate action based on the value of the contents.

Note 24: Defines a function handlemenucommand, returns NULL, and the invocation scope is public.

Note 25: Depending on the caption of the parameter item, the switch function is similar to multiple if judgments, but the switch applies in situations where the conditions of judgment may be many.

Note 26: If the value of caption is "Cut Object", call the Cut (obj) function.

Note 27: If the value of caption is "Copy Object", call the copy (obj) function.

Note 28: If the value of caption is "Paste object", call the Paste (obj) function.

2. Create a new FLA file, save it under the same directory as the clipboard.as you created in the first step.

3, press "Ctrl + F8" to create a new movie Clip, named "Square", in this movie Clip draw a rectangle with the rectangle tool.

4. Drag the movie Clip "Square" created in the third step to the scene and add the following action Script to its action panel:

On (release) {//NOTE 1

Stopdrag ();

}

On (Press) {//NOTE 2

StartDrag (this);

Mx.behaviors.DepthControl.bringToFront (this);

}

Note 1: When you release the mouse, stop dragging the object.

Note 2: When you press the mouse, start dragging the object and set the object to the first level.

5. Add a layer in the main scene's time line, named "Action", adding the following action Scipt to the action panel on this layer:

/* Copyright 2003 Macromedia, Inc. All rights reserved.

The following is Sample Code and are subject to all restrictions

On such code as contained in the end User License agreement

Accompanying this product.

*/

_global. $clipboard = new clipboard (); Note 1

function Menucallback (obj, menuobj) {//NOTE 2

var Empty:boolean = _global. $clipboard. IsEmpty (); Note 3

Menuobj.customitems = []; Note 4

if ((obj instanceof MovieClip) && (obj!= _level0)) {//Note 5

MenuObj.customItems.push (Cutitem); Note 6

MenuObj.customItems.push (CopyItem);

if (!empty) {//Note 7

MenuObj.customItems.push (Pasteitem);

}

' Else if ' (obj = = _level0 &&!empty) {//Note 8

MenuObj.customItems.push (Pasteitem);

}

}

var mymenu = new ContextMenu (menucallback); Note 9

Note 10

var cutitem = new Contextmenuitem ("Cut Object", _global. $clipboard. Handlemenucommand);

Note 11

var copyitem = new Contextmenuitem ("Copy object", _global. $clipboard. Handlemenucommand);

Note 12

var pasteitem = new Contextmenuitem ("Paste object", _global. $clipboard. Handlemenucommand);

Note 13

MovieClip.prototype.menu = MyMenu;

Note 1: This is the instantiation of the Clipboard () just defined, but _global. $clipboard is the system already has, so it can be used directly.

Note 2: Define a function menucallback, which is used when creating a menu.

Note 3: Defines a boolean-type variable empty whose value is the value returned after the call function _global. $clipboard. IsEmpty ().

Note 4: Set the Menuobj customitems to an empty array.

Note 5: Determine the attributes that obj has.

Note 6: Add "Cut" and "copy" to the menu.

Note 7: Add paste to the menu if empty is fake (that is, you already have a cut or copy operation).

Note 8: If the mouse does not have a selected object, add paste to the menu only.

Note 9: Instantiate an object MyMenu, where the constructor calls the Menucallback function.

Note 10: Instantiate an object Cutitem, which is a submenu that displays "Cut object".

Note 11: Instantiate an object CopyItem, which is a submenu with "Copy object" displayed.

Note 12: Instantiate an object Pasteitem, which is a submenu that displays "Paste object".

Note 13: Assign the defined mymenu to the system.

Iii. Practical uses

This example not only provides a way to manipulate Flash Player's menus, but also shows how to program with the as file. Knowing the details is useful for mastering the programming of Flash MX 2004.



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.