As3 communicates with js

Source: Internet
Author: User

1. as and js communication instances
If as3 calls a function in JAVAscript, add it directly in.
If (ExternalInterface. available ){
ExternalInterface. call ("sendToJavaScript", value );
ExternalInterface. available mainly checks whether the player is in a container that provides external interfaces. If swf is not in the webpage, ExternalInterface. call transmits parameters to the external api.
SendToJavaScript is the function name (custom) value in js.
You can try to add
<Script type = "text/javascript">
Function sendToJavaScript (value ){
Alert (value)
</Script> in this way, the value in flash will pop up with alert, which is only the first step. Of course, we recommend that you use try to throw an error during testing.

2. js and as communication instances
Write data to a webpage
<Script type = "text/javascript">
Function thisMovie (movieName ){
If (navigator. appName. indexOf ("Microsoft ")! =-1 ){
Return window [movieName];
} Else {
Return document [movieName];
} // Judge the browser

Function sendToActionScript (value ){
ThisMovie ("flash Animation name"). sendToActionScript (value );
<Form name = "form1">
<Input type = "text" name = "input" value = ""/>
<Input type = "button" value = "Send"/> <br/>
Note: The flash Animation name includes the flash in the webpage. In objct, the id must also be the flash name.

If (ExternalInterface. available ){
Try {
ExternalInterface. addCallback ("sendToActionScript", receivedFromJavaScript );
} Catch (e: Error ){
Trace (e. message )}
} Else {
Trace ("External interface is not available for this container .");
} // End if

Note that when developing the game, we may directly send a request to end the game and use js to process the request and return a result. In this case, flash cannot read the result. Of course, you can make button request capability as event mechanism problems,
In addition, thisMovie ("flash Animation name"). sendToActionScript (value); a function must be written separately after sendToJavaScript (value) and then called.


Related Article

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: 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.