如何获得数组中的下一个元素并显示它

时间:2013-12-11 23:16:45

标签: actionscript-3 flash flash-cc

如何获取下一个数组并在文本字段中一次又一次地在循环中显示它,直到数组达到其最大值,有一个计时器在影片剪辑触及空间(movieclip)时被触发每当定时器被触发时,定时器使氧气下降数十次

oTimer.addEventListener(TimerEvent.TIMER, O2);
stage.addEventListener(Event.ENTER_FRAME, outerSpace);

var oPercent:Array = ["100", "90", "80", "70", "60", "50", "40", "30", "20", "10", "0"]
var txtFld:TextField = new TextField();
var oTimer:Timer = new Timer();
addChild(txtFld);
txtFld.text = "Oxygen: " + oPercent[0];


function O2(evt:TimerEvent)
{
    for(var i:int = 0; i < 10; i++)
        {    
            oTimer.start();
            txtFld.text = "Oxygen: " + oPercent[];
        }
}

function outerSpace(evt:TimerEvent)
{
    if(char.hitTestObject(Space))
    {
        oTimer.start();
    {
}

2 个答案:

答案 0 :(得分:0)

您希望使用Array.length作为最大值。您可以通过Array[INDEX]

访问数组中的项目
var i:uint;
var l:uint = oPercent.length; //total number of items in array

for ( i = 0; i < l; i++ ) {
    trace( oPercent[i] ); // trace out this item in the array
}

答案 1 :(得分:0)

我希望有可能提供帮助。我尝试编辑你的代码,修复一些错误并实现你的想法。

const OXYGEN_DELAY:uint = 100;

//creating a simple index control
var oxygenControl:uint = 0;
//Vector instead Array
var oPercent:Vector.<String> = new <String>['100', '90', '80', '70', '60', '50', '40', '30', '20', '10', '0'];
var oPercentLength:uint = oPercent.length - 1;

//Timer Class has a mandatory parameter 'delay:Number'
var oTimer:Timer = new Timer(OXYGEN_DELAY);

var txtFld:TextField = new TextField();
addChild(txtFld);
txtFld.text = "Oxygen: " + oPercent[oxygenControl];

oTimer.addEventListener(TimerEvent.TIMER, O2TimerHandler, false, 0, true);
stage.addEventListener(Event.ENTER_FRAME, outerSpaceHandler, false, 0, true);

function O2TimerHandler(event:TimerEvent):void
{
   txtFld.text = (oxygenControl <  oPercentLength) ? 'Oxygen: ' + oPercent[++oxygenControl] : gameOver();
}

function gameOver():String
{
//clean up and so on...
oTimer.removeEventListener(TimerEvent.TIMER, O2TimerHandler);
stage.removeEventListener(Event.ENTER_FRAME, outerSpaceHandler);

return ':(';
}

function outerSpaceHandler(event:Event):void
{
   (char.hitTestObject(space)) ? oTimer.start() : oTimer.stop();
}