0
votes

I'm trying to make a file that is easy for a non-flash user to use/reuse to easily display content. The key here is that this file is to be a template for a novice user to just copy/paste some very minimal code to create different "flash card" type swf files.

The file I am creating has multiple buttons on the main timeline which when clicked, attaches a movie clip which will display a dynamic text area with content specific for the button that was clicked. The content for the text area will be loaded from a separate text file.

For the sake of this example, I'm just going to refer to one button...

So, on the main timeline, in frame 1, I have a variable definition:

var myFilename1:String = "mySampleFile2.txt";

When the button on the main timeline, in frame 1 is pressed, a movie clip is loaded which contains a text area. The content for the text area is located in that file: mySampleFile2.txt.

If I hard-code the file name, it works like a dream:

myTextLoader.load(new URLRequest("mySampleFile2.txt"));

But I don't want to hard code the file name. I want to refer the variable in the main timeline. In AS 2, it would have been

myTextLoader.load(new URLRequest(_root.myFilename1));

In AS3 I thought it would be either:

myTextLoader.load(new URLRequest(root.myFilename1));

OR

myTextLoader.load(new URLRequest(MovieClip(this.parent.root).myFilename1));

When I run the code I get the following error and when I run a trace I get the file name is NULL.

TypeError: Error #2007: Parameter url must be non-null.

How to I access the file name stored in the variable on the main timeline?

*************************** UPDATE! *************************

So I just discovered that the issue is related to a button on the screen. The button is one from the buttons library. If I remove the button, everything works great. But as soon as that button is on the main timeline, it makes it to I cannot access the variables using MovieClip(root).variable_name;. Unfortunately I want that button to trigger the events within the MovieClip. Any thoughts?

3
How do you "load a movie clip"? Using Loader or instantiated from the library?Aaron Beall
I am actually changing it now so that the movie clip is actually present on the main timeline (the person who'll be using the template has requested this)... so it will actually be on the main timeline, but the code inside the Movie Clip still needs to access the variable stored in the main timeline.tbar

3 Answers

1
votes

Not a good idea

You are not able to achieve your goals with this bad practice approach.

to use/reuse

The code you want to provide is not very reusable. It heavily relies on one variable existing in a certain place. Therefore it cannot be use twice in a project.

But I don't want to hard code the file name.

And now you are hard coding the variable and its location. If you considered hard coding the file name to be a bad things, consider this a bad thing, too.

What the problem is

Basically speaking, the problem is that your component is reaching out to grab this variable from somewhere within your project. But it is not the concern of the component to find the content it should display. It is not well encapsulated.

Learning from existing things

You want to display text. Let's take a look at the TextField class to see how it displays text.

var tf:TextField = new TextField();
tf.text = "hello";
addChild(tf);

As you can see, the text that it should display is passed to the TextField object. There is not some arbitrary variable one has to set in order to modify the text, as you are planning to do:

var tf:TextField = new TextField();
var someArbitraryVariableThatModifiesATextField = "hello";
addChild(tf);

There is no obvious connection to the TextField object and if there's a second TextField, this doesn't work at all.

Applying that to the problem at hand

Just like the TextField, your "flash card" should receive the file as a parameter. Either pass it to the constructor as seen in the example below, or create a method that takes it as a parameter.

var card:Card = new Card("mySampleFile2.txt");
addChild(card);

Additional thoughts

  1. Create additional methods to set the values individually. There's nothing worse than some code that does exactly what one wants, but only operates on files and one doesn't have a file. The goal is again, to make it easy to reuse the code
  2. Use the [Inspectable] meta tag to allow the user of your code to modify the properties at author time. This can be used to the extend of modifying properties belonging to a physics engine, now this is what I would call easy to use for a novice user.
  3. Instead of writing code and thus requiring to recompile the file again, take the information (either the path to the file or it content) from the flashars that are passed to the .swf file when it is embedded into an html page. This makes the single .swf file truely reusable and easiest to use, because there's no As3 coding required whatsoever.
0
votes

I haven't tested this but I believe the "root"-stage would be:

this.parent

In the child swf.

check out this question: as3 access variable from parent swf

Good luck!

0
votes

Accessing variables or movieclips at main timeline is as following:

AS2:

_root.variable_name;

AS3:

MovieClip(root).variable_name;