Friday, March 26, 2010

Create tweens within a loop

I'm pulling data from xml and using a for loop to have each line of text appear on screen. I want to have each of these ease in. I don't know the number or size in advance, so each textfield is created dynamically.

What I've tried to do is dynamically create a tween for each dynamically created textfield, but I just can't seem to get the syntax right. This is a snippet from what I've got at the moment, inside of the function that's called once the xml is successfully loaded:

?var bl:XMLList = fsXML.content.point;
?var points:Array = new Array();
?var Btween:Array = new Array();
?for (var n:int; n%26lt;bl.length(); n++) {
?var p:TextField = new TextField();
?p.name = ''points'' + points.length;
?points.push(p);
?var b:Tween;
?b.name = ''Btween'' + Btween.length;
?Btween.push(b);
?}

This doesn't work, as it can't assign 'name' for the tween.

I get this error: 1119: Access of possibly undefined property name through a reference with static type fl.transitions:Tween. for b.name = ''Btween'' + Btween.length;

My intent was to use an array to create the tweens, as I do the textfields, in this for loop:

?for (var i:int; i%26lt;points.length; i++) {
?points[i].multiline = true;
?points[i].autoSize = ''left'';
?points[i].wordWrap = true;
?points[i].x=10;
?points[i].y=lh;

?points[i].width=240;
?points[i].embedFonts = true;
?points[i].text = bl.text()[i];

?points[i].antiAliasType = ''advanced'';
?points[i].setTextFormat(textFormat);
?lh = lh + points[i].textHeight + fs;
?addChild(points[i]);
?points[i].alpha = 0;
?Btween[i]:Tween = new Tween(points[i], ''alpha'', None.easeIn, 0, 1, 9, true);
?Btween[i].addEventListener(TweenEvent.MOTION_FINISH, motionFinishHandler);
?}

But I get this error: 1078: Label must be a simple identifier. for Btween[i]:Tween = new Tween(points[i], ''alpha'', None.easeIn, 0, 1, 9, true);

Any suggestions on how this could be accomplished would be greatly appreciated.

Create tweens within a loop

Ok first you do this:

?var bl:XMLList = fsXML.content.point;
?var points:Array = new Array();
?var Btween:Array = new Array();
?for (var n:int; n%26lt;bl.length(); n++) {
?var p:TextField = new TextField();
?p.name = ''points'' + points.length;
?points.push(p);
?}

then you do this:

?for (var i:int; i%26lt;points.length; i++) {
?points[i].multiline = true;
?points[i].autoSize = ''left'';
?points[i].wordWrap = true;
?points[i].x=10;
?points[i].y=lh;

?points[i].width=240;
?points[i].embedFonts = true;
?points[i].text = bl.text()[i];

?points[i].antiAliasType = ''advanced'';
?points[i].setTextFormat(textFormat);
?lh = lh + points[i].textHeight + fs;
?addChild(points[i]);
?points[i].alpha = 0;
?var B:Tween = new Tween(points[i], ''alpha'', None.easeIn, 0, 1, 9, true);
?B.addEventListener(TweenEvent.MOTION_FINISH, motionFinishHandler);

?Btween.push(B);//still adding to array to avoid GC. This can be emptied later
?}

Create tweens within a loop

Thanks for the quick reply! I believe I see where you're going, but the tweens added to the array don't show up on the stage. What would I need to add to get them to appear?

I've resolved this by taking a different approach. I'm using the Tweener class, which allows each object to be created.

  • makeup
  • No comments:

    Post a Comment