1

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
    Please consider refactoring your scripts. As of now, they contain huge amounts of variables with pretty much identical names made up of a non-mnemonic mixes of **s** and **t** and **b** and **d** which makes it **REALLY** difficult to follow the code logic. – Organis Jan 19 '23 at 12:26
  • Ups, my bad.... – Fatah Kurniawan Jan 19 '23 at 14:03
  • 1
    Ok now i've rewritten the variables more simple than before. Hope you can follow the code logic – Fatah Kurniawan Jan 19 '23 at 14:05
  • Much more readable, I commend your effort. So, to the problem. Am I right that you want to initialize the **aDataBack** variable so that **aDataBack.P == aDataGo.P**? Probably, **var aDataBack:Object = SequenceBack[SequenceBack.length - 1 - index];** ? – Organis Jan 19 '23 at 17:03
  • For the first question, yes. This P in these two variables (aDataBack and aDataGo) is the same object. But, i don't think so if we have to use the code in the second question. It's bcs we have to keep the program read the array from top to the bottom for the proper animation. Actually, it's still about wave animation that we've discussed before. I want to improve with a reflecting wave. So, they have to walk from P10 to P1, right? – Fatah Kurniawan Jan 20 '23 at 01:41

1 Answers1

1

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];
Organis
  • 7,243
  • 2
  • 12
  • 14
  • Okay, I can follow the code logic for this part and I've already tried the code. Unfortunately, there is an error in each option. Error that appear in the 1st option is "1170: function does not return a value". On the other hand, error that appear in the 2nd option is "1080: Syntax Error: expecting in before colon" (for the `for each (aDataBack:Object in SequenceBack)` lines) – Fatah Kurniawan Jan 20 '23 at 10:46
  • But, it's okay. Let's focus on my last question. Is there any way that can recall some variables in an array individually? Like I said `P1.y = s1 + s1back; P2.y = s2 + s2back; P3.y = s3 + s3back; P4.y = s4 + s4back;`. We shouldn't use `aDataBack.P.y = aDataGo.s + aDataBack.sback;` because aDataGo.s and aDataBack.sback are ordered inversely. If we use `aDataBack.P.y = aDataGo.s + aDataBack.sback;`, the result will be `P1.y = s1 + s10back; P2.y = s2 + s9back; P3.y = s3 + s8back; P4.y = s4 + s7back;`. So, it's not a proper code, perhaps. – Fatah Kurniawan Jan 20 '23 at 10:55