jsonactionscript-3loopsaddchild

addChild with looping and different height properties


I'm building an application with flash as3.0. This my code.

function onCompleteLoadTimeline(event:Event){
    var result:Object = JSON.parse(event.target.data);
    var yPos = 0;

    for (var i:int=0;i<=2;i++){
        tpostArr[i] = new t_post();
        var batas_mc:batas = new batas();

        tpostArr[i].foto.x = 52.50;
        tpostArr[i].foto.y = 52.50;

        tpostArr[i].nama.x = 120;
        tpostArr[i].nama.y = 20;

        tpostArr[i].postingan.x = 120;
        tpostArr[i].postingan.y = 55.15;
        tpostArr[i].postingan.wordWrap = true;
        tpostArr[i].postingan.autoSize = TextFieldAutoSize.LEFT;

        tpostArr[i].tombol_suka.x = 440;
        tpostArr[i].tombol_suka.y = 73;
        tpostArr[i].nama.text = result[i].timeline_name;      
        tpostArr[i].postingan.text = result[i].timeline_post;
        batas_mc.x = 0;
        batas_mc.y = tpostArr[i].postingan.y + tpostArr[i].postingan.height + 20;
        tpostArr[i].addChild(batas_mc);

        timeline_mc.addChild(tpostArr[i]);
        tpostArr[i].y = i* tpostArr[i].height;
    }
}

about the y position of the tpost. height of the tpost is not always the same. tpost can be higher depending on the tpostArr[i].postingan.height and I want tpost not overlap. Can it be solved by enterFrame? if can can you show the code for me?


Solution

  • I would use a variable to hold the greatest value that is not overlapping. In other words, if you add an instance of a display object at x = 20, and the display object is 10 pixels wide, the variable would be set to 30 like this:

     yourVar = yourMovieClip.x + yourMovieClip.width
    

    Then, when you add the next one, set its lower limit to yourVar.

    This concept should work and be pretty easy to apply to your situation.

    I think in your case, you are trying to instantiate your objects with a certain amount of vertical spacing, right? If so, just make

    yourVar = tpostArr[i].y + tpostArray[i].height;
    

    and set the next instance's y value to

    yourVar + 5; // or whatever number of pixels you want. 
    

    This way, the height of each instance can be any amount, and you have a variable stored that will be the next y value to post a tpost at.

    I hope I've understood your problem well enough. Let me know if this works.