Arrays 动作脚本3-帮助';保存';eventListener参数的值

Arrays 动作脚本3-帮助';保存';eventListener参数的值,arrays,actionscript-3,parameters,timer,Arrays,Actionscript 3,Parameters,Timer,这是我以前在这个网站上问过的第一个问题,我觉得我的英语不是很好,所以我为任何格式或语言错误提前道歉,而且我对编程也比较陌生,所以如果我错过了一些基本的东西或者看起来像个白痴,我也道歉 我一直在做一个有趣的小项目,我已经从零开始做了三次,尝试使用不同的方法,但我总是遇到同样的问题 我在这个项目中的最终目标是让用户能够点击舞台,将一个符号(30个符号中的1个,从0到29的数组中的下一个)移动到用户点击的位置,当与该符号相关的计时器(例如,symbol0到timer0,symbol 1到timer1等

这是我以前在这个网站上问过的第一个问题,我觉得我的英语不是很好,所以我为任何格式或语言错误提前道歉,而且我对编程也比较陌生,所以如果我错过了一些基本的东西或者看起来像个白痴,我也道歉

我一直在做一个有趣的小项目,我已经从零开始做了三次,尝试使用不同的方法,但我总是遇到同样的问题

我在这个项目中的最终目标是让用户能够点击舞台,将一个符号(30个符号中的1个,从0到29的数组中的下一个)移动到用户点击的位置,当与该符号相关的计时器(例如,symbol0到timer0,symbol 1到timer1等)完成计数时消失,用户将能够放置多个符号,从而启动多个计时器。 加载程序时,将创建一个计时器数组,for循环将每个计时器的名称添加到数组中,如下所示:

var timerNamesGreen:Array = new Array();
function addTimerName(s:String)
{
return("timer"+s)
}
for(var i:int = 0;i<5;i++)
{
timerNamesGreen.push(addTimerName("Green")+i)
}
换句话说,我想有一种方法来识别哪个timer eventListener正在调用timeHandler函数,但我想不出如何做

我试图使用这样一种方法,以避免出现大量几乎相同的代码行,比如一个接一个地声明计时器(我计划使用另外两种颜色,可能超过30个计时器,因此至少有90行用于声明计时器,另外90行或更多用于事件侦听器)

我很抱歉,如果有更多的信息超过需要,或缺乏。如果有任何更正,或需要澄清,请不要犹豫,纠正我


非常感谢您提供的任何信息。

您将从创建对象类中受益匪浅。您可以编写希望每个符号/计时器对具有的所有功能,并在单击时实例化新副本。也就是说,为了直截了当,我将在您试图解决它的上下文中解决这个问题


您可以将计时器存储为对象的键,而不是使用匿名函数来传递和自定义索引。这样,当从侦听器引用
TimerEvent.target
时,您可以立即找到与其关联的对象。最终,虽然您可以使用自己的方法,但修改/创建自己的事件可能会对代码造成影响,并(可能)增加令人沮丧的结果

下面的代码创建了一个简单的演示,可以在任何地方单击创建符号,并使用计时器的
currentCount
更新每个圆圈的标签

function createCircle():MovieClip {
    // This creates a circle, with random color.
    var circle:MovieClip = new MovieClip();
    circle.graphics.beginFill(Math.floor(Math.random() * (1+0xFFFFFF-0x000000)) + 0x000000);
    circle.graphics.drawCircle(22, 28, 15);
    circle.graphics.endFill();

    // We'll update this textfield ever time the timer ticks
    var txt:TextField = new TextField();
    txt.name = "txt";
    txt.selectable = false;
    circle.addChild(txt);

    return circle;
}

var currentIndex:int = 0; // Keeps track of which symbol to use
var symbolArr:Array = []; // Stores our circle and timer
var timerToSymbol:Dictionary = new Dictionary(); // Uses timer as the key for each circle

stage.addEventListener("mouseUp", stageClick)

// Create X number of circles in the array;
for (var i:int = 0; i < 30; i++) {
    symbolArr[i] = {
        "circle":createCircle(),
        "timer":new Timer(1000, 5)
    }

    timerToSymbol[symbolArr[i].timer] = symbolArr[i].circle;
    symbolArr[i].timer.addEventListener(TimerEvent.TIMER, updateSymbol);
}

function stageClick(e:MouseEvent):void {
    // Place the next circle at mouse coordinates
    addChild(symbolArr[currentIndex].circle);
    symbolArr[currentIndex].circle.x = e.stageX;
    symbolArr[currentIndex].circle.y = e.stageY;

    // Reset the timer (and text), and start the countdown.
    symbolArr[currentIndex].timer.reset();
    symbolArr[currentIndex].circle.getChildByName('txt').text = "0";
    symbolArr[currentIndex].timer.start();

    // Update our index within the confines of the array length.
    currentIndex = (currentIndex + 1) % symbolArr.length;
}

function updateSymbol(e:TimerEvent):void {
    // Every time we tick, update the text to the current count.
    var txt:TextField = timerToSymbol[e.target].getChildByName('txt');
    txt.text = e.target.currentCount;
}
函数createCircle():MovieClip{
//这将创建一个具有随机颜色的圆。
变量循环:MovieClip=新的MovieClip();
circle.graphics.beginll(Math.floor(Math.random()*(1+0xFFFFFF-0x000000))+0x000000);
圆圈。图形。画圈(22,28,15);
circle.graphics.endFill();
//我们将在计时器计时时更新此文本字段
var txt:TextField=new TextField();
txt.name=“txt”;
txt.exe=false;
circle.addChild(txt);
返回圈;
}
var currentIndex:int=0;//跟踪要使用的符号
var symbolArr:Array=[];//存储我们的圆圈和计时器
var timerToSymbol:Dictionary=new Dictionary();//使用计时器作为每个圆圈的键
stage.addEventListener(“mouseUp”,stageClick)
//在数组中创建X个圆;
对于(变量i:int=0;i<30;i++){
symbolArr[i]={
“圆”:createCircle(),
“定时器”:新定时器(1000,5)
}
timerToSymbol[symbolArr[i]。计时器]=symbolArr[i]。圆圈;
symbolArr[i].timer.addEventListener(TimerEvent.timer,updateSymbol);
}
函数stageClick(e:MouseEvent):void{
//在鼠标坐标处放置下一个圆
addChild(symbolArr[currentIndex].circle);
symbolArr[currentIndex].circle.x=e.stageX;
symbolArr[currentIndex].circle.y=e.stageY;
//重置计时器(和文本),然后开始倒计时。
symbolArr[currentIndex].timer.reset();
symbolArr[currentIndex].circle.getChildByName('txt').text=“0”;
symbolArr[currentIndex].timer.start();
//在数组长度范围内更新索引。
currentIndex=(currentIndex+1)%symbolArr.length;
}
函数updateSymbol(e:TimerEvent):void{
//每次勾选时,将文本更新为当前计数。
var-txt:TextField=timerToSymbol[e.target].getChildByName('txt');
txt.text=e.target.currentCount;
}
function timeHandler(event:TimerEvent, i:int)
{
    if(timerNamesGreen[i].currentCount = 5)
    {
    timerNamesGreen[i].reset();
    timersActiveGreen[i] = 0;
    }
}
function createCircle():MovieClip {
    // This creates a circle, with random color.
    var circle:MovieClip = new MovieClip();
    circle.graphics.beginFill(Math.floor(Math.random() * (1+0xFFFFFF-0x000000)) + 0x000000);
    circle.graphics.drawCircle(22, 28, 15);
    circle.graphics.endFill();

    // We'll update this textfield ever time the timer ticks
    var txt:TextField = new TextField();
    txt.name = "txt";
    txt.selectable = false;
    circle.addChild(txt);

    return circle;
}

var currentIndex:int = 0; // Keeps track of which symbol to use
var symbolArr:Array = []; // Stores our circle and timer
var timerToSymbol:Dictionary = new Dictionary(); // Uses timer as the key for each circle

stage.addEventListener("mouseUp", stageClick)

// Create X number of circles in the array;
for (var i:int = 0; i < 30; i++) {
    symbolArr[i] = {
        "circle":createCircle(),
        "timer":new Timer(1000, 5)
    }

    timerToSymbol[symbolArr[i].timer] = symbolArr[i].circle;
    symbolArr[i].timer.addEventListener(TimerEvent.TIMER, updateSymbol);
}

function stageClick(e:MouseEvent):void {
    // Place the next circle at mouse coordinates
    addChild(symbolArr[currentIndex].circle);
    symbolArr[currentIndex].circle.x = e.stageX;
    symbolArr[currentIndex].circle.y = e.stageY;

    // Reset the timer (and text), and start the countdown.
    symbolArr[currentIndex].timer.reset();
    symbolArr[currentIndex].circle.getChildByName('txt').text = "0";
    symbolArr[currentIndex].timer.start();

    // Update our index within the confines of the array length.
    currentIndex = (currentIndex + 1) % symbolArr.length;
}

function updateSymbol(e:TimerEvent):void {
    // Every time we tick, update the text to the current count.
    var txt:TextField = timerToSymbol[e.target].getChildByName('txt');
    txt.text = e.target.currentCount;
}