-1

我如何获取其中的下一个数组并在循环中一次又一次地在文本字段中显示它,直到数组达到其最大值有一个计时器在影片剪辑触及空间(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();
    {
}
4

2 回答 2

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
}
于 2013-12-11T23:50:13.943 回答
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();
}
于 2013-12-12T16:08:29.933 回答