random code in AS2 convert to AS3,

2019-09-09 16:59发布

问题:

i try to make random quiz in flash cs5.5 for android, so i convert this AS2 code to AS3, but i have 1 error.

AS2:

stop();

benar = 0; salah = 0; total_frame = 11; array_frame = []; 
for (i=2; i<=total_frame; i++) {
    array_frame.push(i); 
}
array_frame.sort(function () {  
    return random(2) ? 1 : -1;
});
jumlah_frame = array_frame.length;

my AS3:

stop()

var benar=0;var salah=0; var jumlah_frame; var total_frame = 11; 
var array_frame = [];

var i:Number; for ( i=2; i<=total_frame; i++) {
    array_frame.push(i);
} 
array_frame.sort(function () {  
    return random(2) ? 1 : -1; 
});
jumlah_frame = array_frame.length;

my error was.. *array_frame.sort(function () { return random(2) ? 1 : -1;* 1180: Call to a possibly undefined method random.

Is my code already correct? i'm newbie in AS3, please somebody explain and correct this code or maybe teach me another effective code, thank you.. :)

回答1:

random(2); is Math.floor(Math.random() * 1);

function oldrandom(n):Number
{
    return Math.round(Math.random()*(n-1));
}


回答2:

I guess, You should random(2); convert to int(Math.random()*2);