Cast loaded SWF with designed interface in main class - actionscript-3

First of all I read linked topic but unfortunately any of the tips didn't work for me: Casting a loaded SWF as an interface
My problem indeed is similiar, I have got created SWF (Content.swf) where Content.as which is document class there:
package {
import flash.display.MovieClip;
public class Content extends MovieClip implements IGame {
public function igameMethod():void {
trace("implemented method of IGame");
}
}
}
IGame interface (in the same directory as Content.as) contains sole method 'igameMethod'
package {
public interface IGame {
function igameMethod():void;
}
}
and such compiled SWF is loaded by Loader class in LoadSWF where Main.as is document class and it is something like that:
public class Main extends MovieClip {
private var swfLdr:Loader;
private var game:IGame;
public function Main():void {
if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
swfLdr = new Loader();
var r:URLRequest = new URLRequest("../lib/Content.swf");
var defaultLoaderContext:LoaderContext = new LoaderContext(true, ApplicationDomain.currentDomain);
//none of settings for ApplicationDomain doesnt' work (with checkPolicy or with or without ApplicationDomain.currentDomain set)
swfLdr.load(r,defaultLoaderContext);
this.addChild(swfLdr);
swfLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, onExternalSWFLoaded);
//tried Event.INIT too without success
}
private function onExternalSWFLoaded(e:Event) : void {
var Game:Class = ApplicationDomain.currentDomain.getDefinition( "IGame" ) as Class;
if (e.target.content is IGame) {
trace("Is IGame");
}else {
//output of this condition is false
//so e.target.content is not IGame
trace("No its not IGame");
}
var Game:Class = ApplicationDomain.currentDomain.getDefinition( "IGame" ) as Class;
//tried to cast as class without success too
var gameContent: IGame = IGame(swfLdr.content); //app breaks here
// TypeError: Error #1034: Type Coercion failed: cannot convert LoaderSWF#87f0089 to IGame
}
I've got also IGame interace (duplicated to LoadSWF project) with the same package structure (in the same folder as document class) as Main.as to let LoadSWF use igameMethod() on Content.swf, but i can not use it because of TypeError: Error #1034: Type Coercion failed: cannot convert LoaderSWF#87f0089 to IGame. So the compiler can't convert this Content to IGame.
Have you got any idea, which would help me with this topic?
Thanks for any tips in advance.
Kindest regards Pawel

Related

Avoid null object reference errors in code inside the child SWF

I'm creating a global loader that loads different SWFs.
I don't have access to the child SWFs code.
I'm trying to avoid null object reference errors in code inside the child SWF that may reference "stage" etc.
I've added all the possible event listeners, added try/catch, tried all the applicationDomains and LoaderContexts permutations. for example:
applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
var con:LoaderContext = new LoaderContext(false, applicationDomain );
con.allowCodeImport = true;
var _loader:Loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
_loader.load( new URLRequest(fileName + ".swf"), con);
I just can't catch the error or disable/remove the code from such a SWF.
for example, the following Main class:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
var h = this.stage.stageHeight;
trace(h);
}
}
}
will crush any SWF that will try to load it as part of another SWF.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main()
Any help with catching the Error, disabling/removing the code or any other idea will be much appreciated.
Thanks.
Do you have access to the code in the child swf files? If so, you can do something like this:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
if (stage) {
init();
}
else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
public function init(evt:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// You can now access the stage here
}
}
}
This will hold off initializing the child object until the stage reference is set. For all child objects the constructor will be called first before the stage is set, so you can't rely on it being there in the constructor.

AS3 | 1120: Access of undefined property stage

My goal is to create rectangle as MovieClip with stage size, but Flash gives me this error: 1120: Access of undefined property stage. (on line 6,7,14)
My code:
package {
import flash.display.MovieClip;
public class main {
var mc_background:MovieClip = new MovieClip();
var stageW:Number = stage.stageWidth;
var stageH:Number = stage.stageHeight;
public function main() {
drawBackground();
}
public function drawBackground():void {
mc_background.beginFill(0xFF00CC);
mc_background.graphics.drawRect(0,0,stageW,stageH);
mc_background.graphics.endFill();
stage.addChild(mc_background);
}
}
}
i had a similar problem, the thing is, the stage hasn't really been setup yet, so you need to wait to get data from it or stuff in it. just add this:
protected function addedToStageHandler(event:Event):void
{
//do stuff
}
protected funcion init():void
{
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
//more stuff
}
hope it helps
The stage property of an object isn't defined until the object has been added to the Stage or another object on the Stage.
The constructor of a class is called when the class instance is created, and that is before the instance could have been added to the Stage. So, you can't access stage within code you call from the constructor, or when you define the instance variables stageW and stageH.
To access the stage property as soon as the object is added to the stage, allow the object to handle the ADDED_TO_STAGE event:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class main
{
var mc_background:MovieClip = new MovieClip();
public function main()
{
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler(event:Event):void
{
// Generally good practice to remove this listener from the object now because it stops addedToStageHandler from being called again if the object is removed and added back to the stage or display list.
removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
drawBackground();
}
private function drawBackground():void {
mc_background.beginFill(0xFF00CC);
mc_background.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
mc_background.graphics.endFill();
addChild(mc_background);
}
}
}

Flash AS3 Loadmovie Error #1009 - External AS file

We have created a program in Flash that uses an external actionscript file.
I'm trying to load/import this swf in another flash file but I'm getting a 'Error #1009: Cannot access a property or method of a null object reference'
I know this is to a reference in my external .as file but I'm not sure how to fix this
This is my code to load (and scale) the swf
var ld:Loader = new Loader();
addChild(ld);
ld.load(new URLRequest("tatton.swf"));
ld.contentLoaderInfo.addEventListener(Event.COMPLETE,function(evt) {
var mc = evt.target.content;
mc.scaleX=0.7031;
mc.scaleY=0.7031;
});
Anyone have any ideas?
Thanks
Edit: here is the full error code: TypeError: Error #1009: Cannot access a property or method of a null object reference. at Tatton()
And below is my .as file code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.setTimeout;
import flash.utils.clearTimeout;
import flash.utils.getDefinitionByName;
public class Tatton extends MovieClip
{
public var active_clip:MovieClip;
public var to:Number;
public function Tatton()
{
// Create empty vector of movieclips;
stage.addEventListener(MouseEvent.CLICK, reset_timeout);
init();
}
public function reset_timeout(e:Event = null)
{
clearTimeout(to);
// 3 mins
to = setTimeout(fire_timeout, 150 * 1000);
}
public function fire_timeout():void
{
// Reset the system (and run attractor) if we're not on the attractor already.
if ( !(active_clip is Attractor))
{
init();
}
}
public function init()
{
// Reset globals
Globals.skip_menu_anim = false;
Globals.skip_staff_menu = false;
load_movie("Attractor");
reset_timeout();
}
public function load_movie(name:String):void
{
if (active_clip is MovieClip)
{
kill_active_movie();
}
active_clip = createInstance(name);
addChild(active_clip);
active_clip.startup();
}
public function kill_active_movie():void
{
active_clip.shutdown();
removeChild(active_clip);
active_clip = null;
}
public function createInstance(className:String):Object
{
var myClass:Class = getDefinitionByName(className) as Class;
var instance:Object = new myClass();
return instance;
}
}
}
try this
public function Tatton() {
addEventListener(Event.ADDED_TO_STAGE, stageAvailable);
}
private function stageAvailable(e:Event = null) {
removeEventListener(Event.ADDED_TO_STAGE, stageAvailable);
// Create empty vector of movieclips;
stage.addEventListener(MouseEvent.CLICK, reset_timeout);
init();
}
refer to this article to understand why
You need to add the event listener after you call Loader.load because contentLoaderInfo is null until load is called.

Actionscript 3: Access movieclips from array with for-loop

I am having some trouble with accessing the movieclips I've added as childs. From what I've read, a way to solve this is to add every new movieclip to an array, and then loop through this array when I want to change something about all the movieclips.
In my case I want to scale them.
This is how I've tried to implement this function:
package
{
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.events.MouseEvent;
import Airport;
public class Main extends Sprite
{
public var tile:MovieClip
public var bg_image:Sprite;
public var airport:Airport;
public static var airportDict:Dictionary = new Dictionary();
public static var collectedAirportArray:Array = new Array();
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// airportDict is being filled up here.. but that's not really relevant for my problem
bg_image = new Image();
addChild(bg_image);
bg_image.addEventListener(MouseEvent.CLICK, testfunction);
for (var k:Object in airportDict)
{
var airport:Airport = new Airport(k,airportDict[k]["x"], airportDict[k]["y"]);
collectedAirportArray.push(collectedAirportArray);
bg_image.addChild(airport);
}
}
private function testfunction(evt:MouseEvent):void
{
for each (tile in collectedAirportArray)
{
tile.scaleY = 2 * tile.scaleY;
}
}
}
}
This give me the error message TypeError: Error #1034: Type Coercion failed: cannot convert []#30977eb1 to flash.display.MovieClip.
at Main/testfunction() when clicking on the bg_image
You have an error in where you are making the array. You have there collectedAirportArray.push(collectedAirportArray); - you're basically pushing an array into itself. Nice snakey, they said. You should instead push the newly created airport in there:
collectedAirportArray.push(airport);
If your airport is any kind of DisplayObject your code should work.
What AirPort class is extending? If it extends MovieClip then try type casting like so:
private function testfunction(evt:MouseEvent):void
{
for each (tile in collectedAirportArray)
{
var myTile:MovieClip = MovieClip(tile);
myTile.scaleY = 2 * myTile.scaleY;
}
}

Flash TypeError: Error #1009: Cannot access a property or method of a null object reference. - when accessing TLFTextField from Document Class

I'm lost on this one. I receive a TypeError: Error #1009: Cannot access a property or method of a null object reference. output message the first time my Document Class tries to access a simple textfield on the stage (added from the IDE, not actionscript)
package {
import flash.display.*;
import fl.text.*;
import flash.text.*;
import flash.events.*;
import flash.net.*;
public class Main extends MovieClip {
private var _netConnection:NetConnection;
private var _responder:Responder;
/* some other public + private vars */
public function Main() {
init();
}
public function init(e:*=null):void {
_netConnection = new NetConnection();
_responder = new Responder(uponResult);
txt.text = "init()";
}
/* more functions */
}
}
I tried adding txt.addEventListener(Event.ENTER_FRAME, init); incase the txt TLFTextField wasn't ... there... at the beginning, but it still outputs the error.
I feel like a bit of an idiot atm, what's the prognosis doc?
JB
TLFTextFields are weird creatures, I've had a lot of issues with them recently.
I'd try to use the Event.ADDED_TO_STAGE event because the TLFTextFields have to be on the stage when you try to access them:
public function Main() {
addEventListener(Event.ADDED_TO_STAGE, init);
};
public function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
txt.text = "init()";
};
It should work if your TLFTextField is on the first frame on the Main Timeline.
Let me know if this one does the magic,
Rob