Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Variable text field in root cannot reside in a movie clip?

Engaged ,
Oct 30, 2013 Oct 30, 2013

I have a movie clip ("mod_control") that resides within other clips that determines the name of its parent and adds a number to a corresponding text field when they appear on stage (example: "Widget_1_count", "Widget_2_count", etc...)

Using this code in the clip works fine:

// get the parent name, example: "Widget_1"

var this_part = Object(this).parent.name;

// get the current value in the corresponding text field, example: "Widget_1_count"

var c_this_part:Number = Number(Object(root[this_part + "_count"]).text);

// add 1 to the current text field value

var v_this_part:Number = Number(c_this_part+1);

// update the text field with the new value

Object(root[this_part + "_count"]).text = String(v_this_part)

I will eventually have several dozen text fields (for several dozen parts) so I now contain them all in a movieclip with instance name "part_count".

 

So I added part_count into the target path:

var this_part = Object(this).parent.name;

var c_this_part:Number = Number(Object(root.part_count[this_part + "_count"]).text);

var v_this_part:Number = Number(c_this_part+1);

Object(root.part_count[this_part + "_count"]).text = String(v_this_part)

...but that produced this error:

 

Symbol 'mod_control', Layer 'as', Frame 1, Line 201119: Access of possibly undefined property part_count through a reference with static type flash.display:DisplayObject.

and then I tried this:

var this_part = Object(this).parent.name;

var c_this_part:Number = Number(Object(root[part_count.this_part + "_count"]).text);

var v_this_part:Number = Number(c_this_part+1);

Object(root[part_count.this_part + "_count"]).text = String(v_this_part)

...but that produced this error:

Symbol 'mod_control', Layer 'as', Frame 1, Line 201120: Access of undefined property part_count.

As is often the case, I may have overlooked an obvious nuance of syntax... could that be the situation here, or is this action simply impossible in flash?

I appreciate the wealth of knowledge and experience here and thank you in advance for giving my issue some thought.

TOPICS
ActionScript
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Nov 07, 2013 Nov 07, 2013

Thanks for the info, I used your suggestion:

var this_part = Object(this).parent.name;

var c_this_part:Number = Number(MovieClip(MovieClip(root).part_count[this_part + "_count"]).text);

var v_this_part:Number = Number(c_this_part+1);

MovieClip(MovieClip(root).part_count[this_part + "_count"]).text = String(v_this_part)

And the movie compiled fine, but when I added an object to the stage (which fires the above code) I got this error:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.te

...
Translate
Community Expert ,
Oct 30, 2013 Oct 30, 2013

cast part_count as a MovieClip:

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 01, 2013 Nov 01, 2013

When you say "cast" my movieclip as a movieclip, I'm afraid I do not know what that means. I have never heard of that before, is there an example of this somewhere?

Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2013 Nov 01, 2013

this is casting root as an object

Object(root)

but from the looks of your code, it's a movieclip and you should use:

MovieClip(root).part_count["tf_"+1].text=whatever;

i'm not sure you need to do anything other than that.  but if you need to cast part_count too, use:

MovieClip(MovieClip(root).part_count)["tf_"+1].text=whatever;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 07, 2013 Nov 07, 2013

Thanks for the info, I used your suggestion:

var this_part = Object(this).parent.name;

var c_this_part:Number = Number(MovieClip(MovieClip(root).part_count[this_part + "_count"]).text);

var v_this_part:Number = Number(c_this_part+1);

MovieClip(MovieClip(root).part_count[this_part + "_count"]).text = String(v_this_part)

And the movie compiled fine, but when I added an object to the stage (which fires the above code) I got this error:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.text::TextField@79cb351 to flash.display.MovieClip.

          at IF_Config_38_fla::mod_control_49/frame1()[IF_Config_38_fla.mod_control_49::frame1:27]

What actually did the trick, was casting the clips as Objects:

var this_part = Object(this).parent.name;

var c_this_part:Number = Number(Object(Object(root).part_count[this_part + "_count"]).text);

var v_this_part:Number = Number(c_this_part+1);

Object(Object(root).part_count[this_part + "_count"]).text = String(v_this_part)

Worked a treat!

Staggeringly confusing, I wish I knew why it worked, but it does.

Thanks again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2013 Nov 07, 2013
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines