A Flash Developer Resource Site

# Thread: [RESOLVED] Flash maths problem

1. ## [RESOLVED] Flash maths problem

I have been trying to make a script to calculate this but keep getting lost in the code. I have a grid 15 by 15 going from 0 - 14, then the next row 15 - 29 and so on till 225. It's so i can set up a depth system on the grid where by 14 is the very back and 210 is the very front. I ended up writing the code manually but would prefer the script to calculate this for me. Here is the code i wrote can someone make a script to do this please (or give me some help).

setChildIndex(MovieData[14],0);

setChildIndex(MovieData[13],1);
setChildIndex(MovieData[29],2);
setChildIndex(MovieData[28],3);

setChildIndex(MovieData[12],4);
setChildIndex(MovieData[27],5);
setChildIndex(MovieData[44],6);
setChildIndex(MovieData[43],7);
setChildIndex(MovieData[42],8);

setChildIndex(MovieData[11],9);
setChildIndex(MovieData[26],10);
setChildIndex(MovieData[41],11);
setChildIndex(MovieData[59],12);
setChildIndex(MovieData[58],13);
setChildIndex(MovieData[57],14);
setChildIndex(MovieData[56],15);

setChildIndex(MovieData[10],16);
setChildIndex(MovieData[25],17);
setChildIndex(MovieData[40],18);
setChildIndex(MovieData[55],19);
setChildIndex(MovieData[74],20);
setChildIndex(MovieData[73],21);
setChildIndex(MovieData[72],22);
setChildIndex(MovieData[71],23);
setChildIndex(MovieData[70],24);

and so on till the top number (14,13,12,11,10) equals -1

Thank you to anyone that can help

2. If you can do the grid so the back corner is like this:

6,3,1, 0
11,7,4,2
17,12,8,5
24,18,13,9 and so on

instead of the way you have it:

9,4,1,0
10,5,3,2
11,8,7,6
15,14,13,12 and so on

(diagonal rather than down then right to left)

I can create a formula for that. It would take about 15 lines of code to do what took you 225 lines of code. But before I do it, I would like to know if it will work for you that way. Please let me know and I will give you the formula.

3. Ok, I hope you can use it the way I suggested. If you can, here is the code (only 33 lines)
Actionscript Code:
`var startPos:uint;var startVal:uint;var pos:uint;function firstHalf():void {    startPos=210;    startVal=225;    for (var a:uint = 0; a<15; a++) {        pos=startPos+a;        for (var b:uint = 0; b<=a; b++) {            startVal--;            //setChildIndex(MovieData[pos],startVal);            trace("MovieData["+pos+"],"+startVal);            pos-=16;        }    }}function secondHalf():void{    startPos = 209;    startVal = 105;    pos = startPos;    for (var c:uint = 14; c>0; c--){        pos = startPos;        for(var d:uint = 0; d<c; d++){            startVal--;            //setChildIndex(MovieData[pos],startVal);            trace("MovieData["+pos+"],"+startVal);            pos-=16;        }        startPos-=15;    }}firstHalf();secondHalf();`
:

I commented out the "setChildIndex" lines because as is, those will trigger errors. Let me know if this will work for you.

4. Wow thank you dukeufan4 i will try this code as soon as i can and get back to you. Thank you very much for your help

5. Hey tried your code and i can't get it to work as needed, i think the problem is that the depths are in the wrong order as the display list writes from 0 up and can't be wrote at 224 first. Thank you for your help and i will see if i can change your code to get the right result. Once again thank you

6. ## Got it working

Hey i just spent the last few hours trying to solve this problem, spent a small time altering your code but couldn't get the right result but got close though, just a few rows were not working. I started writing the code using yours as a base and then slowly got the code to work correctly. Problem Solved, Thank you for your help

The code below allows you to arrange an array displayed row by row in an isometric grid's depths. I have posted the code incase anyone would like to use it, this was built for a 15 by 15 grid in AS3. Please credit me and dukeufan4 if you copy the code. Thank you.

Actionscript Code:
`function ArrangeDepths():void{    var a:int = 14; // Starting Point    var b:int = 0; // Amount of times to run    var c:int = 0; // Equals B    var d:int = 0; // a + 15    var e:int = 0; // Track d + 15    var f:int = 0; // Equals E    var g:int = 0; // Equals D    var h:int = 0; // Equals D - E    var i:int = 0; // Equals E    var Dep:int = 0; // The depth of the movie, increment by 1    // The vars that equal other vars allow the var to be altered as the original is a place holder        while(a >= 0){        //trace("\n" + a);        setChildIndex(MovieData[a],Dep);        Dep++;        c = b;        d = a;        f = e;        g = e;        h = e;        while(c > 0){            while(f > 1){                d += 15;                //trace(d);                setChildIndex(MovieData[d],Dep);                Dep++;                f--;            }            c--;        }        if(a != 14){            g = d;            g += 15;            h = g + e;            i = e;            while(i >= 0){                //trace(h);                setChildIndex(MovieData[h],Dep);                Dep++;                h--;                i--;            }        }        b++;        b++;        e++;        a--;    }}`

7. Well, I got your first response this morning and modified my code for you. If you want a little shorter version, here it is. If you don't use it, doesn't really matter to me. I love working on the math problems in Flash. Good luck with your project.
Actionscript Code:
`var myArray:Array = new Array();var startPos:uint;var startVal:uint;var pos:uint;function firstHalf():void {    startPos=210;    startVal=225;    for (var a:uint = 0; a<15; a++) {        pos=startPos+a;        for (var b:uint = 0; b<=a; b++) {            startVal--;             myArray[startVal] = pos;            pos-=16;        }    }}function secondHalf():void{    startPos = 209;    startVal = 105;    pos = startPos;    for (var c:uint = 14; c>0; c--){        pos = startPos;        for(var d:uint = 0; d<c; d++){            startVal--;            myArray[startVal] = pos;            pos-=16;        }        startPos-=15;    }}function assign():void{    for(var e:uint = 0; e<myArray.length; e++){        //setChildIndex(MovieData[myArray[e]],e);        trace("MovieData["+myArray[e]+"],"+e);    }}firstHalf();secondHalf();assign();`

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap