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 个处理整个过程的函数. 我已经尽我所能尝试了一切(这在这一点上非常少)有什么建议吗?

提前谢谢!

4

2 回答 2

0

我的 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++;
    }
于 2010-05-25T01:47:35.087 回答
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
}
于 2010-05-27T19:59:25.527 回答