2013-03-22 64 views
1

我想提出一個照相館很簡單,但一個孩子,當我嘗試分類我不斷收到此錯誤,當我按下按鈕的照片。該分類按鈕可以去除大拇指,但不會分揀它們。AS3錯誤:提供的DisplayObject必須是調用

引發ArgumentError:錯誤#2025:提供的DisplayObject必須是調用者的子。 在flash.display一::級DisplayObjectContainer/removeChild之() 在myGallery_fla :: MainTimeline/removeAllMovieClips() 在myGallery_fla :: MainTimeline/loadConcert()

我拉我的頭髮試圖修復它,我只是不能得到它。任何幫助將非常感激:)

繼承人我的代碼,它的相當長:

//declaring my permanant variables. 
var columns:Number; 
var my_x:Number; 
var my_y:Number; 
var my_thumb_width:Number; 
var my_thumb_height:Number; 
var my_images:XMLList; 
var my_total:Number; 

var currentCategory:String = "concert"; 

//new movie clip to hold images. 
var container_mc:MovieClip; 
//new movie clip to do progress bar. 
var preloaders_mc:MovieClip; 
//to give full image a hand cursor. button mode does not work on loaders so i must create a new mc. 
var full_mc:MovieClip; 

var x_counter:Number = 0; 
var y_counter:Number = 0; 

//array to hold tweens. 
var my_tweens:Array = []; 
//array for categorizing 
var movieClipArray:Array=new Array(); 

//permenant vars for tweens. 
var container_mc_tween:Tween; 
var full_tween:Tween; 

var myNewXML:XMLList; 
var myXML:XML; 
//creater new urlLoader to load xml 
var myXMLLoader:URLLoader = new URLLoader(); 
myXMLLoader.load(new URLRequest("gallery.xml")); 
myXMLLoader.addEventListener(Event.COMPLETE, processXML); 


function processXML(e:Event):void{ 
    //temperary will be deleted when executed. 
    myXML= new XML(e.target.data); 
    processXMLData(); 
} 

function processXMLData() 
{ 
myNewXML = myXML.GALLERY.(CATEGORY == currentCategory); 
//trace(myNewXML); 
    //set the values of the images 
    //@ retrives the attributes I need 
    columns = [email protected]; 
    my_x = [email protected]N; 
    my_y = [email protected]; 
    my_thumb_width = [email protected]; 
    my_thumb_height = [email protected]; 
    my_images = myXML.IMAGE; 
    my_total = my_images.length(); 



    createContainer(); 
    callThumbs(); 

    //instance of the URLLoader and its associated event listener are 
    //both not required once we extract the data from our XML file. 
    //delete these two once that task is finished. 
    myXMLLoader.removeEventListener(Event.COMPLETE, processXML); 
    myXMLLoader = null; 

} 


function createContainer():void{ 
    //creating movieclip to hold images 
    container_mc = new MovieClip(); 



    //I position the MovieClip in accordance to the variables from xml. 
    container_mc.x = my_x; 
    container_mc.y = my_y; 
    addChild(container_mc); 

    //regestering the click event to the parent instead of the thumbs 
    //instead of to each thumb. 
    container_mc.addEventListener(MouseEvent.CLICK, callFull); 
    //thumbs hover effects 
    container_mc.addEventListener(MouseEvent.MOUSE_OVER, onOver); 
    container_mc.addEventListener(MouseEvent.MOUSE_OUT, onOut); 
    container_mc.buttonMode = true; 

    preloaders_mc = new MovieClip(); 
    preloaders_mc.x = container_mc.x; 
    preloaders_mc.y = container_mc.y; 

    //movieClipArray.push(container_mc); 
    //trace(movieClipArray); 
    addChild(preloaders_mc); 
    //trace(movieClipArray); 
} 

function callThumbs():void{ 
    //loop will cycle through as long as it does not exceed the total num of images. 
    for (var i:Number = 0; i < my_total; i++){ 

     //retrives the url of the thumbs. 
     var thumb_url = my_images[i][email protected]; 
     //creates temperary instance of loader 
     var thumb_loader = new Loader(); 
     //loads the thumbs 
     thumb_loader.load(new URLRequest(thumb_url)); 
     thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded); 

     //unique reference to each thumb to tell us what image to load from the xml array. 
     //thus I set thumb's name property as its number in the XML list. 
     thumb_loader.name = i; 

     //position the thumbs horizontially 
     thumb_loader.x = (my_thumb_width + 30)* x_counter; 

     thumb_loader.y = (my_thumb_height + 30)* y_counter; 


     if(x_counter+1 < columns){ 
      x_counter++; 
     } 
     else{ 
      x_counter = 0; 
      y_counter ++; 
     } 

     var preloader_pb:ProgressBar = new ProgressBar(); 
     preloader_pb.source = thumb_loader.contentLoaderInfo; 
     preloader_pb.x = thumb_loader.x; 
     preloader_pb.y = thumb_loader.y; 
     preloader_pb.width = my_thumb_width; 
     preloader_pb.height = my_thumb_height; 
     preloaders_mc.addChild(preloader_pb); 

     preloader_pb.addEventListener(Event.COMPLETE, donePb); 
    } 
} 


//loads the instances of thumbs to container_mc to be visable. 
function thumbLoaded(e:Event):void{ 
    var my_thumb:Loader = Loader(e.target.loader); 
    container_mc.addChild(my_thumb); 
    //trace(container_mc); 
    stage.addChild(container_mc); 
    movieClipArray.push(container_mc); 
    //trace(movieClipArray.length); 

    //Fading in Each Thumb once it Loads. 
    my_tweens[Number(my_thumb.name)]=new Tween(my_thumb, "alpha", Strong.easeIn, 0,1,0.5, true); 

    //event listener used to check the download process of our thumbnails. 
    //These are only used once in the gallery and are never required again. 
    my_thumb.contentLoaderInfo.removeEventListener(Event.COMPLETE, thumbLoaded); 

} 

//loads the full image by using an instance of loader class. 
function callFull(e:MouseEvent):void{ 
    var full_loader:Loader = new Loader(); 
    var full_url = my_images[e.target.name][email protected]; 
    full_loader.load(new URLRequest(full_url)); 
    //will only be displayed after it fully loaded 
    full_loader.contentLoaderInfo.addEventListener(Event.INIT, fullLoaded); 

    var full_pb:ProgressBar = new ProgressBar(); 
    full_pb.source = full_loader.contentLoaderInfo; 
    full_pb.x = 100; 
    full_pb.y = 400; 
    preloaders_mc.addChild(full_pb); 

    full_pb.addEventListener(Event.COMPLETE, donePb); 


    container_mc.removeEventListener(MouseEvent.CLICK, callFull); 
    container_mc.buttonMode = false; 
    //hover effects for full. 
    container_mc.removeEventListener(MouseEvent.MOUSE_OVER, onOver); 
    container_mc.removeEventListener(MouseEvent.MOUSE_OUT, onOut); 
    //Fading In and Out the Entire Gallery when a full image is shown 
    container_mc_tween = new Tween(container_mc, "alpha", Strong.easeIn, 1,0.5,0.5, true); 

} 

//will load the full pic and position it 
function fullLoaded(e:Event):void{ 
    //for the hand cursor. 
    full_mc = new MovieClip(); 
    full_mc.buttonMode = true; 
    addChild (full_mc); 
    var my_loader:Loader = Loader(e.target.loader); 
    full_mc.addChild(my_loader); // This line was addChild(my_loade), just add full_mc. before it. 
    //Fading In of Full Image 
    full_tween = new Tween(my_loader, "alpha", Strong.easeIn, 0,1,0.5, true); 
    my_loader.x = 150; 
    my_loader.y = 100 
    my_loader.addEventListener(MouseEvent.CLICK,removeFull); 

    my_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, fullLoaded); 
    //stage.addEventListener(MouseEvent.ROLL_OUT, disappear,false,0,true); 
    full_mc.addEventListener(MouseEvent.ROLL_OVER,appear); 

    } 




function removeFull(e:MouseEvent):void{ 
var my_loader:Loader = Loader (e.currentTarget); 
full_tween = new Tween(my_loader, "alpha", Strong.easeOut, 1,0,0.5, true); 
full_tween.addEventListener(TweenEvent.MOTION_FINISH, tweenFinished); 

container_mc_tween = new Tween(container_mc, "alpha", Strong.easeOut, 0.5,1,0.5, true); 
my_loader.addEventListener(MouseEvent.ROLL_OUT,disappear); 
} 

function tweenFinished (e:TweenEvent):void{ 
var my_loader:Loader = Loader (e.target.obj); 
my_loader.unload(); 
full_mc.removeChild(my_loader); // This line was removeChid(my_loader), just add full_mc before it. 
removeChild(full_mc); 
full_mc = null; 

container_mc.addEventListener(MouseEvent.CLICK, callFull); 
container_mc.buttonMode = true; 
container_mc.addEventListener(MouseEvent.MOUSE_OVER, onOver); 
container_mc.addEventListener(MouseEvent.MOUSE_OUT, onOut); 

var my_tween:Tween = Tween(e.target); 
my_tween.removeEventListener(TweenEvent.MOTION_FINISH, tweenFinished); 
} 

function donePb (e:Event):void{ 
    var my_pb:ProgressBar = ProgressBar(e.target); 
    preloaders_mc.removeChild(my_pb); 
    my_pb.removeEventListener(Event.COMPLETE, donePb); 
} 

function onOver (e:MouseEvent):void{ 
    var my_thumb:Loader = Loader(e.target); 
    my_thumb.alpha = 0.5; 
} 

function onOut (e:MouseEvent):void{ 
    var my_thumb:Loader = Loader (e.target); 
    my_thumb.alpha = 1; 
} 

//category buttons 
var animal_btn:SimpleButton = new animals_mc(); 
addChild(animal_btn); 
animal_btn.x = 50; 
animal_btn.y =550; 

var concert_btn:SimpleButton = new concert_mc(); 
addChild(concert_btn); 
concert_btn.x = 180; 
concert_btn.y =550; 

var festival_btn:SimpleButton = new festival_mc(); 
addChild(festival_btn); 
festival_btn.x = 310; 
festival_btn.y =550; 

var flowers_btn:SimpleButton = new flowers_mc(); 
addChild(flowers_btn); 
flowers_btn.x = 440; 
flowers_btn.y =550; 


flowers_btn.addEventListener(MouseEvent.CLICK, loadFlowers); 
function loadFlowers(event:MouseEvent) 
{ 
    currentCategory = "flower"; 
    removeAllMovieClips(); 
    processXMLData(); 
} 

animal_btn.addEventListener(MouseEvent.CLICK, loadAnimals); 
function loadAnimals(event:MouseEvent) 
{ 
    currentCategory = "animals"; 
    removeAllMovieClips(); 
    processXMLData(); 
} 

concert_btn.addEventListener(MouseEvent.CLICK, loadConcert); 
function loadConcert(event:MouseEvent) 
{ 
    currentCategory = "concert"; 
    removeAllMovieClips(); 
    processXMLData(); 
} 

festival_btn.addEventListener(MouseEvent.CLICK, loadFestival); 
function loadFestival(event:MouseEvent) 
{ 
    currentCategory = "festival"; 
    removeAllMovieClips(); 
    processXMLData(); 
} 

function removeAllMovieClips(){ 
    trace(movieClipArray.length); 
    for (var i =0; i<movieClipArray.length; i++){ 
     stage.removeChild(movieClipArray[i]); 
    } 
    movieClipArray = []; 
    //trace(myMovieClipsOnStage); 

} 

var menu_mc:MovieClip = new fullPicMenu_mc(); 
addChild(menu_mc); 
menu_mc.x = 150; 
menu_mc.y =460; 
menu_mc.visible= false; 




var share_btn:SimpleButton = new share_mc(); 
addChild(share_btn); 
share_btn.x = 80; 
share_btn.y =350; 
share_btn.visible= false; 

var fav_btn:SimpleButton = new fav_mc(); 
addChild(fav_btn); 
fav_btn.x = 93; 
fav_btn.y =400; 
fav_btn.visible= false; 

var caption_btn:SimpleButton = new caption_mc(); 
addChild(caption_btn); 
caption_btn.x = 80; 
caption_btn.y =460; 
caption_btn.visible= false; 

回答

0

你是推比一次相同container_mc更進movieClipArray,那麼你正試圖從兩次舞臺中刪除。無論是每一個拇指被加載,或完全擺脫了陣列的時間使一個新的容器中,在thumbLoaded()功能,因爲你只有一個容器。

+0

我只推陣一次,其他推被註釋掉註釋掉。如果我擺脫我的陣營altogeter我將如何刪除舞臺上目前的類別? – 2013-03-22 09:46:47

+0

你是餡這一切變成'container_mc'了,所以只是刪除MC起飛階段,並刪除鏈接,所以MC及其所有後代將得到GC'd。 – Vesper 2013-03-22 10:08:21

+0

所以在我的代碼到處刪除在container_mc? – 2013-03-22 10:14:12

0
function removeAllMovieClips(){ 
    trace(movieClipArray.length); 
    for (var i =0; i<movieClipArray.length; i++){ 
     if(movieClipArray[i] is DisplayObject && movieClipArray[i].parent == stage) 
      stage.removeChild(movieClipArray[i]); 
    } 
    movieClipArray = []; 
} 

我沒有經歷所有的代碼,找出爲什麼你的影片剪輯是不是階段的孩子,但是這將阻止錯誤。

你也可以做

if(movieClipArray[i].parent) movieClipArray[i].parent.removeChild(movieClipArray[i]); 
+0

是啊同樣的事情仍然發生,當我擊中類別按鈕它只是將拇指釘下移動。如果我繼續點擊類別按鈕,它只是在屏幕下移動。我也得到這個輸出錯誤:類型錯誤:錯誤#1009:無法訪問空對象引用的屬性或方法。 \t at myGallery_fla :: MainTimeline/processXMLData() \t at myGallery_fla :: MainTimeline/loadConcert() – 2013-03-22 10:05:33

+0

這對於我們來說是非常多的代碼來調試。你應該啓動一個調試來發現它在哪一行出現錯誤。 ther是一個在你的processXMLData()函數中爲null的對象,代碼在那裏停止。我不能說它是哪個對象。 – Boris 2013-03-22 10:17:12

+0

是啊我知道謝謝你的幫助:) – 2013-03-22 10:18:42

相關問題