2010-05-24 40 views
0
import flash.display.Sprite; 
import flash.net.URLLoader; 

var index:int = 0; 
var constY = 291; 
var constW = 2; 
var constH = 40; 

hydrogenBtn.label = "Hydrogen"; 
heliumBtn.label = "Helium"; 
lithiumBtn.label = "Lithium"; 
hydrogenBtn.addEventListener (MouseEvent.CLICK, loadHydrogen); 
heliumBtn.addEventListener (MouseEvent.CLICK, loadHelium); 
lithiumBtn.addEventListener (MouseEvent.CLICK, loadLithium); 

var myTextLoader:URLLoader = new URLLoader(); 
    myTextLoader.addEventListener(Event.COMPLETE, onLoaded); 

function loadHydrogen (event:Event):void { 
    myTextLoader.load(new URLRequest("hydrogen.txt")); 
} 
function loadHelium (event:Event):void { 
    myTextLoader.load(new URLRequest("helium.txt")); 
} 
function loadLithium (event:Event):void { 
    myTextLoader.load(new URLRequest("lithium.txt")); 
} 

var DataSet:Array = new Array(); 
var valueRead1:String; 
var valueRead2:String; 

function onLoaded(event:Event):void { 
    var rawData:String = event.target.data; 

    for(var i:int = 0; i<rawData.length; i++){ 
     var commaIndex = rawData.search(","); 

     valueRead1 = rawData.substr(0,commaIndex); 
     rawData = rawData.substr(commaIndex+1, rawData.length+1); 
     DataSet.push(valueRead1); 
     commaIndex = rawData.search(","); 

     if(commaIndex == -1) {commaIndex = rawData.length+1;} 

     valueRead2 = rawData.substr(0,commaIndex); 
     rawData = rawData.substr(commaIndex+1, rawData.length+1); 
     DataSet.push(valueRead2); 
    } 
    generateMask_Emission(DataSet); 
} 

function generateMask_Emission(dataArray:Array):void{ 
    var spriteName:String = "Mask"+index; 
    trace(spriteName); 
    this[spriteName] = new Sprite(); 

    for (var i:int=0; i<dataArray.length; i+=2){ 
     this[spriteName].graphics.beginFill(0x000000, dataArray[i+1]); 
     this[spriteName].graphics.drawRect(dataArray[i],constY,constW, constH); 
     this[spriteName].graphics.endFill(); 
    } 
    addChild(this[spriteName]); 
index++; 
} 

嗨,我相對比較新的Flash和動作腳本以及我有一個問題讓精靈被調用後被刪除。我通過在舞臺上的圖片上動態生成蒙版來製作3個元素的發射光譜。一切工作完美罰款與我現在的代碼,除了精靈堆疊在彼此的頂部,並最終在我的圖片結束了大膽的線條,而不是每次我按下按鈕時,一組新的線。幫助刪除動態創建的精靈

我曾嘗試使用try/catch來刪除精靈,我也重新安排了從這裏看到的所有代碼,以使3個獨立的實體(希望我可以刪除它們,如果他們是單獨變量)而不是2個函數處理整個過程。我已經嘗試了所有的知識(在這一點上這是相當簡單的)任何建議?

提前致謝!

回答

0

我的AS3知識現在比較基本,但我認爲兩件事可能會對你有所幫助。

  1. 您可以在重新創建Sprite之前使用removeChild。或者,只需重用Sprite即可。
  2. 嘗試添加[spriteName] .graphics.clear();重置精靈並開始重繪。
 function generateMask_Emission (dataArray : Array) : void { 
     var spriteName:String = "Mask"+index; 
     trace(spriteName); 
     // Don't recreate if sprite object already created 
     if (this[spriteName] == null) 
     { 
      this[spriteName] = new Sprite(); 
      // Only need to add sprite to display object once 
      addChild(this[spriteName]); 
     } 
     for (var i:int= 0; i < dataArray.length; i+=2) 
     { 
      this[spriteName].graphics.clear(); 
      this[spriteName].graphics.beginFill(0x000000, dataArray[i+1]); 
      this[spriteName].graphics.drawRect(dataArray[i],constY,constW, constH); 
      this[spriteName].graphics.endFill(); 
     } 
     index++; 
    }
+0

問題解決了。我不得不使用graphics.clear函數,但是直到我重新安排了我的代碼才工作,即使我沒有添加或減少任何東西以使其正常工作。喜歡編程...永遠完美無缺! (諷刺)感謝您的幫助btw! – 2010-05-27 19:40:28

0

萬一有人很好奇,或有類似的問題。非常簡單的修復,但這是我做的。

另外應該提及的是,我不認爲graphics.clear函數實際上解決了問題(雖然我沒有在之前正確清除精靈),但我相信問題在於onloaded的開始函數,其中3個變量用於函數之外。

import flash.display.Sprite; 
import flash.net.URLLoader; 
import flash.events.Event; 

var constY = 291; //this value represets the Y value of the bottom of the background spectrum image 
var constW = 2; //this value represents the width of every emission line 
var constH = 40; //this value represents the height of every emission line 

//Create Button Labels 
hydrogenBtn.label = "Hydrogen"; 
heliumBtn.label = "Helium";  
lithiumBtn.label = "Lithium"; 

//These listen for the buttons to be clicked to begin loading in the data 
hydrogenBtn.addEventListener (MouseEvent.CLICK, loadHydrogen); 
heliumBtn.addEventListener (MouseEvent.CLICK, loadHelium);  
lithiumBtn.addEventListener (MouseEvent.CLICK, loadLithium); 

var myTextLoader:URLLoader = new URLLoader();//the object to load in data from external files 
    myTextLoader.addEventListener(Event.COMPLETE, onLoaded);//triggers the function when the file is loaded 

var Mask:Sprite = new Sprite(); //This sprite will hold the information for the spectrum to be put on stage 

function loadHydrogen (event:Event):void { 
    myTextLoader.load(new URLRequest("hydrogen.txt"));//starts loading Hydrogen emisson data 
} 
function loadHelium (event:Event):void { 
    myTextLoader.load(new URLRequest("helium.txt"));//starts loading Helium emission data 
} 
function loadLithium (event:Event):void { 
    myTextLoader.load(new URLRequest("lithium.txt"));//starts loading Lithium emission data 
} 


function onLoaded(event:Event):void {//the function that handles the data from the external file 

    var rawData:String = event.target.data; //create a new string and load in the data from the file 
    var DataSet:Array = new Array();//the array to load values in to 
    var valueRead1:String; //subset of array elements (n) 
    var valueRead2:String; //subset of array elements (n+1) 

    for(var i:int = 0; i<rawData.length; i++){ //loop through the string and cut up the data @ commas 
     var commaIndex = rawData.search(","); 

     valueRead1 = rawData.substr(0,commaIndex); 
     rawData = rawData.substr(commaIndex+1, rawData.length+1); 
     DataSet.push(valueRead1); 
     commaIndex = rawData.search(","); 

     if(commaIndex == -1) {commaIndex = rawData.length+1;} 

     valueRead2 = rawData.substr(0,commaIndex); 
     rawData = rawData.substr(commaIndex+1, rawData.length+1); 
     DataSet.push(valueRead2); 
    } 
    generateMask_Emission(DataSet);//call the generateMaskEmission function on new data to fill emission lines 
} 

//This function loops through an array, setting alternating values as locations and alphas 
function generateMask_Emission(dataArray:Array):void{ 
    Mask.graphics.clear(); //Clears the Mask sprite for the next set of values 
    addChild(Mask); //Adds the blank sprite in order to clear the stage of old sprites 

    //This loop actually draws out how the sprite should look before it is added  
    for (var i:int=0; i<dataArray.length; i+=2){ 
     Mask.graphics.beginFill(0x000000, dataArray[i+1]); 
     Mask.graphics.drawRect(dataArray[i],constY,constW, constH); 
     Mask.graphics.endFill(); 
    } 
    addChild(Mask);// actually adds the mask we have created to the stage 
}