Play Movie Clip in reverse and return to frame 1 - actionscript-3

I'm doing a little animation using Actionscript 3.0 but I'm having a few problems (I'm new with AS3) when I want to play a movie clip in reverse and, then, return to frame 1(main).
Here is my code:
B6_btn.addEventListener(MouseEvent.CLICK, onClickReverse6);
function onClickReverse6(event:MouseEvent):void{
m6_mc.addEventListener(Event.ENTER_FRAME, playReverse6, false, 0, true);
}
function playReverse6(event:Event):void{
if(m6_mc.currentFrame == 1){
if(playMusic){
playMusic.stop();
}
gotoAndStop(1);
}else{
m6_mc.prevFrame();
}
}
The error I get is with the line
"if(m6_mc.currentFrame == 1)" - ERROR #1009: Cannot access a property
or method of a null object reference
If I remove the command gotoAndStop(1), no error is presented.
Can anyone, please, help me with my code?

As far as I can see, you are calling gotoAndStop some other MovieClip (it's actually whatever this is in your case). But at this time you are checking the currentFrame property fo the m6_mc.
I think there might be some error with that. If you switch this to frame 1, is there a m6_mc instance (if you're not keeping it in a variable)?

I have this code on frame 1, regarding the code presented before:
function onClick6(event:MouseEvent):void{
musicLoader = new URLRequest("music/GABRIEL.mp3");
music = new Sound();
music.load(musicLoader);
playMusic = music.play(0,4);
gotoAndStop(7);
m6_mc.gotoAndPlay(1);}

Related

How to fix 'TypeError: Error #1009' error in ActionScript3.0 Adobe Animate

I'm setting up a button on the first frame which when clicked will transfer the user to the 2nd frame using this code:
stop();
Btn_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_2);
function fl_ClickToGoToAndPlayFromFrame_2(event:MouseEvent):void
{
gotoAndPlay(2);
}
and on the second frame, I set up a dynamic text that will perform a countdown using this code:
var myTimer:Timer = new Timer(1000,60); // every second for 60 seconds
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
myTimer.start();
function onTimer(e: TimerEvent):void {
countdown_text.text = String(myTimer.repeatCount - myTimer.currentCount);
}
function onComplete(e: TimerEvent):void{
gotoAndStop(3);
}
the thing is keep getting TypeError: Error #1009 message after debugging it. I know the fault is in line 7 of the 2nd code but I have no idea what is wrong with it. Pls help!
I should see your source fla, but it is most likely related to countdown_text not being accessible in that frame. Error description is "Cannot access a property or method of a null object reference", that means it cannot find the reference which is "countdown_text".
It is very very bad practice to write AS directly in frames. Convert code into a class and assign it as a document class.
You can find Adobe documentation for document class here: https://helpx.adobe.com/animate/using/actionscript-publish-settings.html

AS3 gotoAndStop(2); causes a 1009 error second time the frame runs

Disclaimer: I'm really new/incredibly bad at AS3 so it's probably something really stupid that should never happen
Okay so, the first time my main menu frame runs, it runs fine and sends me to the gameplay frame when I press the button. After the gameplay is complete, it returns to the menu frame, and runs fine until I press the same button from before, which calls this error: .
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main_fla::MainTimeline/frame2()[Main_fla.MainTimeline::frame2:6]
at flash.display::MovieClip/gotoAndPlay()
at Main_fla::MainTimeline/easyPress()[Main_fla.MainTimeline::frame3:83]
at Main_fla::MainTimeline/mClickE()[Main_fla.MainTimeline::frame3:45]
My code for the button is as follows:
buttEasy.addEventListener(MouseEvent.CLICK, mClickE);
buttHard.addEventListener(MouseEvent.CLICK, mClickH);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mMove);
function mClickE(e:MouseEvent){
easyPress();
trace("easyP");
menuUsed = true;
}
function easyPress(){
trace("Waited for press and release");
sTime = 0;
sTempo = (6) ;
sBall = 0;
ballSpeed = 7;
gameIsOver = false;
menuUsed = true;
lvlArray0= new Array(1,0,0,2,0,0,1,0,0,3,0,0,1,0,0,2,0,0,1,0,0,3,0,01,0,0,2,0,0,1);
init2 = false;
buttEasy.removeEventListener(MouseEvent.CLICK, mClickE);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mMove);
gotoAndPlay(2);
}
I honestly have no idea why this is happening. I'm using mouse events instead of button press events and whatnot because my movieclips started disappearing and flashing and other unexplainable stuff...
yeah...
I just registered, so I can't post this as a comment.
Anyway the error occurs on frame 2, not in the script you've provided (which is on frame 3).
You can see this in the error message:
"at Main_fla::MainTimeline/frame2()[Main_fla.MainTimeline::frame2:6]"
-> frame 2 line 6.
There you're accessing something that doesn't exist anymore. (-> something that is now null)
Maybe an object on the stage that has been removed. (But there are a lot of other possibilities, so don't stick with that solution)
Post the script you have on frame 3 for further help.
The flashing and other unexplainable stuff happens, because of this error. It aborts the script and runs the flash normally. (this means that for example the stop(); method won't be executed -> the player runs through all your frames -> the objects on the stage appear to be flashing)
You're probably just addressing the "stage" before the reference is given. Start your code with:
addEventListener(Event.ADDED_TO_STAGE, init);
and a handler for this listener
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// write your code after this
}
If you're framescripting (writing AS3 code in a frame) It's not really your problem.
But as the problem states - you're calling some objects property or method witch is null. Your debugger will be able to point to the null object that you try to call on frame 2.

How to fix AS3 TypeError: Error #1009: Cannot access a property or method of a null object reference?

I am getting this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Skool_fla::MainTimeline/frame1()[Skool_fla.MainTimeline::frame1:10]
at flash.display::MovieClip/gotoAndStop()
at Skool_fla::MainTimeline/goback()[Skool_fla.MainTimeline::frame2:22]
What is causing this error and how do I fix it?
This is my code for both the frames:
Frame 1: This is the main menu screen where you can access the credit section
import flash.events.MouseEvent;
//setting up the variables
//events
//stop the timeline
stop();
//the play button
play_btn.addEventListener(MouseEvent.CLICK, playani);
function playani(e:MouseEvent)
{
//asking it to progress to the load menu
gotoAndStop(3);
}
//the credits button
credit_btn.addEventListener(MouseEvent.CLICK, creditslide);
function creditslide(e:MouseEvent)
{
//asking it to go the credits frame
gotoAndStop(2);
}
Frame 2: This is where the credits appear
//
//
//all the imports
//events
var credit:credits_bck = new credits_bck ();
var credits_name: credit_nm = new credit_nm ();
var back_butn: back_button = new back_button ();
addChild (credit);
addChild (credits_name);
addChild (back_butn);
back_butn.addEventListener(MouseEvent.CLICK,goback);
function goback(G:MouseEvent)
{
removeChild (credit);
removeChild (credits_name);
gotoAndStop(1);
}
Either play_btn or back_butn is null. Your error message's line numbers don't correspond to your code so it's hard to say. But the gist is you're trying to access a property of something that isn't anything. Check to make sure you're initializing your variables/references properly.
Maybe your problem is Flash bug too.
In my FLA there was a layer with one empty keyframe. If I puted a vector graphics on it, the error was gone. If there was one or multiple MovieClips and there was no vector graphic - the error was there again.
Then I made a new layer and copy pasted all the objects from damaged layer to new and deleted the damaged layer. It solved the problem.
NOTE: Don't copy the keyframes. Only copy the contents.
Now my project is much more complicated and sadly the error came back again.
Test movie frequently and if the error comes back, check the last keyframes and layers you created.

ActionScript 3.0 Error #1010 - preloader function

I am a Flash and ActionScript newbie. I am trying to follow a video tutorial to make a preloader and I'm having a problem that the video didn't seem to address. I believe I have entered in all of the code correctly from the video. This is it:
stop();
addEventListener(Event.ENTER_FRAME, loaderF);
function loaderF(e:Event):void{
var toLoad:Number = loaderInfo.bytesTotal;
var loaded:Number = loaderInfo.bytesLoaded;
var total:Number = loaded/toLoad;
if( loaded == toLoad ){
removeEventListener(Event.ENTER_FRAME, loaderF);
gotoAndStop(2);
} else {
preloader_mc.preloaderFill_mc.scaleX = total;
preloader_mc.percent_txt.text = Math.floor( total * 100 ) + "%";
preloader_mc.ofBytes_txt.text = loaded + "bytes";
preloader_mc.totalBytes_txt.text = toLoad + "bytes";
}
}
What I typed in doesn't generate a compiler error, but the output tells me:
TypeError: Error #1010: A term is undefined and has no properties.
at preloader_fla::MainTimeline/loaderF()
And since I really don't have any experience outside of what I'm learning from this tutorial series, I don't know what to do to fix this.
I don't use Flash CS5, but you should be able to get the line # for where the error is occurring, I believe, by executing the SWF by pressing CTRL+SHIFT+ENTER.
Once you get the line number, you should see that something on that line is null or not defined. The error says it occurs in the function loaderF(), and looking at that code the only place such an error could occur is in the else block:
} else {
preloader_mc.preloaderFill_mc.scaleX = total;
preloader_mc.percent_txt.text = Math.floor( total * 100 ) + "%";
preloader_mc.ofBytes_txt.text = loaded + "bytes";
preloader_mc.totalBytes_txt.text = toLoad + "bytes";
}
In the above code block, one of these things is not defined:
preloader_mc.preloaderFill_mc,
preloader_mc.percent_txt,
preloader_mc.ofBytes_txt,
preloader_mc.totalBytes_txt
Maybe your preloader movie clip is missing one of these objects...
First, you'll want to turn on debugging found under (File > Publish Settings > Flash (.swf) > Permit Debugging). This will provide line numbers and allow additional debugging to help track down errors.
Secondly, in the code sample you've provided, you haven't declared a loader, so when you call on loaderInfo, it makes sense that flash complains about "a term is undefined". Although, technically, the loaderInfo object is a child of the event object. Thus, loaderInfo.bytesTotal would become e.loaderInfo.bytesTotal, assuming you added the event listener to the loader object; currently yours is added to the timeline.
Bookmark Adobe's Actionscript 3.0 Reference. Use it. As you begin your journey in Flash, this will be your indispensable handbook to speaking AS3. Specifically, you'll want to refer to the Loader class.
Here's what you're likely missing in your code:
var myLoader:Loader = new Loader();
myLoader.load(new URLRequest("path/to/my/file"));
Your function loaderF is being called during every frame update to the screen (likely every .034 seconds). You'd probably be happier with ProgressEvent.PROGRESS instead of Event.ENTER_FRAME. If so, you'll also want to catch the complete event, and that'd look like this:
myLoader.addEventListener(Event.COMPLETE, loadComplete);
myLoader.addEventListener(ProgressEvent.PROGRESS, loadProgress);
function loadComplete(e:Event):void {
// Stuff to do when the file finishes loading.
}
function loadProgress(e:Event):void {
var current:int = e.bytesLoaded;
var total:int = e.bytesTotal;
var percent:Number = current/total;
// Update the readout of your loading progress.
}
Hopefully that points you in the right direction. :)

actionscript error?

I have a problem, this works then at the " and" it dies and gives me an error
TypeError: Error #1009: Cannot access
a property or method of a null object
reference. at
Untitled_fla::MainTimeline/frameLooper()
at
flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
CODE
var string:String = "Welcome to PuppetWeb Inc\nMy name is Steve and I will be your host for this presentation!\n...\nOkay I think it is ready, let's go!";
var myArray:Array = string.split("");
var timer : Timer = new Timer (100, myArray.length);
timer.addEventListener (TimerEvent.TIMER, frameLooper);
timer.start();
function frameLooper(event:Event):void {
if(myArray.length > 0) {
text1.appendText(myArray.shift());
}else{
removeEventListener(Event.ENTER_FRAME, frameLooper);
}
}
It works for the start and then just dies at and, and then it shows that error about 50 times and restarts.
Any help?
I assume this is code written on a keyframe in the timeline, so my guess would be that your textfield goes away for some reason, most likely a keyframe animation of some kind.
It's also somewhat odd that you are removing an Event.ENTER_FRAME listener when the array is empty and not the TimerEvent.TIMER
At the end, you're trying to remove the listener from an implicit "this". Your statement is equivalent to:
this.removeEventListener(Event.ENTER_FRAME, frameLooper);
But "this" is a reference to the main timeline (if this is a frame script on the main timeline) or a reference to the instance that contains this code. It's not a reference to the Timer instance, which is what you need:
event.target.removeEventListener(TimerEvent.TIMER, frameLooper);
Why not use string.charAt()?
var string:String = "Welcome to PuppetWeb Inc\nMy name is Steve and I will be your host for this presentation!\n...\nOkay I think it is ready, let's go!";
var timer : Timer = new Timer (100, string.length);
timer.addEventListener (TimerEvent.TIMER, frameLooper);
timer.start();
function frameLooper(event:TimerEvent):void {
text1.appendText(string.charAt (event.target.currentCount-1);
}
Among all the other issues Stated in the other posts you are not stopping the time.
Dont forget timer.stop( );