package { import flash.display.Sprite; import flash.display.Loader; import…
- User
package { import flash.display.Sprite; import flash.display.Loader; import flash.text.TextField; import flash.events.Event; import flash.display.StageAlign; import flash.events.KeyboardEvent; import flash.desktop.NativeApplication; import flash.events.InvokeEvent; import flash.display.StageDisplayState; import flash.ui.Keyboard; import flash.geom.Rectangle; import flash.display.Screen; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.display.StageQuality; import flash.system.LoaderContext; import flash.system.ApplicationDomain; import flash.events.IOErrorEvent; import flash.net.URLVariables; import flash.net.URLRequest; import background.Background;
public class GameLoader extends Sprite { private var loader:Loader; private var locale:String; public var log:TextField; private var fullScreen:Boolean; private var _background:Background; public function GameLoader() { if (stage) { this.init(); } else { addEventListener(Event.ADDED_TO_STAGE, this.init); }; } private function init(e:Event=null):void { removeEventListener(Event.ADDED_TO_STAGE, this.init); stage.align = StageAlign.TOP_LEFT; stage.frameRate = 60; stage.nativeWindow.maximize(); stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKey); this.setCenterPosition(); this.configureStage(); this.createBackground(); this.log = new TextField(); this.log.x = 15; this.log.y = 15; this.log.width = 520; this.log.height = 370; this.log.background = true; this.logEvent("aaa"); try { NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, this.onInvoke); } catch(e:Error) { logEvent(e.getStackTrace()); throw (e); }; } private function configureStage():void { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; stage.quality = StageQuality.LOW; stage.stageFocusRect = false; mouseEnabled = false; tabEnabled = false; } private function createBackground():void { this._background = new Background(); stage.addChild(this._background); } private function removeFromStageBackground():void { if (stage.contains(this._background)) { stage.removeChild(this._background); }; } private function onKey(e:KeyboardEvent):void { switch (e.keyCode) { case Keyboard.F11: this.fullScreen = (!(this.fullScreen)); if (this.fullScreen) { stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE; } else { stage.displayState = StageDisplayState.NORMAL; }; break; }; } private function setCenterPosition():void { var appBounds:Rectangle = stage.nativeWindow.bounds; var screen:Screen = Screen.getScreensForRectangle(appBounds)[0]; stage.nativeWindow.x = ((screen.bounds.width - stage.nativeWindow.width) / 2); stage.nativeWindow.y = ((screen.bounds.height - stage.nativeWindow.height) / 2); } private function onInvoke(event:*):void { if (event.arguments.length > 0) { this.logEvent(("Arguments: " + event.arguments.toString())); this.locale = event.arguments[0]; }; var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain); this.loader = new Loader(); this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onComplete); this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function (e:IOErrorEvent):void { }); var flashvars:URLVariables = new URLVariables(); flashvars["locale"] = this.locale; flashvars["rnd"] = Math.random(); var urlReq:URLRequest = new URLRequest("battles.swf"); urlReq.data = flashvars; this.loader.load(urlReq, context); } private function onConnectError(event:Event=null):void { Alert.showMessage("Can not connect to server!"); stage.nativeWindow.visible = true; } public function logEvent(entry:String):void { this.log.appendText((entry + "\n")); trace(entry); } private function onComplete(e:Event):void { this.loader.removeEventListener(Event.COMPLETE, this.onComplete); this.removeFromStageBackground(); var mainClass:Class = Class(this.loader.contentLoaderInfo.applicationDomain.getDefinition("Game")); var obj:* = new (mainClass)(); obj.SUPER(stage); addChild(obj); } }}
как сделать чтобы когда не удалось получиться battles.swf вызывалась функция onConnectError
- Assistant
Вы можете добавить обработчик события
IOErrorEvent.IO_ERRORдляloader.contentLoaderInfoв методеonInvoke. В этом обработчике вы можете вызвать функциюonConnectError. Вот, как это можно сделать:- В методе
initизмените следующую строку:
this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function (e:IOErrorEvent):void { });на:
this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onConnectError);- Добавьте метод
onConnectErrorв классGameLoader, который будет обрабатывать ошибку при загрузкеbattles.swf:
private function onConnectError(event:IOErrorEvent):void { // Ваш код для обработки ошибки }Теперь, если возникнет ошибка загрузки файла
battles.swf, будет вызываться функцияonConnectError. Вы можете поместить в эту функцию код для вывода сообщения об ошибке или выполнение других действий. - В методе
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.