AS3 - Error #1009 and cannot find cause of issue - actionscript-3

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

Related

Error 2025 that I absolutely don't understand

I'm trying to build a point & click game.
I can drag item from my inventory to the scene.
I want to make my object disapear when I'm clicking 2 times.
It's working, but when the object disapear I've got an error 2025.. (I can ignore it and everything is working, but I'd like to correct this error).
My error say :
Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at com.laserdragonuniversity.alpaca::DraggedItem/removeDraggedItem()
[C:\Users\Stéphan\Desktop\12 octobre\La Brousse en folie tactile\com\laserdragonuniversity\alpaca\DraggedItem.as:145]
Here's when it's happening :
(I click on my inventory, take my item, drag it to the scene, click 2 times anywhere, the item diseapear, I'm clicking on the inventory again --> ERROR 2025)
Here's my removeDraggedItem function :
private function removeDraggedItem(e:MouseEvent) {
if(timer.running==true) {
if(e.buttonDown) {
stageRef.removeEventListener(MouseEvent.MOUSE_MOVE, dragItem);
stageRef.removeEventListener(Event.ENTER_FRAME, itemHitTest);
draggedItem.removeEventListener(MouseEvent.MOUSE_DOWN, itemClick);
stageRef.removeChild(draggedItem);
toolbar.useText.text = "";
if (stageRef.contains(this))
stageRef.removeChild(this);
Mouse.show();
Engine.playerControl = true;
}
} else {
if(e.buttonDown) {
timer.start();
}
}
}
What am I doing wrong ?
To avoid this error, I do this:
if( itemToBeRemoved.parent )
{
itemToBeRemoved.parent.removeChild( itemToBeRemoved );
}
I can't tell what the problem is in your code as its not showing me the contents of DraggedItem especially like 145. Perhaps you clicking 2 times causes remove-item events that shouldn't?

Error #1009 when jumping to the next frame

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

as3 - addchild with drag and drop

I am trying to add a child instance of an object to the stage, then allow the user to drag and drop this object (in this case, a movie clip) on the stage. However, I am getting the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at working_copy_fla::MainTimeline/dragObject()
So, that is my first problem. Then second problem, is I have not found an answer as to how to make a child object (specifically, a movie clip) able to properly be dragged and dropped on the stage.
Here is my code:
// Allow buttons to bring objects to the stage
myButton.addEventListener(MouseEvent.CLICK, addImage);
function addImage(event:MouseEvent):void
{
var myImage:Image_mc = new Image_mc();
stage.addChild(myImage);
// Center the object
myImage.x = 300;
myImage.y = 300;
// Allow the object to be drag and dropped
myImage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
myImage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
}
function startDragging(event:MouseEvent):void
{
event.target.x = event.target.parent.mouseX - event.target.mouseX
event.target.y = event.target.parent.mouseY - event.target.mouseY
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject);
}
function dragObject(event:MouseEvent):void
{
event.target.x = event.target.parent.mouseX - event.target.mouseX
event.target.y = event.target.parent.mouseY - event.target.mouseY
}
function stopDragging(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject);
}
EDIT
I figured it out, and the solution was as simple as looking to the sample code in Adobe Flash (using CS6). Here is my code now:
// Allow buttons to bring objects to the stage
myButton.addEventListener(MouseEvent.CLICK, addImage);
function addImage(event:MouseEvent):void
{
var myImage:Image_mc = new Image_mc();
stage.addChild(myImage);
// Center the object
myImage.x = 300;
myImage.y = 300;
// Allow the object to be dragged
myImage.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
}
function clickToDrag(event:MouseEvent):void
{
event.target.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
function releaseToDrop(event:MouseEvent):void
{
event.target.stopDrag();
}
The key here was that I have created universal functions (clickToDrag and releaseToDrop) that will accept input from any object (so I can re-use these functions with other images that I add to the stage). This code works with multiple children on the stage (all can be drag and dropped at any time).
The only problem I am having with it now is that I am getting this error whenever I spawn a child element (by clicking on the myButton button instance):
ReferenceError: Error #1069: Property stopDrag not found on flash.display.SimpleButton and there is no default value.
at working_copy_fla::MainTimeline/releaseToDrop()
This error is not stopping the application from working; everything still runs fine. But I would still like to figure out why this error is occuring. My guess is that whatever is using "stopDrag" (should just be a movie clip) is not capable of that method.
event.target.x = event.target.parent.mouseX - event.target.mouseX
The above code assumes that 'event.target' is the stage, right (because you added the listener to the stage)? So you're trying to change the x/y of the stage? No. The start/stopDragging should make a reference to the object being dragged, available as a private class variable, which is visible to the dragObject method. Also, what is the stage's parent ('event.target.parent.mouseX')? There is no parent to the stage. This is probably what the "null object reference" is refering to.
EDIT
I'm used to Object-Oriented AS3 (highly recommended), but I'm guessing that programming on the 'timeline' the following should work. Declare a variable outside of your functions, something like:
var objectCurrentDragging:DisplayObjectContainer;
Then in your 'addImage' function, use the following code to make objectCurrentDragging reference the object which you want to drag:
objectCurrentDragging = myImage;
Then in the dragObject function, simply reference objectCurrentDragging:
objectCurrentDragging.x = ...
Hope that works out for you.
So I have finally figured it out. The key was not to add an event to the stage, but rather, to add the "releaseToDrop" function to the *MouseEvent.MOUSE_UP* event on the child element in the same function that adds it to the stage. Now I get no more errors, and it works with multiple instances of the child objects (movie clips) on the stage.
Here is the code that works:
// Allow buttons to bring objects to the stage
myButton.addEventListener(MouseEvent.CLICK, addImage);
function addImage(event:MouseEvent):void
{
var myImage:Image_mc = new Image_mc();
stage.addChild(myImage);
// Center the object
myImage.x = 300;
myImage.y = 300;
// Allow the object to be dragged
myImage.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
myImage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
}
function clickToDrag(event:MouseEvent):void
{
event.target.startDrag();
}
function releaseToDrop(event:MouseEvent):void
{
event.target.stopDrag();
}

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.

as3 cs5 Odd Error #1009 in drag and drop game

I'm making a drag-and-drop game.
On the MouseEvent.MOUSE_UP I have an if-statement like so:
function fnUp(event:MouseEvent):void
{
clicked.stopDrag();
if (clicked.dropTarget.parent.parent.name == clicked.currentLabel)
{
var dropped:Card = clicked.dropTarget.parent.parent as Card;
dropped.gotoAndStop(dropped.name);
dropped.bg.gotoAndStop("done");
removeChild(clicked);
}
else
{
clicked.x = clicked.sx;
clicked.y = clicked.sy;
clicked.bg.gotoAndStop("off");
}
clicked = null;
}
"clicked" is an earlier declared var of type Card(); and it's set in the MouseEvent.MOUSE_DOWN like so:
function fnDown(event:MouseEvent):void
{
clicked = event.currentTarget as Card;
clicked.bg.gotoAndStop("on");
clicked.startDrag();
addChild(clicked);
}
I thought that if I build my if-statement like this there wouldn't be an error if the card was dropped anywhere but on one of the three possible target cards. But I still get:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
tldr; I get an error if I drop my card outside one of the 3 possible targets.
On which line you getting an error? You need to check if all of the following objects existing:
if (clicked.dropTarget &&
clicked.dropTarget.parent &&
clicked.dropTarget.parent.parent )
{
then do something...
}