Adobe Flash ActionScript 3.0 ERROR 1009 When using UILoader [duplicate] - actionscript-3

This question already has answers here:
as3 #1009 error code provided. "Null Object reference"
(2 answers)
Closed 6 years ago.
I did a lot of search and can't find the answer, please kindly help me.
So I created one flash game called Snake Games and then another Flash which I use UILoader to load the snake game, however when I try to run that Flash game and check whether that flash will load the snake game, I encountered error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/addFood()
at Main/init()
at Main()
Here's the Main.as script inside the Snake Games
http://pastebin.com/raw/Z5ZBtLQn
Please kindly help me.
Thank you.

In your case the source of null error is "Stage is unavailable". You need to alter your snake game constructor to first check stage, then call init() like this:
public function Main(){
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE,init);
}
function init(e:Event=null):void {
removeEventListener(Event.ADDED_TO_STAGE,init);
// rest of init() code

Related

Cannot convert root to movieclip

I have this code in a Movieclip that is called onto the stage in the main timeline
if(MovieClip(root).isWithinRange(MovieClip(e.currentTarget), MovieClip(root).hero, 10))
{
if(e.currentTarget.getStatus() == 0)
{
e.currentTarget.unlock();
}
}
And it gives me an error at MovieClip(root). I tried tracing that and it gave me the same error.
Type Coercion failed: cannot convert flash.display::Stage#4e131e9 to flash.display.MovieClip.
Tracing root gives me Object Stage.
So I can't convert the Stage object to a MovieClip, but when I try to skip the cast, it can't use the method because it says its a static type. How do I access this method?
That is because stage is DisplayObject and not MovieClip. So you can convert it into MovieClip:
var stageRef:MovieClip = this.parent as MovieClip;
if object was placen on stage - stage.addChild(yourObject);
or you can acces it via DisplayObject (make sure to import flash.display.DisplayObject;)
DisplayOject(stage)["nameOfUrObject"];
The true answer here is that Stage IS NOT MovieClip. Period.
You cannot do what you wanted to do and I clearly can't understand where this isWithinRange method came from? If you want answers, ask specific questions. I already answered this one, unfortunately it won't help you much :)
p.s.
After reading all comments - here's a link that may help you: AS3 - Call function in root timeline from class
Keeping everything on timeline is a very very bad approach (sorry to say it).

referencing a movieclip symbol on stage

I am sure this is a very basic question but I just could not find out how it is done (all tutorials I read have this step skipped).
I am working on Adobe Flash CS4, AS3. I have a movieclip symbol on stage, and I have assigned it with an instance name "myClip".
Then, on frame 10, I want to change its position, say,
myClip.x = 10;
but I only got an error message "Cannot access a property or method of a null object reference." What have I missed?
Thanks a lot!
on timeline myClip layer on frame 10 right click >> insertframe..

in what situation,gotoAndStop is not working

I have 3 frames, I don't show the whole code, it's too huge.and the main code is
gotoAndStop(2);
trace('frame:',currentFrame)
the output should be frame: 2
But in fact it's frame: 1, and objects in frame 2 cannot be loaded and become null
no compiler errors
When I delete some codes after it, the application sometimes operate right and stops at frame 2.
This shouldn't happen as any code after it should not involve the output
although I can solve this when I delete the first frame,but it's quite risky to keep developing.
Any ideas why this happen?
Before keep going,I should mention I actually have compiler errors due to null objects,but it's not the main point.
And I have a class called host,the code gotoAndStop is also in the first place of constructor.
I have put override function in host, and the output is
stopping at frame: 2 Called from: Error
at host/gotoAndStop()
at host()
frame: 1
TypeError: Error #1009: Cannot access a property or method of a null object reference
at host()
Then I tried the method2 Creative Magic says,the result shows
stopping at frame: 2 Called from: Error
at host/gotoAndStop()
at host()
displaying frame: 1
frame: 1
TypeError: Error #1009: Cannot access a property or method of a null object reference
at host()
displaying frame: 2
This quite confuses me what's happening in the frame,I wonder the cause is like you say old version of SDK,thanks for help
Your problem can be either you've written buggy code or you use old Flash SDK.
In the first case you should check if you're calling gotoAndStop(), play() or gotoAndPlay() methods of your MovieClip. To do so you can edit the class of your MC:
In Flash Professional in the library panel right-click on the MC
Select "Edit Class"
In your selected IDE add following function
override public function gotoAndStop(frame:Object, scene:String = null):void
{
trace("stopping at frame:", frame, "Called from:", new Error().getStackTrace());
super(this).gotoAndStop(frame, scene);
}
It will trace when the gotoAndStop() method was called and who called it. It will let you see if the frame was set to frame by something else. I recommend you to override play() and gotoAndPlay() functions as well.
If you're intimidated by this code you could just add a trace() on each frame of your MC, since it's only 3 frames it shouldn't be too much work. Your trace could look something like that:
trace("displaying frame:", this.currentFrame);
The other possible cause could lie within the SDK you're using. I, myself had a strange bug when the loaded MC wouldn't listen to stop(), gotoAndStop() and other functions. The problem was solved by upgrading the SDK: http://www.adobe.com/devnet/flex/flex-sdk-download.html
The explanation on how to replace the old SDK is there as well.

Citrus Engine Flash Game not compiling correctly

Im developing a flash game based on the citrus engine for a uni project.
All of it is done and handed in but im trying to compile the entire project into a release for web.
In flahs builder ive gone file --> export --> release build and compiled the game.
the .swf file opens up fine and initiatze the spirte menu but when clicking the start game button it begins to initiate the game state but then hangs up on a solid colour, in flash debugger im getting these errors
SecurityError: Error #2000: No active security context.
Started
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.citrusengine.utils::ObjectMaker$/FromMovieClip()
at GameState/initialize()
at com.citrusengine.core::CitrusEngine/handleEnterFrame()
SecurityError: Error #2000: No active security context.
Started
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.citrusengine.utils::ObjectMaker$/FromMovieClip()
at GameState/initialize()
at com.citrusengine.core::CitrusEngine/handleEnterFrame()
Any suggestion appreciated
Error #2000 is usually a file not found error. You can get more info from running an IOErrorEvent like so:
myLoader.addEventListener(IOErrorEvent.IO_ERROR, IOError)
function IOError(e:IOErrorEvent):void {
trace(e.text);
}
Likely an issue with pathing to the correct file.
Error #1009 is likely a domino effect of not being able to work on the asset that hasn't been loaded due to the IOError. It could also be an issue if you're loading other SWFs to the stage and those child SWFs attempt to utilize the stage before it's ready, in which case you'd want to only start your scripts until after the addedToStage event has fired. You can set that up like so:
if (this.parent is Stage) {
stageReady();
} else {
addEventListener("addedToStage", stageReady);
}
function stageReady(e:Event = null):void {
// begin your setup code here.
}
The logic here being that if your swf is not encapsulated inside a loader, the parent object should be the stage, otherwise, you can safely add a listener to the loaded swf's timeline that listens for the addedToStage event.
Maybe the following will resolve your problem:
Wrap all the init code to a custom function (lets say: initFunctionOfApplication). Set a delay timer before this init function is called. It's a issue I've had before, and got it fixed with a small delay... Maybe this will fix your problem.
setTimeout(function():void{initFunctionOfApplication();}, 3000);
Keep us posted!

Flash crash (ends up in a restart loop) when loading an external swf

Im working with FlashDevelop and have two main projects, all pure AS3 projects.
When trying to load my second project from my main project I get all kinds of errors.
The Main class of the main project extends Sprite and the Main class in the "to-be-imported" project extends MovieClip. Looking at the loading of the swf in the debug window in FD it all seems fine:
[SWF] 'pathToSwf'\secondProject.swf - 410 626 bytes after decompression.
If i try to assign the loaded swf to a newly created MovieClip I get a coercion failiure:
swfContent = loader.content; // =>
Type Coercion failed: cannot convert Main#46c0201 to flash.display.MovieClip.
So, typecasting the loaded content like so:
swfContent = loader.content as MovieClip;
removes that error but then I fall into the next pit as I try to call addChild:
Error #2007: Parameter child must be non-null.
Trying to get around the issue I tried to add the loader directly into the container where I want to show the external swf. This is when the real interesting problems begin:
targetContainer.addChild(loader);
My main application now hang, restarting in a never ending loop. I have no idea why..
So my issue is really. How can my external swf be loaded but then again be null.
It works perfectly fine when I run the external swf by itself...
Use getQualifiedClassName and getQualifiedSuperclassName functions (and even describeType if you must) on loader.content to get its exact type information.
loader.content as MovieClip returns null because loader.content is not a MovieClip - casting with as keyword silently returns null when it fails. Is there any chance that the loaded content is an AS2 movie clip instead of AS3 movie clip? In that case getQualifiedClassName will return "AVM1Movie".
The latter issues is weird, but first try changing the type of swfContent to Sprite. A main class does not always extend MovieClip, and judging from the error message it indeed doesn't in this case.
Your swfContent will be null, if it cannot be casted to MovieClip. That is how the as operator is supposed to work when type coercion fails.
Modify your assignment operation like this:
var swfContent :MovieClip = MovieClip(loader.content);
You might want to encompass the assignment in a try...catch block, as an error will be thrown in case of failure, instead of swfContent being set null, as with as.
So the problem was that the main class of my loaded swf had the same name as the swf I was loading from. This led to that when flash tries to execute the loaded swf it actually calls the parent MAIN class which results in the looping behaviour.
To avoid this DHuntrods suggested to change the application domain which solved the issue.
loader = new Loader();
var AD:ApplicationDomain = new ApplicationDomain( null );
var context:LoaderContext = new LoaderContext( false, AD );
loader.load(new URLRequest(path), context);