是的,您应该手动将 MouseEvent.CLICK 事件发送到您的舞台:
var yourX:Number = 40; //Whatever you want
var yourY:Number = 40; //Whatever you want
//Make sure you have a link to the stage (usually after Event.ADDED_TO_STAGE event occured)
stage.dispatchEvent(new MouseEvent(
MouseEvent.CLICK,
true,
false,
yourX,
yourY);
如果您需要某个 DisplayObject 与此 MouseEvent 相关联,则应向 MouseEvent 构造函数传递一个附加参数:
var yourX:Number = 40; //Whatever you want
var yourY:Number = 40; //Whatever you want
//Make sure you have a link to the stage (usually after Event.ADDED_TO_STAGE event occured)
stage.dispatchEvent(new MouseEvent(
MouseEvent.CLICK,
true,
false,
yourX,
yourY,
linkToYourRelatedDisplayObject);
==更新==
嗯,我成功了。有点“黑客”,就在这里。您的 youtube 播放器包含一个 safeLoader 对象,该对象包含一个 videoApplication,其中包含一个 LargePlayerButton。这个 LargePlayerButton 有一个 MouseEvent.CLICK 事件监听器,所以我们需要将事件分派给这个按钮,而不是分派给舞台。这是带有 autoClick 模拟的完整代码:
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.DisplayObjectContainer;
// The player SWF file on www.youtube.com needs to communicate with your host
// SWF file. Your code must call Security.allowDomain() to allow this
// communication.
Security.allowDomain("www.youtube.com");
// This will hold the API player instance once it is initialized.
var player:Object;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
function onLoaderInit(event:Event):void {
addChild(loader);
loader.content.addEventListener("onReady", onPlayerReady);
}
function autoClick():void
{
//=========================
//Some nested children, we need to dig through a bit to get to the LargePlayButton
var safeLoader:DisplayObjectContainer = (loader.content as DisplayObjectContainer).getChildAt(0) as DisplayObjectContainer;
var videoApplication:DisplayObjectContainer = safeLoader.getChildAt(0) as DisplayObjectContainer;
var largePlayBtn:DisplayObjectContainer = videoApplication.getChildAt(6) as DisplayObjectContainer;
//=========================
//And finally dispatching our event to this button. It will think that a person has clicked it
largePlayBtn.dispatchEvent(new MouseEvent(MouseEvent.CLICK,
true,
true,
stage.stageWidth / 2,
stage.stageHeight / 2));
}
function onPlayerReady(event:Event):void {
// Event.data contains the event parameter, which is the Player API ID
trace("player ready:", Object(event).data);
// Once this event has been dispatched by the player, we can use
// cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
// to load a particular YouTube video.
player = loader.content;
// Set appropriate player dimensions for your application
player.setSize(300, 250);
player.cueVideoById("AfTCtRDsWVw",0);
//====================================================
//As long as player is loaded we can call our function
autoClick();
}
这是一个很好的练习,干杯:)