You can use the LocalConnection class to create a LocalConnection object, which can call methods in another LocalConnection object between one SWF file or multiple SWF files. Local connections allow communication between SWF files. LocalConnection objects can only communicate with SWF files running on a client computer, but these SWF files can run in different applications. For example, one SWF file runs in the browser, and the other SWF file runs in the file application.
Communication between two SWF files. a swf file is called a sender SWF file, which contains the method to call. The sender SWF file must contain a LocalConnection object and call the send () method. Another SWf file is called the receiver SWF file. This file is the file that calls the method. The receiver SWF file must contain another LocalConnection object and call the connect () method.
Simple Example:
The sender SWF initializes LocalConnection () after the Application is created and listens to the communication status. The send () method is triggered by the btnSend button.
Conn. send ("LocalConnectionTest", "testHandler", "Hello World! "); LocalConnectionTest is the LocalConnection of the receiver SWF, and testHandler is a method of the receiver SWF, followed by the parameter passed to testHandler.
Show row number copy code?
<? Xml version = "1.0" encoding = "UTF-8"?> <Mx: Application xmlns: mx = "http://www.adobe.com/2006/mxml" layout = "absolute" creationComplete = "Init ()"> <mx: Script> <! [CDATA [import flash.net. localConnection; private var conn: LocalConnection; public function Init (): void {conn = new LocalConnection (); conn. addEventListener (StatusEvent. STATUS, onStatus);} public function Send (): void {conn. send ("LocalConnectionTest", "testHandler", "Hello World! ");} Private function onStatus (event: StatusEvent): void {switch (event. level) {case "status": trace ("send () to); break; case" error ": trace (" send () lost); break ;}}
]> </Mx: Script> <mx: Canvas> <mx: Button id = "btnSend" label = "test click =" Send () "/> </mx: canvas> </mx: Application>
The receiver SWF first initializes LocalConnection () and creates LocalConnection () named LocalConnectionTest through the connect () method. The name must be unique. A parameter error is reported when the same name is created in the rough form. TestHandler () is called by the sender SWF.
Show row number copy code?
<? Xml version = "1.0" encoding = "UTF-8"?> <Mx: Application xmlns: mx = "http://www.adobe.com/2006/mxml" layout = "absolute" creationComplete = "Init ()"> <mx: Script> <! [CDATA [import flash.net. localConnection; private var conn: LocalConnection; public function Init (): void {conn = new LocalConnection (); conn. client = this; try {conn. connect ("LocalConnectionTest");} catch (error: ArgumentError) {trace ("Wf pair named ocalConnectionTest);} public function testHandler (msg: String): void {lbInfo. text = "Receiver Info:" + msg;}]> </mx: Script> <mx: Label id = "lbInfo"/> </mx: Application>