Skip to main content
Inspiring
April 26, 2012
Answered

Very basic variable question

  • April 26, 2012
  • 1 reply
  • 1316 views

I can get my way around Javascript and PHP, but simple things in AS3 stump me.

The following code works fine:

slideIn();

                              function slideIn():void

                              {

                        var myTween:Tween = new Tween(_root.content_txt1,"y",Strong.easeOut,-150,5,3,true);

                              }

I want to reuse the function for different items by inserting an argument (num) in the function. The trace function shows me that the variable has the correct value. However, the Tween doesn't parse the variable "item". It looks for an item named "item", resulting in an error.

slideIn(1);

                              function slideIn(num):void

                              {

                                        var item = "_root.content_txt" + num;

                                        trace(item);

                                        var myTween:Tween = new Tween(item,"y",Strong.easeOut,-150,5,3,true);

                              }

Thanks.

This topic has been closed for replies.
Correct answer Rothrock

To reitterate the question without the misleading "_root" variable:

The first code works fine. The second code generates Error #1056: Cannot create property y on String.

var eric:MovieClip = MovieClip(this.root);

               xml = new XML(e.target.data);

               eric.content_txt1.htmlText = getContent(1);

          slideIn();

               function slideIn():void

               {

var myTween:Tween = new Tween(eric.content_txt1,"y",Strong.easeOut,-150,5,3,true);

               }

var eric:MovieClip = MovieClip(this.root);

xml = new XML(e.target.data);

eric.content_txt1.htmlText = getContent(1);

slideIn(1);

function slideIn(num):void

{

var item = "eric.content_txt" + num;

//trace(item);

var myTween:Tween = new Tween(item,"y",Strong.easeOut,-150,5,3,true);

}


Right, because item is a string and strings don't have a y property. Just because you didn't type the variable item doesn't mean that Flash doesn't know it is a string.

You need to use array notation to get the object inside of the eric movieclip.

var item:TextField=eric["content_txt"+num];

You might also have to actually cast it like this:

var item:TextField=TextField(eric["content_txt"+num]);

Or if you plan to use it on various different display objects:

var item:DisplayObject=DisplayObject(eric["content_txt"+num]);

1 reply

sinious
Legend
April 26, 2012

You're no longer in AS2 land, there is no _root.

Try this:

slideIn(1);

function slideIn(num):void

{

          var item:MovieClip = MovieClip(getChildByName("content_txt" + num));

          trace(item);

          var myTween:Tween = new Tween(item,"y",Strong.easeOut,-150,5,3,true);

}

Inspiring
April 26, 2012

I'm getting the trace  result as null and the error "TypeError: Error #1009: Cannot access a property or method of a null object reference."

(note that the object I'm animating is a text object, not a movieclip.

sinious
Legend
April 26, 2012

Ah, ok:

slideIn(1);

function slideIn(num):void

{

          var item:TextField = TextField(getChildByName("content_txt" + num));

          trace(item);

          var myTween:Tween = new Tween(item,"y",Strong.easeOut,-150,5,3,true);

}

Do note that this frame script expects content_txt1 to be on the timeline at the same scope. Otherwise getChildByName() will not find content_txt1 and you will get a null reference.