使用this.addChild后,AS3程序开始搞乱

时间:2014-07-10 14:50:18

标签: actionscript-3 this parent-child removechild addchild

示例问题:

我最近在我的程序中添加了一个按钮;当按钮是  单击,背景音乐静音,静音按钮的动画片段变为另一个。再次点击时,  音乐取消静音,动画片段再次改变为最初的状态。  我使用以下代码执行此操作:

//loading the music:
var req:URLRequest = new URLRequest("teardrop.mp3");
var s:Sound = new Sound(req);
var myChannel:SoundChannel = new SoundChannel();
var sTransform:SoundTransform = new SoundTransform();
sTransform.volume = 1;
myChannel = s.play(0,9999);

//Creating the movieclip and making it a button:
var sb1:MovieClip = new MovieClip();
sb1.addChild(onS);
sb1.buttonMode = true;
sb1.useHandCursor = true;
sb1.addEventListener(MouseEvent.CLICK,muter);

//If the Music is muted, unmute it and switch to onS, if not, mute it and switch to offS.
function muter(e:MouseEvent):void
{
  if (sTransform.volume == 1)
  {
      sTransform.volume = 0;
      sb1.removeChild(onS);
      sb1.addChild(offS);
  }
  else if (sTransform.volume == 0)
  {
      sTransform.volume = 1;
      sb1.removeChild(offS);
      sb1.addChild(onS);
  }
  myChannel.soundTransform = sTransform;
}
//Note: onS and offS are movieclips which are put into the first scene Since the mute/unmute button is supposed to appear in all of the

~10个场景,在使用gotoAndStop(frame, "Scene")之后  场景到另一个,我在每个场景中使用this.addChild(sb1)来添加  静音按钮到那个场景。

但出于某种原因,自从我这样做以来,我的计划一直都是  搞乱。静音/取消静音工作正常,但在其余部分  程序:无法加载变量,按钮和文本字段  正确地,或者很快就没有加载,因为我收到了错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference After a while of travelling from scene to scene,

指向这一行...

getCards.addEventListener(MouseEvent.CLICK, getCardsMenu);

最近,这个:

TypeError: Error #1007: Instantiation attempted on a non-constructor. Which was pointing at this line...

var aroz:Array = new Array[nextPart,prevPart];

为什么会这样?

1 个答案:

答案 0 :(得分:0)

答案:切勿在未清除任何已加载到该场景的动画片段的情况下切换场景。在使用gotoAndStop(frame, "scene")之前,请始终删除您已添加到每个场景的孩子。例如:

//some code
function menuScene (e:MouseEvent):void
{
    //Never forget this line before moving scenes
    this.removeChild(sb1);
    gotoAndStop(1, "Menu Scene");
}
//more code

我发布了这个,所以没有人犯过我一直犯的错误,这个错误让我发疯,让我讨厌孩子。

PS:不是真正的孩子,我的意思是显示方法xD