0

私のループには、絶えず変化している数があります-数が増加しているか減少しているかを判断する方法を理解する必要があります:

動作しないいくつかの擬似コード:)

    var now:Number; 
    var then:Number;

    function loop():void 
    {
        now = changingNumber;
        then = changingNumber;
        if (now > then) {
            // increasing
        }else {
            // decreasing
        }
    }
4

1 に答える 1

7
var now:int = 0;
var thn:int = 0;

function loop():void
{
    // Change the number.
    now = changingNumber;

    if(now > thn)
    {
        trace("Got larger.");
    }
    else if(now < thn)
    {
        trace("Got smaller.");
    }
    else
    {
        trace("Maintained value.");
    }

    // Store the changed value for comparison on new loop() call.
    // Notice how we do this AFTER 'now' has changed and we've compared it.
    thn = now;
}

または、自分の価値に合わせてゲッターとセッターを準備し、そこで増減を管理することもできます。

// Properties.
var _now:int = 0;
var _thn:int = 0;


// Return the value of now.
function get now():int
{
    return _now;
}


// Set a new value for now and determine if it's higher or lower.
function set now(value:int):void
{
    _now = value;

    // Comparison statements here as per above.
    // ...

    _thn = _now;
}

この方法はより効率的で、ループを必要としません。

于 2012-07-05T09:38:15.500 に答える