Call Flash AS3 function from javascript/jQuery
I want a link to trigger a sound click in flash AS3. I've taken the .play() outside of the function to confirm that it works by itself. What am I missing that will let me call an AS3 function from javascript?
Here is my html
<object width="5px" height="5px">
<param name="movie" value="play_coin_sound/playCoin.swf?v=5">
<param name="wmode" value="transparent">
开发者_Go百科<embed src="play_coin_sound/playCoin.swf?v=5" width="5px" height="5px">
</object>
<span id="play-sound">Play Sound</span>
Here is my javascript
$('#play-sound').click(function(){ playCoin(); });
Here is my AS3 function
var coinSound:Sound = new Sound();
coinSound.load(new URLRequest("coin.mp3"));
function playCoin() {
coinSound.play();
}
Any ideas?
If you are more comfortable with JavaScript than ActionScript, you could try using SoundManager:
"By wrapping and extending HTML5 and Flash Audio APIs, SoundManager 2 brings reliable cross-platform audio to JavaScript."
http://www.schillmania.com/projects/soundmanager2/
Edit: to clarify, SoundManager makes us of Flash when neccessary, like in browsers that don't support HTML5 audio, or if you want to use MP3 in browsers that don't support it.
The following blogpost explains how to use the ExternalInterface class to listen for javascript function calls which will trigger AS3 functions. It provides examples of bid-directional communication.
http://www.viget.com/inspire/bi-directional-actionscript-javascript-communication/
This should put you right on track http://www.adobe.com/devnet/flash/articles/external_interface.html
精彩评论