Rizal Nurul Huda Rizal Nurul Huda - 6 months ago 23
JSON Question

send json data to array(movieclip) as3.0

I am building an air app in flash cs6 using as3. I need to send a json to movieclip. I wanted to create a "timeline" on my application.
This is the code I use.

function onCompleteLoadTimeline(event:Event){
var result:Object = JSON.parse(event.target.data);
var yPos = 0;
for (var h:int=0; h<=1; h++){
tpost = new t_post();
tpost.x = 0;
tpost.y = 0;
timeline_mc.addChild(tpost);
timeline_container.push(tpost);
timeline_container[h].y = yPos;
yPos += timeline_container[h].batas.y;

}
for (var i:Object in result){
for (var j:int=0; j<=1; j++){
timeline_container[j].nama.text = result[i].timeline_name;
timeline_container[j].postingan.text = result[i].timeline_post;
}
trace ("nama : "+result[i].timeline_name);
trace ("status : "+result[i].timeline_post);
trace ("waktu : "+result[i].date);
trace ("suka : "+result[i].likers);
}
}


for the code only the latest data appear in the movie clip.
please help me.

Answer

You have a few for loops that go up to 1. I don't know what they are for.

The only loop that you need is the one iterating over the result.

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

for (var i:Object in result){
    var tpost:t_post = new t_post();
    tpost.y = yPos;
    timeline_mc.addChild(tpost);
    tpost.nama.text = result[i].timeline_name;      
    tpost.postingan.text = result[i].timeline_post;
    yPos += tpost.height + 10;
}

I have no idea what batas means (use English in your code!) which is why I created the logic to position the objects myself. If yours worked for you, just use that.

I also omitted the array code for brevity.

Btw: You are essentially building a custom component with a custom way to display data which is often referred to as an "item renderer". Go ahead and search for that term if you don't want to build everything from scratch.

Comments