James Dawson
James Dawson

Reputation: 5409

Updating Flash text via ActionScript

I want to have a score in the bottom corner of my game. I made a new layer called Score, dragged out a text area, converted it to a symbol and named it Score. Then I set its instance name to Score.

The main class of my flash game is called Main.as. However, I can't seem to access the Score text area I made within the code. I get this error:

1046: Type was not found or was not a compile-time constant: Score.

What did I miss? How can I update the text areas text from within my code?

Edit: Forgot to mention I clicked the "Export for ActionScript" box when I converted it to a symbol. And it's of type MovieClip, if that matters.

Upvotes: 1

Views: 149

Answers (3)

Marty
Marty

Reputation: 39456

Don't export for ActionScript unless you intend to create additional logic for the text field (because it will have its own class) or want to add it to the stage dynamically (via addChild()).

With the instance name being Score, give this a go:

Score.text = "822";

I suggest using lowercase instance names to differentiate from CamelCase used for classes. The above would read to more experienced developers as updating a static property text within a class Score.

Upvotes: 0

Khalizar
Khalizar

Reputation: 307

When you export a symbol for ActionScript 3, Flash make a class for it with the given name (that is the same name you used both for class definition and instance).

  • You get this error because you're using the same name for class and instace.
  • You should check on them and use different names.
  • I usually name my instances ending with _mc

e.g. In your case you should have Score as the class and name your instance score_mc (or just score with lowercase S.

Upvotes: 2

francis
francis

Reputation: 6349

The layer name makes no difference. You need to set the instance name of your dynamic text field, not the clip containing it. There is also no need to convert it to a MovieClip. Create text field on your stage, make sure its not static, give it an instance name that you can use to access it.

Example, I made my text fields name scoreText_txt

//inside main.as
scoreText_txt.text="new text";

If you want to put your text filed inside a MovieClip you would need to give both the clip and your text field a name. You would then access it using .

MyMovieClip.MyText.text="new text"

Upvotes: 0

Related Questions