在Flash中加载Javascript(图片参考)
我需要帮助!我想在FlashAS3中加载一个图片url,我有下面的javascript代码来显示图片。但是我想在Flash中加载相同的引用在Flash中加载Javascript(图片参考),javascript,actionscript-3,Javascript,Actionscript 3,我需要帮助!我想在FlashAS3中加载一个图片url,我有下面的javascript代码来显示图片。但是我想在Flash中加载相同的引用 FB.api('/me?fields=username', function(response) { function detect(URL) { var image = new Image(); image.src = URL; image.onload = function() { var res
FB.api('/me?fields=username', function(response) {
function detect(URL) {
var image = new Image();
image.src = URL;
image.onload = function() {
var result = 'result'; // An example result
}
document.body.appendChild(image)
}
detect("https://graph.facebook.com/" + response.username + "/picture?width=200&height=200");
您可以使用
Loader
类在Actionscript 3中加载图像:
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
function loadImage(url:String):void
{
var l:Loader = new Loader();
var r:URLRequest = new URLRequest(url);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
l.load(r);
function onLoadError(e:IOErrorEvent):void
{
trace("Couldn't load image.");
e.target.removeEventListener(Event.COMPLETE, onLoadComplete);
e.target.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
}
function onLoadComplete(e:Event):void
{
trace("Load completed!");
e.target.removeEventListener(Event.COMPLETE, onLoadComplete);
e.target.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
stage.addChild(e.target.content); // e.target.content holds the loaded bitmap
}
}
更新
您可以使用回调和ExternalInterface
,监听来自Javascipt的调用:
if(ExternalInterface.available) // check that SWF is running in a browser
ExternalInterface.addCallback("loadImage", loadImage);
然后使用对SWF的引用从Javascript调用函数。例如,如果您的闪存是这样嵌入的:
<embed src="flash.swf" id="myFlash" width="800" height="600" />
更新1
我举了一个简单的例子,让您尝试一下,并理解我告诉您的内容:
请注意,这只适用于Firefox,因为我使用嵌入标签嵌入了它,这与Chrome有问题,但如果您正确嵌入它,它可以跨浏览器工作。您不能直接从AS3调用API,因为Facebook的库是用JavaScript编写的。您必须使用JS作为中介。不过,新闻局的上述回应是正确的。使用ExternalInterface API将消息从JS传递给AS。我将在下面的JS中粘贴您需要进行的修改,这些修改基于他所写的内容 他的回答是正确和完整的,你应该这样做
FB.api('/me?fields=username', function(response) {
function detect(URL) {
var image = new Image();
image.src = URL;
image.onload = function() {
var result = 'result'; // An example result
}
// Pasted from Gio's code above
var swf = document.getElementById("myFlash"); // get a reference
swf.loadImage(myURL); // call your AS3 function
}
detect("https://graph.facebook.com/" + response.username + "/picture?width=200&height=200");
还有..这与Java有什么关系?”“Java”与“JavaScript”的关系就像“Car”与“地毯”的关系一样。嗨,问题是我需要在JavaScript引用中加载。在本例中是url,因为这只是java脚本的一小部分。javasript从facebook的朋友那里随机获得ID。因此,我需要使用该函数加载图片,您可以从Javascript调用Actionscript函数,并将url作为参数传入。我会更新我的答案,我提供的例子正是如此。请仔细阅读。现在它加载了从flash到java的链接,,,我需要用另一种方式around@BasMienis等等,我将为您上传一个示例。它不起作用,我需要使用javascript引用在Flash AS3中加载图片。。
FB.api('/me?fields=username', function(response) {
function detect(URL) {
var image = new Image();
image.src = URL;
image.onload = function() {
var result = 'result'; // An example result
}
// Pasted from Gio's code above
var swf = document.getElementById("myFlash"); // get a reference
swf.loadImage(myURL); // call your AS3 function
}
detect("https://graph.facebook.com/" + response.username + "/picture?width=200&height=200");