0
我想使用LocalConnection在三個swf加載到單獨的broswer窗口(全部由最初打開的頁面打開)之間建立通信。我是跨swf溝通的新手,並且正在教程中發現here。AS3 - 本地連接似乎不起作用
我從一個xampp服務器運行這些測試。
本質上,我有3個swfs。我有一個'主'瑞士法郎和兩個'奴隸'瑞士法郎。主swf啓動時,它會在新窗口中打開其他swfs。當主人點擊一個按鈕時,我正在嘗試更新其中一個從服務器中的某些文本。更復雜的東西將在稍後發生,我只是想先弄清楚LocalConnections。
在 'master.swf' 的代碼(在時間軸上):
import flash.net.URLRequest;
import flash.events.Event;
//Used as a flag to open each slave window, one of the first frame, one on the second frame, as using navigateToURL twice in a row doesn't seem to work.
var frameCount = 0;
//This is what I'm attempting to send over the LocalConnection.
var messageText = "it worked!";
var slave1url:URLRequest = new URLRequest("slave1.html");
var slave2url:URLRequest = new URLRequest("slave2.html");
//Creates a one-way connection to communicate with JUST slave1
var slave1OutboundConnection:LocalConnection = new LocalConnection();
this.addEventListener(Event.ENTER_FRAME, Update);
slave1Button.addEventListener(MouseEvent.CLICK, SendMessageToSlave1);
slave1Button.buttonMode = true;
//Send a message to slave1 via the slave1OutboundConnection
function SendMessageToSlave1(e:Event){
slave1OutboundConnection.send("masterToSlave1Connection", "UpdateTextFromCommunication", messageText);
}
function Update(e:Event){
//Open the slave1 page on the first frame, and the slave2 page on the second frame.
if(frameCount == 0){
navigateToURL(slave1url, "_blank");
frameCount++;
} else if (frameCount == 1){
navigateToURL(slave2url, "_blank");
frameCount++;
}
}
在SLAVE1:
var masterInboundConnection:LocalConnection = new LocalConnection();
masterInboundConnection.allowDomain("*");
masterInboundConnection.connect("masterToSlave1Connection");
function UpdateTextFromCommunication(receivedArguments){
displayText.text = receivedArguments;
}
我也得到了自定義的一點點的JavaScript中的每個HTML頁面設置頁面大小和位置,但我不認爲這應該很重要:
<script>
//Get the width of the user's monitor
var resolutionWidth = window.screen.availWidth;
//Get the height of the user's monitor (minus the height of the task bar)
var resolutionHeight = window.screen.availHeight;
//First screen is 0, second is 1, third is 2, etc.
var screenNumber = 2;
//If this screen is not displayed on the primary monitor, add 40 pixels for the lack of the taskbar on other monitors
if(screenNumber > 0){
resolutionHeight += 40;
}
//Maximize the window
this.resizeTo(resolutionWidth, resolutionHeight);
//Move the window to the appropriate display
this.moveTo(resolutionWidth * screenNumber, 0);
</script>
當我點擊按鈕是應該的觸發溝通,似乎沒有任何事情發生。
我在Windows 7的本地主機xampp服務器上運行IE11中包含這些swf的html頁面。在CS5.5中創建Swfs。