Use LocalConnction to communicate with SWF

Source: Internet
Author: User
Tags cdata

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?
  1. <? 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 ;}}
  2. ]> </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?
  1. <? 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>

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.