Error #1009 when jumping to the next frame - actionscript-3

I'm getting a TypeError: Error #1009 when jumping to the next frame.
This code is in frame 3:
this.addEventListener(Event.ENTER_FRAME,activate);
function activate(event:Event)
{
if (fname.length > 2 && sname.length > 2 && cname.length > 1)
{
bt_Send.alpha = 1.0;
bt_Send.enabled = true;
enableIt = true;
}
else
{
bt_Send.alpha = 0.05;
bt_Send.enabled = false;
enableIt = false;
}
}
When I click the "next" button, the movie jumps to frame 4. After the button is clicked appears the TypeError: Error #1009 at new_cics_fla::MainTimeline/activate()
In the frame 4 are no mention to the function "activate".
The movie runs normally, but I'd like to know why I'm getting this message.
Thanks in advance.
Cheers, Sergio

It sounds like your enterFrame is continuing to execute but the properties the activate method acts on are not available in the frame you've sent the playhead to.
Try removing the enterFrame in the method which handles clicks on your next button:
function nextClick(event:MouseEvent):void
{
// Clean up the enterFrame
this.removeEventListener(Event.ENTER_FRAME,activate);
// Now advance to the next frame
this.gotoAndPlay(4);
}

Related

AS3 - Error #1009 and cannot find cause of issue

Using AS3 in Animate CC, I get this error:
Error #1009: Cannot access a property or method of a null object reference.
I have read through all the work-through for this error On Stack Overflow but I still can't figure this one out. Any help would be a blessing.
I have a navigation panel of 96 buttons I'm attempting that when user clicks a movie from the library appears. A couple problems, first the error 1009 and also secondly when a new MovieClip appears the current one needs to go away.
Label for one of the buttons
btn_SAFER_25_1Jan
Label for Movie clip Symbol property
mcSaf_Jan01_25
Label for Name of Movie Symbol Class
Saf_Jan01_25
Code below, each frame of timeline that suppose to load each movie has similar, this one is for Jan 1 25
stop();
var saf_Jan01_25:Saf_Jan01_25=new Saf_Jan01_25;
addChild(saf_Jan01_25);
addChildAt(saf_Jan01_25,0);
saf_Jan01_25.x=394;//sets the x position
saf_Jan01_25.x=344;//sets the y position
saf_Jan01_25.visible = true;
saf_Jan01_100.visible = false;
saf_Jan01_250.visible = false;
saf_Jan01_500.visible = false;
script below is on timeline of for each navigation button
btn_SAFER_25_1Jan.addEventListner(MouseEvent.CLICK, fl_MouseOverHandler2);
btn_SAFER_25_1Jan.enabled = true;
function fl_MouseOverHandler2(eventMouseEvent):void
{
{
gotoAndPlay(2);
}
trace("Click");
}
btn_SAFER_100_1Jan.addEventListner(MouseEvent.CLICK, fl_MouseOverHandler3);
btn_SAFER_100_1Jan.enabled = true;
function fl_MouseOverHandler3(eventMouseEvent):void
{
{
gotoAndPlay(3);
}
trace("Click");
}

ActionScript 3.0 - How to make a button finish looping the scene, then go to the next scene

So, lets say scene 1 has 40 frames. Someone clicks on the button to go to the next scene at frame 10. I want it to continue to frame 40, then change to the next scene.
Keep in mind that the 40 frame scene is on loop, so it could loop 4 or 5 times, then someone clicks next scene. So, I want it to finish the loop, then go to the next scene.
I've been looking everywhere on how to do this, and can't find anything!
So, I have this code right here -
At the beginning of the movie (Frame 1-39), I have -
var clicked:Boolean = false;
button1.addEventListener(MouseEvent.CLICK, goScene2);
function goScene2(event:MouseEvent):void {
clicked = true;
}
Then, on the last frame (frame 40), I have -
if(MouseEvent.CLICK){
gotoAndPlay(1,"Scene 2");
}
else{
gotoAndPlay(1,"Scene1");
}
It goes to scene 2, even if you don't click the button.
Thanks in advance!
That's because the MouseEvent.CLICK variable is always truthy. It's the constant you use to bind a click event handler. I think you mean to use the clicked variable that you set to true in the click handler.
if(clicked){
gotoAndPlay(1,"Scene 2");
}
else{
gotoAndPlay(1,"Scene1");
}

as3 how to perfom different actions if button is clicked or not

I am a complete n00b.
I'm working on a game. Basically, I have to perform no action if a button is pressed on a certain keyframe, but if it's not clicked, I need the scene to proceed until a certain point and then go to another frame.
In my stupid head I thought: if I can create an empty global variable I could input "something" when the button is clicked on my frame and check on a latter frame "if globalvar == "something": nothing happens, else: gotoAndPlay(where I need)"
Explanatory image:
Apparently that's not how AS3 works. So what can I do?
import flash.events.MouseEvent;
var pressed = "no";
function work(event:MouseEvent):void
{
pedal.visible = false;
cursor_mc.visible = false;
pressed = "yes"
}
pedal.addEventListener(MouseEvent.CLICK, work);
This is what I've got when the action needs to be performed.
Then I would put (I've never written an if statement in as3)
if (pressed == "no") {
gotoAndPlay(some other frame);
}
This would be put on a complete different frame AND LAYER from the previous so that the part in between is still played.
You got to add a new event, and ENTER_FRAME event to which will fire a function on every frame. There you could check whether the button is pressed. For example:
stage.addEventListener(Event.ENTER_FRAME, updateFunction);
function updateFunction(e:Event){
if(pressed == "no"){
if(currentFrame == frame number when you want to check){
gotoAndPlay(some other frame);
}
}
}

As3 Flash trying to hide a clip / frame

I have the following code:
stop();
stage.addEventListener(MouseEvent.RIGHT_CLICK, function(e:Event){});
login_btn.buttonMode = true; // show hand cursor
//adding event listener
login_btn.addEventListener(MouseEvent.CLICK, loginUser);
function loginUser(e:MouseEvent):void{
//if username & password are correct allow entry
if(IDTxt.text == "wixlab"){
gotoAndStop(2);
}
if(IDTxt.text == "hello"){
gotoAndStop(2);
}
if(IDTxt.text == "hello"){
gotoAndStop(2);
}else {
//otherwise show an error message
var alert:TextFormat = new TextFormat();
alert.color = 0xff0000; //red font
messageTxt.text = "Incorrect ID, Please try again.";
messageTxt.setTextFormat(alert);
}
}
and in the frame 2, i have stop();
Please i am trying to make it ones they login, the whole clip just go away so they can click on what is behind it. i am using Wix to add this SWF to hide a video. but ones they are logined you won't be able to click on the video because the swf is above it.
please help
If you move all this code to a layer just for scripting, at frame 1 (which you may already have) you could write a simple conditional that says something like if (this.currentFrame == 2){ getRidOfMyObjects(); } I'm not positive that syntax is correct, but you could put this right under your current conditional statement. And getRidOfMyObjects could be simple visibility commands or removeChild or whatever is best for your scene...Does this help?

Parameter child must be non-null error in AS3

I have a code to pause the game, and it runs the pause function as shown:
public function onKeyPress(keyboardEvent:KeyboardEvent) :void
{
//Check for pause
if(keyboardEvent.keyCode == Keyboard.P)
{
//If the timer is still running
if(gameTimer.running)
{
gameTimer.stop();
Mouse.show();
pauseText = new PauseText();
pauseText.x = 150;
pauseText.y = 100;
addChild(pauseText);
//If the player is using the mouse, resume by clicking on the player
if(mouseControl)
{
player.addEventListener(MouseEvent.CLICK, resumeGame);
pauseText.pauseInformation.text = "click on yourself";
}
else
{
pauseText.pauseInformation.text = "press 'p'";
}
}
else
{
//Only allow the player to resume with P IF he is using the keyboard
//This prevents cheating with the mouse.
if(!mouseControl)
{
gameTimer.start();
removeChild(pauseText);
pauseText = null;
}
}
}
}
The game runs perfectly fine. On my first playthrough, the pause functions work. However, if later I die and restart the game, then pause it, I get the following message:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at Game/onKeyPress()
The game still runs fine though. However, everytime I pause, or unpause, this error appears. If I die again, restart, then pause, TWO of these errors appears. From what I can gather it seems as if it attempts to remove the pauseText…but I’ve been removing it just fine on the first playthrough, I’ve used removeChild() then set as null for other parts of my code and it works fine. Additionally, if I add a trace(“a”); statement right after the function header, I get the error before the “a” appears on the output panel.
What’s wrong?
Additional notes:
If I don’t use the pause function at all for my first playthough, there is no error when I call it up on my second playthrough.
put removeChild into 'if' ,this will solve error :
if(pauseText.parent){
pauseText.parent.removeChild(pauseText);
}
but You should anyway check what is the source of problem , maybe 'gameTimer.running' is false on beggining ?
You probably instantiate another Game object (the one that contains the whole game) while not removing the previous game's event listener. That would explain such behavior, since you have more than one KeyboardEvent.KEY_DOWN listener active, and note that when you're stopping the game, you most likely stop the timer in it, so the "else" clause of your "if (gameTimer.running)" statement is executed, but the timer was effectively stop without pauseText to be generated. So, you miss a
removeEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);
in your game destruction code.
if (!mouseControl) {
gameTimer.start();
if (pauseText && contains(pauseText)) {
removeChild(pauseText);
pauseText = null;
}
}