array trouble in OO code tetris
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: array trouble in OO code tetris

  1. #1
    Junior Member
    Join Date
    Nov 2000
    Posts
    27
    Aargh!
    beginning to question my sanity!! for some reason
    the first horiz line is overlooked!! the peace stops on second line just fine(ok, at this point thought it could be logical) but it stops on the last line too(when last is only)!! i'm making no sense i know...
    i'm making a tetris clone, with a 10x15 area(12x16 with borders) where i have a 2dim array area[col][row] 0=empty, 1=occupied, ok. i have a 3dim array for peaces shape[rotpos][squareno][0=x,1=y].
    to complicate things im trying to make it OO. i made a rotation cw+ccw methods, a refresh, and started coding the vert movement(for this i needed a peace of code to stop my peace if encounters a "1"), but it all seems illogical now
    cause it overlooks "1" in area array for first time ONLY,trace says its "0", after playing around with the area array (0,1,0,0,0,1,0,0,0,1,0,0,0,1)->(0,0,0,0,0,1,0,0,0,1,0,0,0,1)->(0,0,0,0,0,0,0,0,0,1,0,0,0,1)etc. etc. it always stops on the 2ND LINE!! OH WHY OH WHY! give me medication!!!

    here's the code:

    frame 1 (main stage)

    area = new Array();
    for (l=0; l<12; l++) {
    area[l] = new Array(0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1); //1=horiz line
    }
    no = 50;
    for (l=0; l<16; l++) {
    area[0][l]="1";
    area[11][l]="1";
    } //vert lines
    for (l=0; l<16; l++) {
    line = "";
    for (c=0; c<12; c++) {
    line += area[c][l];
    _root.attachMovie("box", ["pohja"+no], no);

    if (area[c][l] == "0") {
    _root["pohja"+no].gotoAndStop(5);
    } else {
    _root["pohja"+no].gotoAndStop(6);
    }
    _root["pohja"+no]._x = 50+20*c;
    _root["pohja"+no]._y = 50+20*l;
    no++;
    }
    trace (line);
    }//just a visual aid
    function dummyCheck () {
    for (r=0; r<15; r++) {
    sum = 0;
    for (c=0; c<10; c++) {
    if (area[c][r] != 0) {
    sum++;
    }
    }
    if (sum == 10) {
    return r;
    }
    }
    return -1;
    }
    scrollDown(5);
    trace (dummyCheck());
    function clearRow (row) {
    for (c=0; c<10; c++) {
    area[c][row] = 0;
    }
    }
    clearRow(dummyCheck());
    trace (dummyCheck());
    function scrollDown (row) {
    for (r=row; r>0; r--) {
    for (c=0; c<10; c++) {
    area[c][r] = area[c][r-1];
    }
    }
    area[0] = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    }
    function pala (muoto) {
    this.shape = new Array();
    for (s=0; s<10; s++) {
    this.shape[s] = new Array();
    for (s1=0; s1<10; s1++) {
    this.shape[s][s1] = new Array();
    }
    }
    if (muoto == "viiva") {
    this.shape[0][0][0] = 2;
    this.shape[0][0][1] = 1;
    this.shape[0][1][0] = 2;
    this.shape[0][1][1] = 2;
    this.shape[0][2][0] = 2;
    this.shape[0][2][1] = 3;
    this.shape[0][3][0] = 2;
    this.shape[0][3][1] = 4;

    this.shape[1][0][0] = 1;
    this.shape[1][0][1] = 2;
    this.shape[1][1][0] = 2;
    this.shape[1][1][1] = 2;
    this.shape[1][2][0] = 3;
    this.shape[1][2][1] = 2;
    this.shape[1][3][0] = 4;
    this.shape[1][3][1] = 2;
    this.lngth = 2;
    }
    if (muoto == "s1") {
    this.shape[0][0][0] = 1;
    this.shape[0][0][1] = 2;
    this.shape[0][1][0] = 2;
    this.shape[0][1][1] = 2;
    this.shape[0][2][0] = 2;
    this.shape[0][2][1] = 1;
    this.shape[0][3][0] = 3;
    this.shape[0][3][1] = 1;

    this.shape[1][0][0] = 1;
    this.shape[1][0][1] = 1;
    this.shape[1][1][0] = 1;
    this.shape[1][1][1] = 2;
    this.shape[1][2][0] = 2;
    this.shape[1][2][1] = 2;
    this.shape[1][3][0] = 2;
    this.shape[1][3][1] = 3;
    this.lngth = 2;
    }
    if (muoto == "s2") {
    this.shape[0][0][0] = 1;
    this.shape[0][0][1] = 1;
    this.shape[0][1][0] = 2;
    this.shape[0][1][1] = 1;
    this.shape[0][2][0] = 2;
    this.shape[0][2][1] = 2;
    this.shape[0][3][0] = 3;
    this.shape[0][3][1] = 2;

    this.shape[1][0][0] = 2;
    this.shape[1][0][1] = 1;
    this.shape[1][1][0] = 2;
    this.shape[1][1][1] = 2;
    this.shape[1][2][0] = 1;
    this.shape[1][2][1] = 2;
    this.shape[1][3][0] = 1;
    this.shape[1][3][1] = 3;
    this.lngth = 2;
    }
    if (muoto == "nelio") {
    this.shape[0][0][0] = 1;
    this.shape[0][0][1] = 1;
    this.shape[0][1][0] = 2;
    this.shape[0][1][1] = 1;
    this.shape[0][2][0] = 1;
    this.shape[0][2][1] = 2;
    this.shape[0][3][0] = 2;
    this.shape[0][3][1] = 2;
    this.lngth = 1;
    }
    if (muoto == "tee") {
    this.shape[0][0][0] = 2;
    this.shape[0][0][1] = 1;
    this.shape[0][1][0] = 1;
    this.shape[0][1][1] = 2;
    this.shape[0][2][0] = 2;
    this.shape[0][2][1] = 2;
    this.shape[0][3][0] = 3;
    this.shape[0][3][1] = 2;

    this.shape[1][0][0] = 3;
    this.shape[1][0][1] = 2;
    this.shape[1][1][0] = 2;
    this.shape[1][1][1] = 1;
    this.shape[1][2][0] = 2;
    this.shape[1][2][1] = 2;
    this.shape[1][3][0] = 2;
    this.shape[1][3][1] = 3;

    this.shape[2][0][0] = 2;
    this.shape[2][0][1] = 3;
    this.shape[2][1][0] = 3;
    this.shape[2][1][1] = 2;
    this.shape[2][2][0] = 2;
    this.shape[2][2][1] = 2;
    this.shape[2][3][0] = 1;
    this.shape[2][3][1] = 2;

    this.shape[3][0][0] = 1;
    this.shape[3][0][1] = 2;
    this.shape[3][1][0] = 2;
    this.shape[3][1][1] = 3;
    this.shape[3][2][0] = 2;
    this.shape[3][2][1] = 2;
    this.shape[3][3][0] = 2;
    this.shape[3][3][1] = 1;
    this.lngth = 4;
    }
    if (muoto == "l1") {
    this.shape[0][0][0] = 1;
    this.shape[0][0][1] = 1;
    this.shape[0][1][0] = 1;
    this.shape[0][1][1] = 2;
    this.shape[0][2][0] = 1;
    this.shape[0][2][1] = 3;
    this.shape[0][3][0] = 2;
    this.shape[0][3][1] = 3;

    this.shape[1][0][0] = 3;
    this.shape[1][0][1] = 1;
    this.shape[1][1][0] = 2;
    this.shape[1][1][1] = 1;
    this.shape[1][2][0] = 1;
    this.shape[1][2][1] = 1;
    this.shape[1][3][0] = 1;
    this.shape[1][3][1] = 2;

    this.shape[2][0][0] = 2;
    this.shape[2][0][1] = 3;
    this.shape[2][1][0] = 2;
    this.shape[2][1][1] = 2;
    this.shape[2][2][0] = 2;
    this.shape[2][2][1] = 1;
    this.shape[2][3][0] = 1;
    this.shape[2][3][1] = 1;

    this.shape[3][0][0] = 1;
    this.shape[3][0][1] = 2;
    this.shape[3][1][0] = 2;
    this.shape[3][1][1] = 2;
    this.shape[3][2][0] = 3;
    this.shape[3][2][1] = 2;
    this.shape[3][3][0] = 3;
    this.shape[3][3][1] = 1;
    this.lngth = 4;
    }
    if (muoto == "l2") {
    this.shape[0][0][0] = 1;
    this.shape[0][0][1] = 3;
    this.shape[0][1][0] = 2;
    this.shape[0][1][1] = 3;
    this.shape[0][2][0] = 2;
    this.shape[0][2][1] = 2;
    this.shape[0][3][0] = 2;
    this.shape[0][3][1] = 1;

    this.shape[1][0][0] = 1;
    this.shape[1][0][1] = 1;
    this.shape[1][1][0] = 1;
    this.shape[1][1][1] = 2;
    this.shape[1][2][0] = 2;
    this.shape[1][2][1] = 2;
    this.shape[1][3][0] = 3;
    this.shape[1][3][1] = 2;

    this.shape[2][0][0] = 2;
    this.shape[2][0][1] = 1;
    this.shape[2][1][0] = 1;
    this.shape[2][1][1] = 1;
    this.shape[2][2][0] = 1;
    this.shape[2][2][1] = 2;
    this.shape[2][3][0] = 1;
    this.shape[2][3][1] = 3;

    this.shape[3][0][0] = 3;
    this.shape[3][0][1] = 2;
    this.shape[3][1][0] = 3;
    this.shape[3][1][1] = 1;
    this.shape[3][2][0] = 2;
    this.shape[3][2][1] = 1;
    this.shape[3][3][0] = 1;
    this.shape[3][3][1] = 1;
    this.lngth = 4;
    }
    this.dir = 0;
    }

    pala.prototype.rotcw = function () {
    if (++this.dir == this.lngth) {
    this.dir = 0;}dir = this.dir;
    for (l=0; l<4; l++) {
    _root["nelio"+l]._x = 50+this.X*20+this.shape[dir][l][0]*20;
    _root["nelio"+l]._y = 50+this.Y*20+this.shape[dir][l][1]*20;
    }
    };

    pala.prototype.rotccw = function () {
    if (--this.dir<0) {
    this.dir = this.lngth-1;
    }
    dir = this.dir;
    for (l=0; l<4; l++) {
    _root["nelio"+l]._x = 50+this.X*20+this.shape[dir][l][0]*20;
    _root["nelio"+l]._y = 50+this.Y*20+this.shape[dir][l][1]*20;
    }
    };

    pala.prototype.refresh = function () {
    dir = this.dir;
    for (l=0; l<4; l++) {
    _root["nelio"+l]._x = 50+this.X*20+this.shape[dir][l][0]*20;
    _root["nelio"+l]._y = 50+this.Y*20+this.shape[dir][l][1]*20;
    }
    };


    pala.prototype.moveok = function (Xmov, Ymov) {
    dir = this.dir;
    ok = true;
    for (l=0; l<4; l++) {
    c=Xmov+this.X+this.shape[dir][l][0];
    r=Ymov+this.Y+this.shape[dir][l][1];
    if (_parent.area[c][r] == "1") ok = false;

    trace(_parent.area[c][r]);

    }
    return ok;
    }; //something wrong with this????

    peace = new pala("tee");//init a peace to screen[viiva,tee,s1,s2,l1,l2,nelio]
    peace.X = 0;
    peace.Y = -1;
    dir = 0;
    for (l=0; l<4; l++) {
    _root.attachMovie("box", ["nelio"+l], l+2000); //box is 6 frame mc, 20x20 square with diff. colors each frame
    _root["nelio"+l].gotoAndStop(l+1);
    _root["nelio"+l]._x = 50+peace.X*20+peace.shape[dir][l][0]*20;
    _root["nelio"+l]._y = 50+peace.Y*20+peace.shape[dir][l][1]*20;
    }
    trace (viiva.lngth);

    and a control mc on stage has following code:

    onClipEvent(enterFrame) {
    if(Key.isDown(Key.UP) ) _root.peace.rotcw();
    if(Key.isDown(Key.DOWN) ) _root.peace.rotccw();
    c++;
    if(c==5){
    c=0;
    //trace(_root.peace.Y);
    if(_root.peace.moveok(0,1)){
    _root.peace.Y++;
    _root.peace.refresh();
    }
    }
    }

    i know ill have to abstract the code more (get rid of direct references[_root] etc. ) im going to do it after i get the damn thing to work!! (movement) the worst thing is, i know flash isnt fool proof! awhile ago i made a cup game, u know under one cup is a pea or something, shuffle them fast, and u have follow the "pea"... a STRANGE bug forced me to use a "dummy" cup at one point!! After adding it (it had no reference, code, only a instace name(witch wasnt referenced) ) and the damn thing worked!! ,but help me out with this one, and ill give it to u as a gift!!! (or if u ask me nicely, lol)

    Phef, in case u missed it, im tired, hope i made SOME sense, lol!

    PS. all u have to do is copy/paste the code; make 1 mc 6 frames 20x20 square, diff. colors each frame, and export it in library;

    S@m

  2. #2
    Senior Member
    Join Date
    Jul 2000
    Location
    Not on the dole any more
    Posts
    1,040
    I'm afraid I don't have time to debug your code for you, but I do have time to offer my sympathies: I developed a version of tetris to fit in a footer, and damn near tore my hair out getting it to work in just 8KB! I succeeded eventually, and here's the result...

    [swf width="300" height="40" background="#000000"]http://www.the-stickman.com/graphics/junk/f5tinytetris.swf[/swf].

    [Left and right arrows to rotate, up and down to move up and down, space to speed up, p to pause]

    Good luck!

  3. #3
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    It is confusing. What does the area array stores ? Is the data a number or string. Sometimes you give it a value of 1,0 and sometimes you assing "0","1" to it. And you have a conditional statement if (area[x][x]=="0"){};

    I am afraid I have no time to debug them in details. Just a glance.

  4. #4
    Junior Member
    Join Date
    Nov 2000
    Posts
    27
    Originally posted by ericlin
    It is confusing. What does the area array stores ? Is the data a number or string. Sometimes you give it a value of 1,0 and sometimes you assing "0","1" to it. And you have a conditional statement if (area[x][x]=="0"){};

    I am afraid I have no time to debug them in details. Just a glance.
    Umm, yes that is inconsistent, i made it in a hurry, but flash autoconverts the variables, yes? (loose typed langueage) so i didn't pay attention, and it does sometimes stop at 1, just not on the first row... maybe ill make em all strings or numbers, just to be sure, ty for this lead!!

  5. #5
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    Maybe your are right. Flash will convert their type.
    Sorry, I forgot this.
    (Oh, I really hate this - loose typing.)

    Anyway, it maybe safer to make it consistant.

  6. #6
    Junior Member
    Join Date
    Nov 2000
    Posts
    27
    like i said, my first OO code prog... im used to make things up as i go, not plannig thing ahead so much. so now i wrote some functions before hand clearrow and scrolldown, things ill need to manage the area once a row is made. well, after testing, me stupid, i forgot to remove the calls to these(used for testing), and the area always got cleard of the first row!! so on my screen it looked like there was something, but actually i cleared it!! basically nothing wrong with my code!! so thanks on your help, for some payment on your efforts, ill tell some of my favourite blonde jokes:

    What does it mean when a blond has a black velvet collar?
    -She is a blowjob blackbelt!

    What's the difference between a blonde and an ironingboard?
    -It's alot harder to spread the legs of an ironingboard!

    Why did god make blondes 2% smarter than cows?
    -So they didn't say "mooooo", when you grab their tits!

    have phun, S@m

Posting Permissions

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




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com