Can we recall a set of variable inside the Sequence Array?

104 Views Asked by At

I'd like to ask about my program bcs it doesn't work correctly. I want to recall a set of variable in two different Sequence Array. Here is my code.

// Array of Arrays
var SequenceGo:Array =
\[
{dt:dt1, P:P1, s0:s01, s:s1},
{dt:dt2, P:P2, s0:s02, s:s2},
{dt:dt3, P:P3, s0:s03, s:s3},
{dt:dt4, P:P4, s0:s04, s:s4},
{dt:dt5, P:P5, s0:s05, s:s5},
{dt:dt6, P:P6, s0:s06, s:s6},
{dt:dt7, P:P7, s0:s07, s:s7},
{dt:dt8, P:P8, s0:s08, s:s8},
{dt:dt9, P:P9, s0:s09, s:s9},
{dt:dt10, P:P10, s0:s010, s:s10},
\];

var SequenceBack:Array =
\[
{dtback:dt10back, P:P10, s0:s010, sback:s10back},
{dtback:dt9back, P:P9, s0:s09, sback:s9back},
{dtback:dt8back, P:P8, s0:s08, sback:s8back},
{dtback:dt7back, P:P7, s0:s07, sback:s7back},
{dtback:dt6back, P:P6, s0:s06, sback:s6back},
{dtback:dt5back, P:P5, s0:s05, sback:s5back},
{dtback:dt4back, P:P4, s0:s04, sback:s4back},
{dtback:dt3back, P:P3, s0:s03, sback:s3back},
{dtback:dt2back, P:P2, s0:s02, sback:s2back},
{dtback:dt1back, P:P1, s0:s01, sback:s1back}
\];

function onNext(index:int = 0):void
{
    if (index >= SequenceGo.length)
    {
        return;
    }
    
    var aDataGo:Object = SequenceGo[index];
    var aDataBack:Object = SequenceBack[index];
    
    
    //variables
    F = s_teganganst.value;
    m = s_masjenst.value/10000;
    v = Math.sqrt(F/m);
    tp = 5000/v;
    f = s_frekuensist.value;
    w = 2*Math.PI*f;
    aDataGo.dt += t;
    aDataGo.s = aDataGo.s0 - A * Math.sin(w * aDataGo.dt);
    aDataGo.P.y = aDataGo.s;
    if(P10.y < 607){
        aDataBack.dtback += t;
        aDataBack.sback = - A * Math.sin(w * aDataBack.dtBack);     
        aDataBack.P.y = aDataGo.s + aDataBack.sback;
    }
    setTimeout(onNext, tp, index + 1);
}

Actually, code

aDataBack.P.y = aDataGo.s + aDataBack.sback;

is not a fit code for the animation because aDataBack is ordered inversely from aDataGo (we have to stay this inverse order for the proper animation in my program). I want to recall the variables based on its number, so each variable will match with another variable. For example,

P1.y = s1 + s1back;
P2.y = s2 + s2back;
P3.y = s3 + s3back;
P4.y = s4 + s4back;

//and so on

I've tried the code above, but it also doesn't work. Any other expression for calling some couples of variables just like my code above? Thanks!

I want to recall the variables based on its number, so each variable will match with another variable

1

There are 1 best solutions below

2
Organis On

Ok, there are two options.

Option one, simple and straightforward: compose a method to find the correspondent back object on spot:

function findBack(P:Object):Object
{
    for each (var aDataBack:Object in SequenceBack)
    {
        if (aDataBack.P == P)
        {
            return aDataBack;
        }
    }
}

So, that piece of code would be

var aDataGo:Object = SequenceGo[index];
var aDataBack:Object = findBack(aDataGo.P);

The possible problem here is the performance. It is fine on the scale of 10 or 100 objects, but as (I suppose) you devise a particle system, the object count easily scales to thousands, and the amount of loop-searching might become cumbersome.

So I advise to prepare a pre-indexed hash so that you won't need to search each single time.

var SequenceBack:Array =
[
    // ...
];

// Dictionary is a storage of key:value data, just like Object,
// but Dictionary allows Object keys.
var HashBack:Dictionary = new Dictionary;

for each (var aDataBack:Object in SequenceBack)
{
    HashBack[aDataBack.P] = aDataBack;
}

I encourage you to read more about Dictionary class.

And so that piece of code would be

var aDataGo:Object = SequenceGo[index];
var aDataBack:Object = HashBack[aDataGo.P];