Monday, March 22, 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