TypeError:错误#1006:addChild不是函数

时间:2015-04-05 09:50:16

标签: actionscript-3 flash

我正在尝试在我自己的flash动画中嵌入一个“预先制作的”flash actionscript 3库。这两种动画都是独立工作的。但是,一旦我将照片库插入我自己的项目中,我就会收到错误;

TypeError: Error #1006: addChild is not a function.
at project_fla::galeri_6/onCompleteXmlLoad()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

我的flash按钮代码通向画廊;

stop();

galeri_btn.addEventListener(MouseEvent.CLICK, galeri);

function galeri(event:MouseEvent):void
{
    gotoAndStop(3);
}

最后,flash画廊代码是;

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;


var xmlLoader:URLLoader = new URLLoader(new URLRequest("photo_gallery.xml"));

xmlLoader.addEventListener(Event.COMPLETE, onCompleteXmlLoad);

var xmlFile:XML;


var xcoord:int = 10;

var gal:gallery = new gallery();
gal.x = 10;
gal.y = 10;
addChild(gal);

var thumbsContainer:Sprite = new Sprite();
thumbsContainer.x = 10;
thumbsContainer.y = 320;
addChild(thumbsContainer);


var txtTitle:TextField = new TextField();
txtTitle.x = 15;
txtTitle.y = 15;
var format:TextFormat = new TextFormat();
format.bold = true;
format.color = 0xFFFFFF;
format.size = "20";
format.font = "Arial";
txtTitle.defaultTextFormat = format;
addChild(txtTitle);

var txtDesc:TextField = new TextField();
txtDesc.x = 15;
txtDesc.y = 40;
var format1:TextFormat = new TextFormat();
format1.color = 0x000000;
format.font = "Calibri";
format.size = 12;
txtDesc.defaultTextFormat = format1;
addChild(txtDesc);

function onCompleteXmlLoad(e:Event):void{

    xmlFile = new XML(xmlLoader.data);      

    var len:int = xmlFile.photo.length();

    txtTitle.text = xmlFile.photo.name[0];
    txtDesc.text = xmlFile.photo.desc[0];

    for(var i:int = 0;i<len;i++){
        var t:thumbs = new thumbs();
        t.x = xcoord;
        t.y = 10;
        t.buttonMode = true;        
        t.name = (i+1).toString();              
        thumbsContainer.addChild(t);

        t.addEventListener(MouseEvent.MOUSE_OVER, onMouseover);
        t.addEventListener(MouseEvent.MOUSE_OUT, onMouseout);
        t.addEventListener(MouseEvent.CLICK, onMouseClick);

        var tloader:Loader = new Loader();
        tloader.load(new URLRequest("thumbs/" + (i+1) + ".jpg"));
        t.addChild(tloader);        

        xcoord += t.width + 10;
    }

    var loader:Loader = new Loader();
    loader.load(new URLRequest("img/1.jpg"));
    gal.addChild(loader);


    scroller.source = thumbsContainer;
    scroller.setSize(550,110);

}

function onMouseover(e:MouseEvent):void{

    e.currentTarget.alpha = 0.5;

}

function onMouseout(e:MouseEvent):void{

    e.currentTarget.alpha = 1.0;

}


function onMouseClick(e:MouseEvent):void{

    var loader:Loader = new Loader();
    loader.load(new URLRequest("img/" + e.currentTarget.name + ".jpg"));    
    gal.addChild(loader);       

    txtTitle.text = xmlFile.photo.name[int(e.currentTarget.name) - 1];
    txtDesc.text = xmlFile.photo.desc[int(e.currentTarget.name) - 1];

}

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

问题出在以下两行之一:

t.addChild(tloader);    

gal.addChild(loader);

原因是addChild is not a function错误消息。如果您在不存在的对象上调用addChild,则错误将是其他错误。

t的类型为thumbs - var t:thumbs = new thumbs();galgallery - var gal:gallery = new gallery();

因此,其中任何一个类(thumbsgallery)都存在问题。他们应该做以下之一(在课堂内):

  • 延长DisplayObjectContainer - Sprite甚至MovieClip是最安全的
  • 提供addChild种功能