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

发布于 2024-11-18 14:11:09 字数 3988 浏览 3 评论 0原文

这是我的第一篇文章:)。我在 c# 方面有相当多的经验,在 as3 方面也有一点经验,在动画方面也有很多经验。无论如何,先回答这个问题。

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

我有一个名为 eqNotch 的影片剪辑,它只是一个矩形,其链接名称是 EqNotch。 我还有一个电影剪辑,我在第一帧上写我的 as3,它称为dynamicBar,链接名称是DynamicBar。

我已经成功编写了 DynamicBar 的代码来创建一列 eqNotches,行数取决于麦克风活动。 问题来了:我把一个放在舞台上,效果很好。但我想放置大约 25 个,但它只显示一个。 我尝试在舞台上的第一帧上循环创建动态项目。 我尝试从库中单独拖动 我尝试在动态条形码内部循环以移动 .x ,具体取决于它所处的循环以及其中的“while (this.numChildren < floater){...”

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

动态栏代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心头的小情儿 2024-11-25 14:11:09

我可能会在 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.
    }
}

这将是基本想法,但您需要将其实现到您的代码中。

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.

一梦等七年七年为一梦 2024-11-25 14:11:09

知道了。

我想我不妨与社区分享:)

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文