是的,可以使用JavaScript从Flash applet拖放到外部。这可以通过以下步骤实现:
以下是一个简单的示例代码:
Flash applet代码:
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.external.ExternalInterface;
var dragSprite:Sprite = new Sprite();
dragSprite.graphics.beginFill(0xFF0000);
dragSprite.graphics.drawRect(0, 0, 50, 50);
dragSprite.graphics.endFill();
addChild(dragSprite);
dragSprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
function onMouseDown(event:MouseEvent):void {
dragSprite.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
function onMouseUp(event:MouseEvent):void {
dragSprite.stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
ExternalInterface.call("onDrop", dragSprite.x, dragSprite.y);
}
ExternalInterface.addCallback("getPosition", getPosition);
function getPosition():String {
return dragSprite.x + "," + dragSprite.y;
}
JavaScript代码:
var swf = document.getElementById("mySwf");
swf.onDrop = function(x, y) {
alert("拖放到外部,坐标:" + x + "," + y);
};
swf.getPosition = function() {
return swf.x + "," + swf.y;
};
在上面的示例中,Flash applet中的一个Sprite对象被设置为可拖动,当拖放事件触发时,使用ExternalInterface类将拖放的坐标发送给JavaScript。在JavaScript中,接收Flash applet传递的坐标,并弹出一个提示框来显示拖放的坐标。
领取专属 10元无门槛券
手把手带您无忧上云