0
votes

Could anyone tell me whats wrong with this code? I'm attempting to rotate a button in action script 3 and i keep getting the error:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at distributor_app_fla::MainTimeline/NewChartOptionsReturn()[distributor_app_fla.MainTimeline::frame1:218] at distributor_app_fla::MainTimeline/ClickNewChartOptions()[distributor_app_fla.MainTimeline::frame1:101]

I've already Googled the error and everything i read told me to remove the child then re-add it to the frame but it continues to break at the same spot.

code:

//defined

var btnNewChartOptions:NewChartOptions = new NewChartOptions();

btnNewChartOptions.y = 279;

btnNewChartOptions.x = 439;

//created

function NewChartDown():String

{

btnNewChartOptions.addEventListener(MouseEvent.CLICK, ClickNewChartOptions);

    btnNewChartOptions.alpha = 0;

addChild(btnNewChartOptions);

var NewChartOptionsTween:Tween = new Tween(btnNewChartOptions, "alpha", Strong.easeOut, 0, 1, 1, true);

return "NewChartSelected";

}

//actual code on button

function NewChartOptionsDown():String

{
rightGrayOut.alpha = 0;

addChild(rightGrayOut);

var grayOutTween:Tween = new Tween(rightGrayOut, "alpha", Strong.easeOut, 0, 1, 1, true);

var rotateTween:Tween = new Tween(btnNewChartOptions, "rotation", Strong.easeOut, 0, 180, 1, true);



return "NewChartOptions";

}

any help is appreciated!

2
To fix your fix try replacing ` removeChild(btnNewChartOptions);` with if(btnNewChartOptions.parent == this) removeChild(btnNewChartOptions);. However I think your fix is the wrong approach to begin with. - Taurayi
@Taurayinope still the same runtime error. and as to putting it into the wrong place thats very possible as im almost completely new to as3. im attempting to make the button rotate with a tween when clicked on - Donovan
where else could i put it? if youd like i can copy the complete code but it looks like a mess in a comment - Donovan
Just edit your question and post it at the bottom instead of putting it in a comment. - The_asMan
right above this comment area is a link "edit" click it. - The_asMan

2 Answers

0
votes

There will be no need to remove and re-add the object because it is either present and available to the method or not, meaning if it were available to the method for removal then it would also be available for addressing and manipulation. This sounds a whole lot like a scope/visibility issue.

This code is on frame 1 on the timeline, which means that it should be able to address anything that's directly on the stage or assigned to a variable that is in the same scope at the time the function is run. (In the _root scope if this was AS2, like the methods on frame 1)

Is btnNewChartOptions inside another DisplayObject, like a sprite you use to hold all your buttons or something, as opposed to sitting directly on the stage? Maybe you can describe the object heirarchy you are trying to address as well as how the button gets attached to the stage (e.g. instantiated and attached at runtime or placed on the stage in a keyframe).

Can you provide the error that was being thrown before the add and remove fix was attempted?

0
votes

If looks like to me the object has not been added yet to the display list.

// replace the following lines
removeChild(btnNewChartOptions);
addChild(btnNewChartOptions);

// with 
if( !this.contains( btnNewChartOptions ) ){
  return "";
}

[EDIT]
Your code.

//var created
  var btnNewChartOptions:NewChartOptions = new NewChartOptions();
  btnNewChartOptions.y = 279;
  btnNewChartOptions.x = 439;
//button code clicked that creates button
  function NewChartDown():String {
    btnNewChartOptions.addEventListener(MouseEvent.CLICK, ClickNewChartOptions);
    btnNewChartOptions.alpha = 0;
    addChild(btnNewChartOptions);
    var NewChartOptionsTween:Tween = new Tween(btnNewChartOptions, "alpha", Strong.easeOut, 0, 1, 1, true);
    return "NewChartSelected";
  }




//function for button
  function NewChartOptionsDown():String {
    rightGrayOut.alpha = 0;
    addChild(rightGrayOut);
    var grayOutTween:Tween = new Tween(rightGrayOut, "alpha", Strong.easeOut, 0, 1, 1, true);
    var rotateTween:Tween = new Tween(btnNewChartOptions, "rotation", Strong.easeOut, 0, 180, 1, true);
    return "NewChartOptions";
  }


In your code you provided you are only doing addChild(btnNewChartOptions); in the NewChartDown function.

So that tells me that NewChartOptionsDown can not be called until NewChartDown has been called first. Because the btnNewChartOptions has never been added.
What you can do is move addChild(btnNewChartOptions); outside of the function.

//var created
  var btnNewChartOptions:NewChartOptions = new NewChartOptions();
  btnNewChartOptions.y = 279;
  btnNewChartOptions.x = 439;
  addChild(btnNewChartOptions);