1120 undefined error, but the instance name is correct - actionscript-3

There are 2 separate buttons, a, and b. I have double checked and triple checked that their instance name is a and b respectively.
I want a to take me to frame3, and b to frame4.
But when I test it, it tells me that it (both) is undefined so the buttons don't do anything.
Before that, I have a start menu and I have the same code for the 'Start' button and it works just fine, it takes me to the frame where a and b are.
But other buttons just won't work? I always get the same undefined error.
a.addEventListener(MouseEvent.CLICK, aClick);
function aClick(e:MouseEvent):void{
gotoAndStop(3);
}
b.addEventListener(MouseEvent.CLICK, bClick);
function bClick(e:MouseEvent):void{
gotoAndStop(4);
}

Related

TypeError: Error #1010: A term is undefined and has no properties?

Error:
TypeError: Error #1010: A term is undefined and has no properties. at Untitled_2_fla::MainTimeline/frame1()[Untitled_2_fla.MainTimeline::frame1:4]
Can't seem to figure out what's that about.
Here's my code:
import flash.events.MouseEvent;
btnMc1.txtSourceMc.gotoAndStop();
btnMc1.addEventListener(MouseEvent.ROLL_OVER, over);
btnMc1.addEventListener(MouseEvent.ROLL_OUT, out);
function over(e: MouseEvent) {
btnMc1.buttonMode = true;
btnMc1.gotoAndPlay(1);
}
function out(e: MouseEvent) {
btnMc1.gotoAndPlay(62);
}
You need to create the variable btnMc1, such as var btnMc1 And you're going to have to give it a class such as var btnMc1:
whatEverObjectICreated = new whatEverObjectICreated;
btnMc1.txtSourceMc.gotoAndStop();
Change that to
btnMc1.txtSourceMc.gotoAndStop(1);
Make sure that you've named you MCs correctly. Firstly, go to your stage, and click on btnMc1. Make sure you've name it as such in the properties panel(windows -> properties). Next, double click onto btnMc1 and do the same for txtSourceMc, make sure that it has the correct name. Im assuming that you have txtSourceMc inside btnMc1 on the timeline.
If the problem persists, it is probably because whatever you're loading into btnMc1 or txtSourceMc is too huge. So it cannot instantiate on the frame you're trying to call it in this code, thus it returns as undefined.
To solve this, right click onto btnMc1 from the library and select properties. Then select export for actionscript and make sure that the box for exporting on frame1 is checked. Do the same for txtSourceMc.

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.

actionscript 3.0 function mouseevent event handler

I have a function that uses a mouse event and it removes and adds things onto the stage:
beginBut.addEventListener(MouseEvent.CLICK, bgnListener);
function bgnListener (event:MouseEvent) {
removeEventListener(Event.ENTER_FRAME, setScreen);
removeChild(beginBut);
removeChild(myWord);
healthBar.addEventListener(Event.ENTER_FRAME, healthLose);
ball.addEventListener(Event.ENTER_FRAME, moveBall);
myGem.addEventListener(Event.ENTER_FRAME, addGem);
myScore.addEventListener(Event.ENTER_FRAME, scoreCount);
healthBar.width+=1000;
}
However after some other things happen, I need this event to occur again. I have already
added beginBut but when I use
beginBut.addEventListener(MouseEvent.CLICK, bgnListener);
the event adds and removes the things automatically when the function that adds beginBut back occurs and not when I actually click on beginBut. I have also tried
bgnListener();
but it says that there is the wrong number of arguments. I already searched all over and I can't seem to fix this. Any help would be greatly appreciated.
If you call bgnListener() like you are now, you'll get an argument mismatch error because the function is expecting to receive a MouseEvent.
If you want to be able to call bgnListener() on its own like that, you can define a default value for your argument event, which can be null:
function bgnListener(event:MouseEvent = null)
{
// ...
}

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;
}
}

Can't remove child from the stage - if(stage.contains(child1)){removeChild(child1);} doesn't work

It may sound stupid, but how can I remove a definite child from the stage?
e.g.
function giveMeResult(e:MouseEvent):void
{
if(stage.contains(result))
{removeChild(result);}
addChild(result); // this part works fine, but it adds one over another
}
it adds one result on the top of the previous one.
I want the function "giveMeResult: to remove "result" if is on the stage and add a new one.
UPDATE:*
result is a TextField, and result.txt ="" changes from time to time ...
trace (result.parent); /// gives [object Stage]
trace (result.stage); /// gives[object Stage]
trace (result.parent != null && result.parent == result.stage); // gives true
when
result.parent.removeChild(result);
is written without if statement - gives error TypeError: Error #1009: Cannot access a property or method of a null object reference.
when written inside:
if (result.parent !=null && result.parent == result.stage)
{
result.parent.removeChild(result);
}
nothing happens and new child is added on the top of the previous one.
Thanks to all!!!
The result is simple :)
All I had to do is just change result.txt without even removing it from the stage :)
You need to type stage.removeChild(result); and subsequently stage.addChild(result);
Edit:
Looking at a function similar to yours:
private function func(e:Event) : void {
if(stage.contains(result)) {
stage.removeChild(result);
}
stage.addChild(result);
}
The only way this would add a new instance of the TextField result to the stage, without removing the old one, would be if result has changed. See this flow of execution:
var result : TextField = new TextField();
// An event occurs and func get's called.
// now result will be added to stage.
result = new TextField();
// An event occurs and func get's called again
// This time the stage.contains(..) will return false, since the current result
// is not actually on stage. This will add a second TextField to the stage.
If I am clearly understood what you want, then this code can help you:
if (result.parent != null && result.parent == result.stage)
{
// stage itself contains result
result.parent.removeChild(result);
}
From the docs for DisplayObjectContainer.contains(): "Grandchildren, great-grandchildren, and so on each return true."
So contains means anywhere on the display list, not just direct children. What you want is the parent check Manque pointed out, or simply to remove result from anywhere it might be:
if (result.parent) {result.parent.removeChild(result); }
Though oddly, addChild(result) will automatically remove it from its previous parent - a DisplayObject can only be at one place in the DisplayList at a time, so I'm not sure why you're seeing multiple results...
Is it possible that the "result" you've passed in isn't the result that's already on the stage?
Have you tried?
MovieClip(root).removeChild(result)
[EDIT]
function giveMeResult(e:MouseEvent):void{
if(result.parent != null && result.parent == result.stage){
stage.removeChild(result);
}
stage.addChild(result);
}
All I had to do is just change result.txt without even removing it from the stage