0

This is my first post ever :). I have quite a bit of experience in c# and a little in as3 and a lot in animation. Anyway onto the question.

I'm attempting to create a graphical representation of the microphone activity in flash for a conference.

I got a movieclip called eqNotch that's just a rectangle and its linkage name is EqNotch. I also have a movieclip where I am writing my as3 on the first frame, its called dynamicBar and linkage name is DynamicBar.

I've successfully written the code for the dynamicBar to create one column of eqNotches and the number of rows is dependent on mic activity. Here comes the problem: I place one on the stage, works fine. but i want to place about 25 and it only displays one. I tried loops creating dynamic items on the first frame on the stage. I tried dragging separately from the library I tried looping inside the dynamic bar code to move .x dependent on what loop it was in and inside that was the "while (this.numChildren < floater){..."

I want to be able to repeat the code of this object multiple times or something to that effect.

Code for 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;
}

UPDATE

Great idea Adam, I got it though but I am having trouble deleting the correct ones. I'm a little too far in to quit now and i want to learn this for the experience.

I had to create an array to record the height of each. It adds them properly but doesn't delete them correctly.

    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;
}
3
  • It doesn't look like the columns array is being set anywhere. Commented Jul 5, 2011 at 21:37
  • its set to zero when a column is created if its null, and then its ++ or -- depending on adds or removes. Commented Jul 6, 2011 at 4:14
  • 1
    Oh yes, I see. Well it seems like you are over complicating things. Creating and destroying objects like your EqNotch objects is expensive. I would recommend just creating all your notches and just turning the visible property on/off as needed. Commented Jul 6, 2011 at 4:27

2 Answers 2

3

I would probably just generate all your "notches" on init, and then turn there visibility on/off as needed. Maybe something like this:

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.
    }
}

This would be the basic idea, but you would need to implement it into your code.

Sign up to request clarification or add additional context in comments.

Comments

1

Got it.

Thought I may as well share it with the community :)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.