Issue with Document Class and Tutorial - actionscript-3

So I've been following this tutorial on AS3 and Flash. Its been going well until, note I have tried to contact the writer of the tutorial and had no reply. Here's what it tells me to do;
Right-click PlayScreen in the library, select Properties, and check Export for ActionScript. This time, instead of accepting the default value, enter the name of your document class. Click OK.
So it pops up an error, first we’ll have to make a new document class, since no two different objects can share the same class. Cancel the Properties box.
Hit File > New and select ActionScript File. Enter the (by now familiar) code.
Save this in the Classes directory as DocumentClass.as. (There’ll be no confusing the purpose of this file!) Now, back in your FLA, change the document class to DocumentClass.
Check everything’s fine by clicking that pencil icon — if it’s all OK, that should bring up the AS file that you just created.
// So this bits all fine, its the next that i'm stuck with:
Now you can set the PlayScreen‘s class to AvoiderGame. So do so!
// So I go ahead into the properties and change the name but then it pops up with the same error as before: 'Please enter a unique class name that is not associated with other library symbols'
What does this mean!? How do I resolve this!?
Full tutorial here:Flash Tutorial

Its hard to tell what you are trying to accomplish without knowing what all the parts you are referring to actually do, which are objects in the library and which are classes, but maybe this can help:
First of all, document class in AS3 typically refers to the project's main set of code that initializes the app. This class can be called anything but is often called Main, and is entered in the property panel that is displayed when you click the projects main stage in the field called class.
Now, when linking a class to an object in the library, its a little different. In the library object's property panel, tick the box for Export for Actionscript, and put a unique name in the top box. This is what you reference in your code to call it, like new somethingOrOther() or using the pic below as an exaample, new Ball(). The second box is the base class, pathed to where it lives in your code base. This is the class you will write that actually controls the object you've linked the class to. Giving a linked object a base class and a unique identifier allows you to use the same base class for multiple linked objects.
Note that when you do this approach, Flash will warn you that there is no class associated with Ball and one will be created for you. Don't worry, this is normal behavior. If you set this up properly, your object will still be controlled by its base class.

Related

Edit complex component in Flash IDE in one ActionScript class

I'm new to Adobe Flash. I'd like to create a button or component in Flash Professional using MovieClip and add label to it, and build my own button with special functions.
And i'd like use ActionScript to edit code. I faced a problem: i can not edit all code of my component (that cotains several ui-elements) in one AS3 class. I create empty document, add rectangle to it, then convert it to symbol. Next, i go to Advanced ActionScript 3.0 Settings of IDE, add path to folder where i put my Class.
Then add MyClass to Document class in that window.
But when i add new elements to the document, it doesn't appear in MyClass.as
Is it possible to edit complex component in Flash IDE in one ActionScript class ?
Sorry for a long explanation and for my poor english.
I create empty document, add rectangle to it, then convert it to
symbol.
Make sure symbol is type: MovieClip. Give it a name you prefer (no space is good idea). Further down (see Advanced section?) there is section called Linkage click "Export for ActionScript" and remember the name shown in Class box (you can edit it) and just click OK. No need for folder paths etc.
Lets assume the name used for Class is : myComponent
Now you can use it in your code. To make a document class open the Properties panel (ctrl+F3) and just type a name example Main (no extensions) and press enter and then save FLA and secondly also save the class code (it will ask for a file name so use as Main.as).
Now If you click that pencil icon in properties you will be editing class code (Main.as) for the FLA document and it knows your component if you reference it by code:
public var test_comp : myComponent = new myComponent (); here test_comp is just a quick reference name. Use your own preferred reference name.
You can edit the MovieClip of myComponent by right-clicking and choosing "edit" (not "edit Class"). Now you can add/draw different things and even convert them to Graphic or MovieClip and give them instance names. A MovieClip object is like a mini-stage so it has its own unique timeline and you can work exactly like if this was the main stage (but without actually affecting main stage).
If you add something to Component MC such as drawing a circle (as some button?) and convert that circle to MC or Graphic type with instance name circle1, in the code you access that as:
test_comp.circle1.alpha = 0.5; since test_comp is your code's reference name for that myComponent MC in the Library.
After testing the above example... now begin your own component.
Remember... If an object is inside the MC access it via it's instance name and if you need to addChild it from the Library instead then use its AS3 linkage name.

Actionscript 3 - Accessing a variable on the main timeline

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?
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
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
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.
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.
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!
Accessing variables or movieclips at main timeline is as following:
AS2:
_root.variable_name;
AS3:
MovieClip(root).variable_name;

AS3 Avoider game tutorial document class

everyone! I'm trying to find an answer to a problem that I've found with the AS3 Avoider Game Tutorial by Michael James Williams, found here.
More specifically, I'm stuck on the part where you have to set the "PlayScreen" to a differen Document Class. I right-click on the symbol in the library, select "Properties," and get a pop-up screen that sets up Class, Base Class, linkage...but nothing in there about "Document Class." It doesn't look like the screenshot.
So what do I do, here?? How do I find the document class?
It looks like you should have declared AvoiderGame.as as your document class, originally. That's the instruction in the first section of the tutorial under 'Making an Enemy.'
This instruction also tells you how to declare a document class.
Now, in section 3, you're instructed to create a new class that will become the document class instead of AvoiderGame.as. It just happens that you're asked to name this class 'DocumentClass.as' But you MAKE it your document class (which, of, course, is the point-of-entry into the game) in the same way as before: by putting it's name -- DocumentClass -- into the document class box in the Properties panel.

AS3 interclass variables

I have a button, that's a class, say, Button. And a main document(class Main) where I can see all the changes in a text field, refreshed every frame (yeah, I don't like trace()). The button has a function of changing a variable that's used in main file. So when I press the button, that I add in the main file to the stage, I need it's function to work. Button class has this.addEventListener(MouseEvent.CLICK,function), further function adds to the var from main class. So if it did add, I would see the changes in the text field. I see changes made by an object I add directly, in the graphic editor, but no changes from the button, whose child I add. Variables are public. Functions are all public. So...
Classes cannot exchange variables freely?
How do I change a variable via a function in a different class?
Maybe I need to export and import var? I thought they all work as a united program that shares all public variables.
Main: I create var i, add Button child to stage and show i every frame =D
Button: When you press me, I add 1 to i =D
SherWood: Main, Y U don't show i, changed by Button? T_T
Code example:
http://piratepad.net/g5tTMFX4Bo
The Button (or any other class) has no built-in knowledge of the environment that is using it's instance.
The DisplayObject ancestors have the ability to check the parent container, and "main" container via parent and stage properties.
The programmers role (you) is to code flexible way to exchange information that any 2 (or more) classes needs to have, e.g. by exposing public methods/properties, dispatching events (that other party can listen) etc.
So, if the Main needs to know that Button (instance of Button) was clicked add listener to that button for click event, and then you will be able to do whatever you want e.g. add one to i
If Button then, for instance needs to present up-to-date value Main via exposed method can do that e.g. myButton.setLabel("Value of i="+i); which may be executed in aforementioned click listener,
regards

When do we need to add reference to the stage from one class to another

When do we need to pass reference to the stage from one as3 class to another like in this tutorial
http://asgamer.com/2009/as3-flash-games-for-beginners-firing-weapons-with-delays
he added a ref to the stage from the bullet class to the ship class
as I understand a reference is needed when we want to use a function in a certain class from another class but why do we have to reference the stage isn't it only one stage for the whole project or each class has it's own stage ?
I am very confused
Thanks
Only objects that are connected to a stage will be shown on screen. In the tutorial he is adding the lasers onto the stage display list so that it appears on screen. Until it is added, it will not be rendered regardless of the visible property.
See this for more info about the display list.
#Geotarget is correct, but the answer is a little bit indirect.
Objects that are not on the display list do not actually have a reference to stage. So if, for example, you create a var mc:MovieClip = new MovieClip(); which is not added to the display list (as in addChild(mc)), then mc.stage will be null. (Also, checking if(mc.stage){[...]} is also a way to verify if the MovieClip is part of the display list yet.)
So you can pass a reference to the stage to non-display-list objects to allow them access to things like stageWidth.
Yes, a reference is needed when you wanna have to access a function present in that particular class ( to which reference belongs). This is one of the uses.
There is only one stage for the whole project.
In the tutorial you are following, both the classes are using the reference of a COMMON Stage, so that both of them can access the Stage.
It's like giving the address of a place to two people. So that both of them can go there. Naturally, giving addresses doesnot mean, we are building two places for each of them.
V.