Flex implements swf interaction for loading as2

Source: Internet
Author: User

Flash can do a lot of expressive animation effects and interactive effects, which is incomparable to flex.
But it does not mean that Flex cannot achieve the same effect.
It is not difficult for Flex programs to integrate Flash because they all use the ActionScript language.
Flex can also control Flash
Inserting Flash In Flex is a good way to improve program interaction.
The advantages of Flex and Flash can be realized.
However, there is an AS version problem: when the swf file is loaded, both the loaded file and the main file run in FlashPlayer.
Flex adopts the As3.0 language, and the corresponding swf file is interpreted and executed by the player's AVM2 (Virtual Machine,
When the loaded swf is not in as3.0, the player uses AVM1 to explain the execution.
These two versions cannot pass information, causing Flex to be unable to determine the location of the Swf file and to use functions and variables. This greatly reduces interaction.

You can use the flash.net. LocalConnection object to cleverly solve this problem. You can avoid the player restrictions, regardless of whether swf runs on AVM1.
Or AVM2, can communicate with each other.

Example:

Flex (as3 swf) transmits a value to an as2 swf

 

1
2 <? Xml version = "1.0" encoding = "UTF-8"?>
3 <mx: Application xmlns: mx = "http://www.adobe.com/2006/mxml" layout = "absolute" creationComplete = "initApp ()">
4 <mx: Script>
5 <! [CDATA [
6 import flash.net. LocalConnection; // import LocalConnection class
7 private var CNum: Number = 0; // The swf passed to as2 by the Flex variable
8 private var sendConn: LocalConnection;
9 //
10 internal function initApp (): void {
11 // create a LocalConnection instance
12 sendConn = new LocalConnection ();
13 //
14}
15
16 internal function addNum (): void {
17 CNum ++;
18 trace (CNum + "cc ")
19 // send (connection name: String, Method in Flash: String, parameter)
20 sendConn. send ("flexToSwf", "showNum", CNum );
21}
22 public function performancenum (): void {
23 CNum --;
24 trace (CNum + "dd ")
25 // send (connection name: String, Method in Flash: String, parameter)
26 sendConn. send ("flexToSwf", "showNum", CNum );
27
28}
29]>
30 </mx: Script>
31 <mx: SWFLoader x = "30" y = "10" source = "test.swf"/>
32 <mx: Button x = "48" y = "403.5" label = "parameter + 1" click = "addNum ()"/>
33 <mx: Button x = "99" y = "403.5" label = "parameter-1" click = "performancenum ()"/>
34
35
36 </mx: Application>
37
38
39

 

 

Open Flash and create an As2 Flash
Press F9 to enter the code:

Code
Stop ();
// Declare a LocalConnection instance for swf communication with as3
// Note: Flex also generates a swf in AS3 language.
Var localConn: LocalConnection = new LocalConnection ();
Var data: Number;
LocalConn. showNum = function (num ){
Data = num; // obtain the swf parameter from as3
Trace ("yes" + data)
}
// The connection here should be the same as as3
LocalConn. connect ("flexToSwf ");

 

 

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.