2011-07-05 14 views
0

これは初めての投稿です:)。私はC#でかなりの経験があり、as3では少し、アニメーションではたくさんの経験があります。とにかく質問に。ループでフラッシュ内に複数の動的オブジェクトを作成

会議のマイクアクティビティをフラッシュでグラフィカルに表示しようとしています。

私はeqNotchという矩形のムービークリップを取得しました。その連結名はEqNotchです。 私は最初のフレームに私のas3を書いているムービークリップを持っています。それはdynamicBarと呼ばれ、リンケージ名はDynamicBarです。

私は、dynamicBarがeqNotchesの1つの列を作成し、行数がマイクの動作に依存していることに成功しました。 ここで問題が発生します:私はステージに1つ置き、うまく動作します。しかし、私は約25を配置したいとそれは1つだけを表示します。 ステージ上の最初のフレームに動的アイテムを作成するループを試しました。 Iは、依存.X移動させるために、私は動的バーコード内側ループ試みライブラリ 別にドラッグしようとしたものがで、その内部にあったループ「一方(this.numChildren <フロータ){...」

このオブジェクトのコードを何度も繰り返すことができるようにしたい。 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

素晴らしいアイデアアダムは、私もそれを得たが、私は、トラブル正しいものを削除を抱えています。 私はちょっと遠すぎてやめてしまいました。私はこれを経験のために学びたいと思います。

それぞれの高さを記録するために配列を作成する必要がありました。それらは正しく追加されますが、正しく削除されません。

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

それは*配列は、いずれかの場所に設定されている*列のようには見えません。 –

+0

列が作成されたときにゼロに設定され、その列がnullの場合は++に、または+または - が追加または削除に応じてセットされます。 – MrJD

+1

ああ、そうだよ。まあ、あなたは物事を複雑にしているようです。 EqNotchオブジェクトのようなオブジェクトを作成して破棄するのは高価です。ノッチをすべて作成し、必要に応じて可視プロパティをオン/オフにすることをお勧めします。 –

答えて

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

これが基本的な考えですが、あなたはあなたのコードにそれを実装する必要があります。

+0

ちょっとアダム私の更新を確認してください – MrJD

関連する問題