AS3 movie instance turning null - actionscript-3

I'm banging my head on what seems like a simple as3 problem. I have a flash chart that contains a series of buttons that go to different parts of the timeline on Roll_over.
so for example - the "Market maneuvers" button looks like this
marketManeuversButton.addEventListener(MouseEvent.ROLL_OVER, marketManeuversButtonReaction)
and the function it calls looks like this
function marketManeuversButtonReaction (event:MouseEvent):void{ gotoAndStop('18'); }
The problem is, when I mouseover that button (and many others), it goes to frame '18' and then throws this error:
Error #1009 Cannot access a property or method of a null object
reference
here is my flash file
Any help would be appreciated. Thanks.

When you change frame, flash recreated all objects in frame, and you loose all your data.
Yes, it's simple AS3 problem, just don't use a scene frames at all. Program all in classes, don't use any frames to code any logic except stop(), gotoAndStop(), gotoAndPlay().
In your problem, put all scene in movieclip, exclude control buttons from it to another movieclip and control scene movie clip with control movieclip >____<. It pegleg. Next time just do it right, don't use scene frames.

Related

How do you move to another scene in actionscript 3 using buttons

I'm trying to use a button to skip from one scene to the first frame from the next scene in Flash CS6, however I keep getting the
error 2108: the scene was not found.
stop();
btnNext.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextScene);
function fl_ClickToGoToNextScene(event:MouseEvent):void {
gotoAndPlay(1, 'scene2');
}
This is my code and ''scene2'' is spelled right, how can I correct this?
Checking the API for gotoAndPlay(), it is an overloaded argument which accepts either a String for the label, or an int for the frame. Furthermore, if a second argument is provided, it points to the frame number in the target scene. A Scene should not be confused with a frame Label. Scenes are like multiple MovieClip timelines, each with their own first frames. According to Adobe,
Using scenes is not the best approach because of a number of drawbacks...
If you are using the automatically generated names, then it should be Scene 2, and not scene2. Furthermore, although your command will work, it will appear to have done nothing unless you also add stop() to the first frame of your new scene.
Alternatively, you could just switch to the gotoAndStop() which won't cause your playhead to jump back to the first scene.
gotoAndStop(1, "Scene 2");

ActionScript 3: Jump to a specific frame and target a MovieClip in that frame

I'm trying to do some stuff in Flash. Let's say I have a MovieClip called mcShape with three frames. In each frame there is a different shape that masks another movie clip, in another layer, that spans for the said three frames. That movie clip is called mcColor and defines the color seen through the shape, thus rendering the desired shape in the desired color:
From the main timeline, on the frame where I have mcShape, I've written the following code:
mcShape.gotoAndStop(some frame);
mcShape.mcColor.gotoAndStop(some other frame);
The thing is this doesn't work. When I don't change mcShape frame, everything works fine, but when I gotoAndStop some other mcShape frame, mcColor is not detected anymore and I get Error #1009: Cannot access a property or method of a null object reference. Even if I put three different frames with a different instance of mcColor on that frame, it doens't work.
Are there any workarounds to this? What am I doing wrong? Thank you very much!
Updating to Adobe Animate seemed to solve the problem.

how to set actionscript code to execute only once

Okay so I have a movieclip called a_mc, if you click the movieclip, it goes to frame 5, and then on frame 5 there is a button called close_btn where if you click the button, it goes back to frame 1 and it is supposed to make a_mc invisible. Here is the actionscript code for frame 1.
stop();
a_mc.addEventListener(MouseClick.CLICK, aClicked);
function aClicked(event:MouseEvent):void {
gotoAndStop(5);
}
and on frame 5, the actionscript code is
stop();
close_btn.addEventListener(MouseEvent.CLICK, closeCLicked);
function closeClicked(event:MouseEvent):void {
gotoAndStop(1);
a_mc.visible = false;
a_mc.removeEventListener(MouseEvent.CLICK, aClicked);
}
see, the problem is, in frame 5, I make a_mc invisible and remove the event listener and go back to frame 1 and on frame one, it always executes the actionscript code so it again creates the event listener and makes a_mc visible. Any idea on how to stop this from happening?
I tried putting the code from frame 1 into a package and then a class and then a constructer method but it is saying
"Syntax error: package is unexpected"
Could you put all the code that you want to execute once in frame 1? - don't call stop() and let it run to the next frame.
Then put the rest of your code in other key frames and don't use gotoAndStop(1) so frame 1 is only called once?
You can try not removing the event listener on a_mc in frame 5, and then in frame 1 check if the event listener is already present (a_mc.hasEventListener()) as a signal that frame 1 has already been shown. Not exactly a 'bets practices' solution, but it might work.
Unfortunately, depending on the actual conents of those clip, and what happens in other frames, it may be the problem you're having is a consequence of the way movieclip object works in flash. When a frame is changed, flash instantiates new objects on the stage (added in new frame), and removes the ones not needed anymore (depending on the contents, but generally it's true). The 'a_mc' object that you manipulate in frame 5 may not be the same 'a_mc' object that is on the stage when you go back to frame 1. It may have been deleted and reinstantiated in the meantime.
To avoid things like that, it would be a better solution to have controlling code in a class outside of the timeline of the animating clip, or at least to keep the state in a separate object. I work in Flash Builder so I can't help you with the details of such organization in Flash Pro (which I presume you're using), but you could probably have all code on the frame 1 of the main clip, and then put the other movieclips with buttons and stuff as children of the main clip. That way main clip can control the state, and know what to show when.

Actionscript 3: gotoAndStop

I have a project in Flash CS3 that's giving me a bit of trouble. I have a movieclip, and in that movieclip, I have a button. The movieclip is named bg and the button tohallway_btn. My coding is on the stage on a layer, not on classes or in a package, or anything of that sort. This is my coding:
bg.tohallway_btn.addEventListener(MouseEvent.CLICK, tohallwayClick);
function tohallwayClick(event:MouseEvent):void{
gotoAndStop (141);
}
It seems simple enough, yet when I debug and the button is clicked, the flash player freezes over. I have absolutely no idea what's causing it to do this.
I get a type error in output as well:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Camille_fla::MainTimeline/enterF()[Camille_fla.MainTimeline::frame140:130]
Any help is appreciated.
An onEnterFrame listener was called and not removed that was referencing an object (bg) that was not on the stage after the goto call.
function tohallwayClick(event:MouseEvent):void {
**removeEventListener(Event.ENTER_FRAME, enterF);**
gotoAndStop(141);
}
First make sure your button and your code are on the same frame, they can be on different layers, but make sure they are lined up.
If you want it to go to the frame on your main timeline, or stage, instead of writing:
gotoAndStop (141)
try:
stage.gotoAndStop(141);

AS3 preloader sorrows, unable to load symbols from library

I created an AS3 preloader, and placed the code for that on frame one.
I then made a symbol, and placed it in the library. It was set to NOT export on frame 1, and the fla's settings had all classes exported on frame two. There were no references to the object until frame two.
Then, flash crashed whenever I compiled without the "Export in frame one" box checked.
To fix this, a friend suggested I start my game logic on frame 3, so it will have properly loaded frame 2. That seemed to work fine, the class was instantiating properly.
Then, it turned out that it was not loading the movieclip, only instantiating the class. Again, this could be fixed by exporting in frame 1, but I really cannot afford to do that.
The same friend suggested I place an instance of the symbol on the stage on frame 3, and perform game logic on frame 4. They said this would initialize the movieclip properly.
However, this was not the case. How can I load the entire symbol, graphics and all, without exporting to frame 1? This single symbol will contain probably 10-20 MB of graphics, so it needs to be preloaded.
Thanks for the help!
EDIT: To make a long story short, all I need is some way to load a movieclip so it can be used and visible and everything.
EDIT: Is there any way to force-load a movieclip via AS3?
Hard to figure out from descriptions.
If you make a new .fla file, paste your large(10-20MB) clip on frame 2,
set your export frame as 2, then try to preload from frame 1 and access the large clip's content in frame 2, do you get the same error ?
say you have this in frame 1:
stop();
this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onComplete(event:Event):void{
gotoAndStop(2);
}
and in frame 2:
trace(myLargeClip);//where myLargeClip would be your 10-20MB clip
It should be ok, otherwise, in case tracing your large clip returns null, you might want to try to invalidate the stage:
on frame 2:
stage.addEventListener(Event.RENDER,onRender);
stage.invalidate();
function onRender(event:Event):void{
trace(myLargeClip);
}
Basically what I'm suggesting is:
Isolate the problem. See if your large clip is causing problems in a similar, but simplified scenario and why, then once you got a fix use it in your main fla.
Try the stage invalidation, although, since I don't fully understand your setup, it's just a wild guess.
HTH,
George