使用循环在flash中创建多个动态对象
这是我的第一篇帖子:)。我在c#方面有相当多的经验,在as3方面有一点经验,在动画方面也有很多经验。不管怎样,我还是要回答这个问题 我正在尝试用flash为会议创建麦克风活动的图形表示 我有一个叫做eqNotch的movieclip,它只是一个矩形,它的链接名是eqNotch。 我还有一个movieclip,我在第一帧上写我的as3,它叫dynamicBar,链接名是dynamicBar 我已经成功地为dynamicBar编写了创建一列EqNotch的代码,行数取决于麦克风活动。 问题来了:我把一个放在舞台上,效果很好。但我想放置25个,它只显示一个。 我尝试在舞台上的第一帧创建动态项目。 我试着从图书馆里分开拖 我试着在动态条码中循环移动.x,这取决于它在哪个循环中,在这个循环中是“while(this.numChildren使用循环在flash中创建多个动态对象,flash,actionscript-3,while-loop,children,Flash,Actionscript 3,While Loop,Children,这是我的第一篇帖子:)。我在c#方面有相当多的经验,在as3方面有一点经验,在动画方面也有很多经验。不管怎样,我还是要回答这个问题 我正在尝试用flash为会议创建麦克风活动的图形表示 我有一个叫做eqNotch的movieclip,它只是一个矩形,它的链接名是eqNotch。 我还有一个movieclip,我在第一帧上写我的as3,它叫dynamicBar,链接名是dynamicBar 我已经成功地为dynamicBar编写了创建一列EqNotch的代码,行数取决于麦克风活动。 问题来了:我把
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;
}
导入flash.media.micro;
导入flash.events.SampleDataEvent;
导入flash.utils.ByteArray;
var my_mic:micromic=micromic.getmicromic();
我的最低工资率=22;
我的麦克风增益=100;
my_mic.addEventListener(SampleDataEvent.SAMPLE_数据,drawSampleData);
var myTimer:Timer=新定时器(20,8);
myTimer.addEventListener(TimerEvent.TIMER,timerListener);
var-lastData:int;
var定时清除器;
var myData:ByteArray;
函数timerListener(e:TimerEvent):无效
{
var currentData:int=1;
var浮动汇率:int=1;
尝试
{
//currentData=myData.readFloat()*30;
currentData=my_mic.activityLevel/5;
}
捕获(e:错误)
{
currentData=1;
}
尝试
{
浮动=数学abs((当前数据*3+lastData)/4));/-(stage.x/2))
}
捕获(e:错误)
{
浮动=当前数据;
}
lastData=当前数据;
如果(浮子>20)
{
漂浮物=20;
}
如果(浮子<1)
{
浮子=1;
}
var numocols:int=5;
var j:int;
变量i:int;
while(this.numChildren
更新 好主意,亚当,虽然我知道了,但是我在删除正确的部分时遇到了麻烦。 我现在离辞职有点太远了,我想通过学习来体验一下 我必须创建一个数组来记录每个数组的高度。它正确地添加了它们,但没有正确地删除它们
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;
}
导入flash.media.micro;
导入flash.events.SampleDataEvent;
导入flash.utils.ByteArray;
var my_mic:micromic=micromic.getmicromic();
我的最低工资率=22;
我的麦克风增益=100;
my_mic.addEventListener(SampleDataEvent.SAMPLE_数据,drawSampleData);
var myTimer:Timer=新定时器(20,8);
myTimer.addEventListener(TimerEvent.TIMER,timerListener);
var-lastData:int;
var定时清除器;
var myData:ByteArray;
var numocols:int=25;
变量列:数组=新数组();
/*对于(变量iLoop:iLoop=0;iLoop 20)
{
漂浮物=20;
}
如果(浮子<1)
{
浮子=1;
}
对于(var j:int=0;j我可能只会在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.
}
}
const numNotches:int=20;
变量浮点值:int=0;//0-20之间的卷值
var缺口:数组;
initNotches();
函数initNotches():void
{
对于(变量i:int=0;i
这是一个基本的想法,但您需要在代码中实现它。我可能只需要在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.
}
}
const numNotches:int=20;
变量浮点值:int=0;//0-20之间的卷值
var缺口:数组;
initNotches();
函数initNotches():void
{
对于(变量i:int=0;i
这是一个基本的想法,但您需要在代码中实现它。明白了
我想我不妨与社区分享:)
导入flash.media.micro;
导入flash.events.SampleDataEvent;
导入flash.utils.ByteArray;
var my_mic:micromic=micromic.getmicromic();
我的最低工资率=22;
我的麦克风增益=100;
my_mic.addEventListener(SampleDataEvent.SAMPLE_数据,drawSampleData);
var myTimer:Timer=新定时器(20,8);
myTimer.addEventListener(TimerEvent.TIMER,timerList