开发者

AS3: Quick question regarding children and referencing them

Edit: Cameron requested the full offending function:

function picSwitch(p=null)
{
 if (picShown == true)
 {
  picx = 3072;
  picy = 2304;
  picTweens.removeChild(pic);
  picShown = false;
 }
 else
 {
  var pic:image = new image();
  pic.smoothing = true;

  pic.width = 750;
  pic.height = 560;

  if (pic.scaleX < pic.scaleY) 
  {
   pic.scaleY = pic.scaleX;
  } 
  else 
  {
   pic.scaleX = pic.scaleY;
  }

  picx = 1536 - (pic.width/2) + 64;
  picy = 1152 - (pic.height/2) - 12;

  picTweens.addChild(pic);

  TweenLite.to(pic, 1, {x:picx, y:picy, rotation:4, ease:Quint.easeInOut});

  picShown = true;
 }
}

Original Post: Oh man, I can't believe I've spent several hours on this one.. such a stupid question that undou开发者_高级运维btedly has been asked many times before.. I apologise to you all in advance..

I'm having problems referencing a child I created.

I've got a movie clip in my library called 'image'

a container movie clip called 'picTweens' on my stage (its instance name is the same)

and in the actions panel in the stage i've got a function which contains

var pic:image = new image();
pic.smoothing = true;
picTweens.addChild(pic);

all well and good, it creates me a movie clip where it's supposed to.

now in an if statement in the same function, i have

picTweens.removeChild(pic);

but when that if statement triggers, (and i've got my pic loaded where it's supposed to be)

it tells me :

TypeError: Error #2007: Parameter child must be non-null.
 at flash.display::DisplayObjectContainer/removeChild()

is my syntax wrong? and if not, then what should i check for?


So obvious. The pic variable simply is NOT available when you want to remove the child. You declared it in the "else" part, and it was in some function (which I would guess is called on a button press, or enter frame, or any other event). Therefore, when the function is called AGAIN, this time with picShown set to true, the pic variable is NOT available, and it is null.

Either that OR you did actually declare the variable OUTSIDE of the function, but with "var" inside the functions scope, it was overwritten locally.

So - make sure to declare the pic variable outside the function, so you will be able to access the handle later:

var pic:image;
function picSwitch(...){
    // etc.
}

Also - when you have that, do NOT redeclare the pic variable INSIDE the function, use just a simple assignment ( pic = blah; ).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜