使用循环在flash中创建多个动态对象

时间:2011-07-05 01:00:47

标签: flash actionscript-3 while-loop children

这是我的第一篇文章:)。我在c#中有相当多的经验,在动画中有很多as3和很多。无论如何都要回答这个问题。

我正在尝试在会议的闪存中创建麦克风活动的图形表示。

我有一个名为eqNotch的动画片段,它只是一个矩形,其连接名称为EqNotch。 我还有一个movieclip,我在第一帧写我的as3,它叫做dynamicBar,连接名称是DynamicBar。

我已经成功编写了dynamicBar的代码来创建一列eqNotches,行数取决于mic活动。 问题出现了:我把一个放在舞台上,工作正常。但我想放置约25,它只显示一个。 我尝试在舞台的第一帧上创建动态项目的循环。 我试着从库中单独拖动 我尝试在动态条形码内部循环以移动.x,这取决于它所在的内部循环,即“while(this.numChildren< floater){...”

我希望能够多次重复此对象的代码或类似的事情。

dynamicBar代码:

import flash.media.Microphone;
import flash.events.SampleDataEvent;
import flash.utils.ByteArray;

var my_mic:Microphone = Microphone.getMicrophone();
my_mic.rate = 22;
my_mic.gain = 100;
my_mic.addEventListener(SampleDataEvent.SAMPLE_DATA, drawSampleData);
var myTimer:Timer = new Timer(20,8);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
var lastData:int;
var timeRemover;
var myData:ByteArray;


function timerListener(e:TimerEvent):void
{
    var currentData:int = 1;
    var floater:int = 1;
    try
    {
        //currentData = myData.readFloat()*30;
        currentData = my_mic.activityLevel/5;
    }
    catch (e:Error)
    {
        currentData = 1;
    }


    try
    {
        floater = Math.abs(((currentData*3 + lastData)/4)); //- (stage.x/2))
    }
    catch (e:Error)
    {
        floater = currentData;
    }
    lastData = currentData;

    if (floater > 20)
    {
        floater = 20;
    }
    if (floater < 1)
    {
        floater = 1;
    }
    var numOfCols:int = 5;
    var j:int;

    var i:int;

    while (this.numChildren < floater)
    {
        i++;
        var eqNotch:EqNotch = new EqNotch();
        eqNotch.y = i * -10;
        try
        {
            addChild(eqNotch);
        }
        catch (e:Error)
        {
        }
    }
    this.removeChildAt(this.numChildren-1);
}

function drawSampleData(eventObject:SampleDataEvent):void
{
    myTimer.start();
    myData = eventObject.data;
}

更新

好主意亚当,我得到了它,但我无法删除正确的。 我现在离开的时间有点太远,我想学习这个经验。

我必须创建一个数组来记录每个的高度。它正确添加它们但不能正确删除它们。

    import flash.media.Microphone;
    import flash.events.SampleDataEvent;
import flash.utils.ByteArray;

var my_mic:Microphone = Microphone.getMicrophone();
my_mic.rate = 22;
my_mic.gain = 100;
my_mic.addEventListener(SampleDataEvent.SAMPLE_DATA, drawSampleData);
var myTimer:Timer = new Timer(20,8);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
var lastData:int;
var timeRemover;
var myData:ByteArray;

var numOfCols:int = 25;
var columns:Array = new Array();
/*for (var iLoop:iLoop=0;iLoop<numOfCols;iLoop++){
    column[
}*/



function timerListener(e:TimerEvent):void
{
    var currentData:int = 1;
    var floater:int = 1;
    try
    {
        //currentData = myData.readFloat()*30;
        currentData = my_mic.activityLevel / 5;
    }
    catch (e:Error)
    {
        currentData = 1;
    }


    try
    {
        floater = Math.abs(((currentData*3 + lastData)/4));//- (stage.x/2))
    }
    catch (e:Error)
    {
        floater = currentData;
    }
    lastData = currentData;

    if (floater > 20)
    {
        floater = 20;
    }
    if (floater < 1)
    {
        floater = 1;
    }

    for(var j:int = 0; j<numOfCols;j++)
    {

        var notchDistance:int = j * 30;
        if(columns[j]==null){
            columns[j]=0;
        }
        while (int(columns[j]) <= floater)
        {
            var eqNotch:EqNotch = new EqNotch();
            eqNotch.x = notchDistance;
            eqNotch.y = int(columns[j]) * -7;
            addChild(eqNotch);
            columns[j]++;
        }
        if(int(columns[j]) >= floater){
        columns[j]--;
        this.removeChildAt(int(columns[j])*(j+1)-1-j);
        trace("height"+columns[j]+"col"+j);
        }
    }
}

function drawSampleData(eventObject:SampleDataEvent):void
{
    myTimer.start();
    myData = eventObject.data;
}

2 个答案:

答案 0 :(得分:3)

我可能只是在init上生成所有“缺口”,然后根据需要打开/关闭可见性。也许是这样的:

const numNotches:int = 20;

var floater:int = 0; // Value of volume from 0-20
var notches:Array;

initNotches();

function initNotches():void 
{
    for (var i:int = 0; i < numNotches; i++) 
    {
        var newNotch:EqNotch = new EqNotch();
        newNotch.y = i * -10;
        newNotch.visible = false;
        addChild(newNotch);
        notches[i] = newNotch; // Add newly created EqNotch to the notches array, in position.
    }
}

function dataUpdated():void 
{
    floater = ;// Set floater based on data, and bound it between 0-20.

    for (var i:int = 0; i < numNotches; i++) 
    {
        var notch:EqNotch = notches[i] as EqNotch;
        notch.visible = (i < floater); // Show or hide notch based on volume level.
    }
}

这是基本的想法,但您需要在代码中实现它。

答案 1 :(得分:1)

知道了。

我想也许可以与社区分享:)

import flash.media.Microphone;
import flash.events.SampleDataEvent;
import flash.utils.ByteArray;

var my_mic:Microphone = Microphone.getMicrophone();
my_mic.rate = 22;
my_mic.gain = 100;
my_mic.addEventListener(SampleDataEvent.SAMPLE_DATA, drawSampleData);
var myTimer:Timer = new Timer(20,8);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
var lastData:int;
var timeRemover;
var myData:ByteArray;

var numOfCols:int = 25;
var columns:Array = new Array();
/*for (var iLoop:iLoop=0;iLoop<numOfCols;iLoop++){
    column[
}*/



function timerListener(e:TimerEvent):void
{
    var currentData:int = 1;
    var floater:int = 1;
    try
    {
        //currentData = myData.readFloat()*30;
        currentData = my_mic.activityLevel / 5;
    }
    catch (e:Error)
    {
        currentData = 1;
    }


    try
    {
        floater = Math.abs(((currentData*3 + lastData)/4));//- (stage.x/2))
    }
    catch (e:Error)
    {
        floater = currentData;
    }
    lastData = currentData;

    if (floater > 20)
    {
        floater = 20;
    }
    if (floater < 1)
    {
        floater = 1;
    }

    for(var j:int = 0; j<numOfCols;j++)
    {

        var notchDistance:int = j * 30;
        if(columns[j]==null){
            columns[j]=0;
        }
        while (int(columns[j]) <= floater)
        {   
            var eqNotch:EqNotch = new EqNotch();
            eqNotch.x = notchDistance;
            eqNotch.y = int(columns[j]) * -7;
            addChild(eqNotch);
            columns[j]++;

        }
        if(int(columns[j]) >= floater){
        columns[j]--;
        this.removeChildAt((int(columns[j])*j)+((j+1)*(int(columns[j])-1)));
        trace("height"+columns[j]+"col"+j);

        }
    }
}

function drawSampleData(eventObject:SampleDataEvent):void
{
    myTimer.start();
    myData = eventObject.data;
}