Search code examples
spriteaddchildactionscript-3removechild

Help on Removal of Dynamically Created sprites


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++;
}

Hi, I am relatively new to flash and action script as well and I am having a problem getting the sprite to be removed after another is called. I am making emission spectrum's of 3 elements by dynamically generating the mask over a picture on the stage. Everything works perfectly fine with the code I have right now except the sprites stack on top of each other and I end up with bold lines all over my picture instead of a new set of lines each time i press a button.

I have tried using try/catch to remove the sprites and I have also rearranged the entire code from what is seen here to make 3 seperate entities (hoping I could remove them if they were seperate variables) instead of 2 functions that handle the whole process. I have tried everything to the extent of my knowledge (which is pretty minimal @ this point) any suggestions?

Thanks ahead of time!


Solution

  • My AS3 knowledge is rather rudimentary right now but I think two things may help you.

    1. You could use removeChild before recreating the Sprite. Alternatively, just reuse the Sprite.
    2. Try to add this[spriteName].graphics.clear(); to reset the sprite and start redrawing.
         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++;
        }