关于删除动态创建的精灵的帮助

发布于 2024-09-02 13:46:53 字数 2365 浏览 8 评论 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 个函数。我已经在我所知的范围内尝试了一切(在这一点上这是相当少的)有什么建议吗?

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2024-09-09 13:46:53

我现在对 AS3 的了解还很初级,但我认为有两件事可能对你有帮助。

  1. 您可以在重新创建 Sprite 之前使用removeChild。或者,只需重复使用 Sprite。
  2. 尝试添加 this[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++;
    }

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++;
    }
來不及說愛妳 2024-09-09 13:46:53

以防万一有人好奇或遇到类似的问题。修复非常简单,但这就是我所做的。

还应该提到的是,我不认为 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
}

Just in case anyone was curious or having a similar problem. Extremely simple fix but here is what I did.

Also should mention that I don't think that the graphics.clear function actually fixed the problem (though I didn't have the sprite being cleared properly before), but I believe the problem lies in the beginning of the onloaded function where 3 of those variables used to be outside of the function.

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文