Error 2025 that I absolutely don't understand - actionscript-3

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?

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

SImple Coloring Book: Error #1009: Cannot access a property or method of a null object reference

I am creating a simple flash coloring book and am not very familiar with as3 programming language.
I entered the following code,and when I attempted to press the back button in the test movie I got that error.
stop();
back_btn.addEventListener(MouseEvent.CLICK, GoToChooseA);
function GoToChooseA(event:MouseEvent):void
{
gotoAndStop("Choose");
}
color_scroll.mask = myMask;
var goY: Number = color_scroll.y;
stage.addEventListener(Event.ENTER_FRAME, scrollManage);
function scrollManage(Event): void {
color_scroll.y += (goY - color_scroll.y) / 20;
}
up_btn.addEventListener(MouseEvent.MOUSE_DOWN, scrollUP);
down_btn.addEventListener(MouseEvent.MOUSE_DOWN, scrollDown);
function scrollUP(MouseEvent): void {
goY += 20;
}
function scrollDown(MouseEvent): void {
goY -= 20;
}
*
It seems to indicate the error is here
color_scroll.y += (goY - color_scroll.y) / 20;
But I'm really bummed because I'm not really sure how to proceed from there.
Whenever you gotoAndStop() to a different keyframe, your current frame is invalidated and all its members destroyed. Listeners persist, if they are attached to an object that persists. So, right after you call GoToChooseA(), your color_scroll is destroyed, and then the listener attached to stage is called and tries to modify a destroyed object, there goes your 1009. The solution is either manually remove the event listeners "scrollManage", "scrollUp", "scrollDown" before you change the frame, at least "scrollManage" because it's attached to stage, or stop using frames altogether, but even then you'll have to control your event listeners.
You could add some logic to your function to check if you are in the right frame and then proceed. I am not familiar with frames so the condition would be something like this._currentframe == 2 or timeline.currentFrame == 2.
function scrollManage(Event): void {
if ( condition ) {
color_scroll.y += (goY - color_scroll.y) / 20;
}
}
If you are not at the right frame (in my example that is frame 2), the function does not execute any code.
This error means you are trying to modify something that is no longer existent.

Actionscript 3.0, adobe flash cs6 - removing a child

I set a state every time I press a button. Then it goes to a frame and in the action script, whatever the state, it add a child to the stage and change the state again, but when you click the button again the child removes and state is changed back to previous state and the same child gets added again. Here is my code:
// first frame and will never come back to this one
function blueTurnValve(event:MouseEvent) // to go to the frame
{
gotoAndPlay("blue");
STATE = 1;
}
var redscreen:redfilling = new redfilling();
if (STATE == 2)
{
removeChild(redscreen);
STATE = 1;
}
if (STATE == 1)
{
addChildAt(redscreen,2);
STATE = 2;
}
//after the child was added
function blueTurnValve1(event:MouseEvent)
{
gotoAndPlay("blue");
}
but it won't work, I get the error in the output(just before removing the child):
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at p1_fla::MainTimeline/frame2()[p1_fla.MainTimeline::frame2:35]
at flash.display::MovieClip/gotoAndPlay()
at p1_fla::MainTimeline/blueTurnValve1()[p1_fla.MainTimeline::frame30:19]
what is wrong with my code?
You need to use removeChildAt() since at STATE==1.
You are adding it as addChildAt(redscreen,2);
Try, removing it like so,
removeChildAt(2);

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller

I'm creating a small game in as3.0. I spawn a lot of walls in the game that are trying to crush you (the player).
I'm trying to delete all the walls that are near you from the array and from the screen. The first time I hit the button it works it just deletes all the walls that are in a 250px range. But the second time I hit the button get the following error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
I think this is because the tries to delete childs who already has been deleted. I tried to check if the child existed with the following code but it doesn't seem to work.
if (wallArray[i] != null && contains(wall)) {
if (wallArray[i].x < 250 + wp_reach){
//haalt de muur weg
//TODO : KIJK OF HET KIND IS
removeChild(wallArray[i]);
}
}
Here is the full code that handels the button press:
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
action.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler);
function fl_TapHandler(event:TouchEvent):void
{
for (var i:Number=0;i<wallArray.length;i++){
if (wallArray[i] != null && contains(wall)) {
if (wallArray[i].x < 250 + wp_reach){
removeChild(wallArray[i]);
}
}
else{
trace ("There is no wall in range yet");
}
}
}
Always test if item in query is the child of the container you want to remove it from as follows:
if(wallArray[i] && contains(wallArray[i]))
{
removeChild(wallArray[i]);
}
best regards

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