Getting a null object reference error for a button instance - actionscript-3

I am trying to use this script to jump to a certain point on my timeline:
feature1_btn.addEventListener(MouseEvent.CLICK, feature1);
function feature1(event:MouseEvent):void {
gotoAndPlay(620);
}
I have the instance of my button labeled as "feature1_btn". Why am I getting this error?

You're likely getting this error because you're attempting to add the listener to the instance before the instance has been created and/or initialized. If this code is in your timeline you need to make sure that feature1_btn exists on the stage at that point in the timeline.

Related

Spark Window close function, stage becomes null

I have a Spark Window that I use as a popup window in front of my main application. I prefer this method to the PopUp Manager. However, I run into a strange error when calling the windows close() method. In the closing event handler, I have a function which does the following
public function closeHandler(event:Event):void {
this.owner.removeChild(this);
}
The window's owner is WindowedSystemManager, but at some point during the removeChild phase, the stage becomes null, and Flex throws an error trying to close the nativeWindow because it cannot find a reference to it. Two things fix this,
Instead of calling window.close(), call window.nativeWindow.close();
Do not call this.owner.removeChild(this) in the closing handler.
I'm trying to understand what is going on, does anybody have any idea??

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.

Targeting event.target instance name and not object

I have a drag and drop SWF I created and am now trying to implement a new function where I need to pass the instance and x/y coordinates to a different function outside of the startDrag and stopDrag functions. I'm trying to target the instance name by setting up a variable and storing the event.target in in the startDrag function.
However, the issue is that when I run a trace on that variable, it doesn't return the instance name. For example, I have a movieclip with an instance name of myClip1. Yet, when I run a trace on the variable, the console reports it as [object myClip1_1].
How can I instead pass the actual instance to the variable so I can use with the external function?
Update: I figured out why I was getting "object myClip1_1" as the output: somehow AS Linkage in the library got turned on. I've disabled that, but now, when I run a trace on event.target and event.currentTarget, I just get "[output MovieClip]" and not the instance name. What gives?
just add .name to event.target:
event.target.name
EDIT:
You shouldn't need the instance name to connect to the object that is being dragged. just say in the startDrag function:
currentDragObject = event.target.
currentDragObject is defined at the global scope:
var currentDragObject:MovieClip;
In this manner, currentDragObject will always be reset to the object being dragged.

flying star flash error message

I'm trying to make flying stars in flash. I decided to combine two tutorials I found (http://www.lashf.com/page/basic/running_circles_effect and http://www.republicofcode.com/tutorials/flash/motion_guide_bc/), so I started first with this "running circles" tutorial. However, I got this error "Syntax error: expecting semicolon before leftbraces".
The code in question is
onClipEvent(enterFrame){
if(random(300) == 0) {
removeMovieClip(this)
};
};
I think the problem is because the actionscript from this tutorial is actionscript 2 while I'm working in actionscript 3. So how do I code it?
You should use addEventListener() to make your MC react on something that's happening in Flash. In order to perform actions on enter frame, add a listener to Event.ENTER_FRAME. The listener should be a named function that accepts one parameter of type Event or corresponding event type. Learn more here. Practically, wherever you see a onClipEvent(*) code, separate the code in brackets into a function, and use addEventListener() to attach a listener. Note, that in order to completely remove a MovieClip instance from anywhere, you should also remove attached event listeners, so write down what events does your MCs listen and use removeEventListener() where approppriate.

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!