ActionScript 3.0 :: Class Has Be Instantiated In Document Class?
Mar 18, 2009
I am making my way with AS3 in little steps and have hit a road block. This is what I have:
I have a document class called "Document Class" I have a custom class called "Game"
I have instantiated an object of "Game" class and I am able to trace a class method which return a simple "HELLO". within my Game class, I have a variable(type Array) called "questions" as instance variable.
I would like to add questions to "questions" array by using "Mutator" method, or count the current elements with the questions array and return the total number of questions. I am unable to add or access elements to the questions array.
I have question regarding removing instantiated objects. Let's say i have a main class in which i instantiate a custom video player (or whatever) class:
Code: ... var videoPlayer = new VideoPlayer(...); addChild(videoPlayer); ...
And inside VideoPlayer class i have of course dozens of objects (movie clips, sprites representing different parts of VideoPlayer) and dozens of event listeners attached to them. My question is, if i want to remove this VideoPlayer class from my main class is it enough to just do:
I have a initApp.as which instantiates a class which needs to access the "currentState" property and the States array as well. However we cannot get this to work as we cannot see how we can access this information.
Within initApp.as currentState is accessed via "this.currentState". This does not work in the class which is instatiated within initApp.as. The following error is thrown:
I'm wondering if anyone has experience with if there is a big difference in performance in ActionScript 3 between keeping a class with only public static functions, and utilizing those functions often (as in a frame event at 30fps), and in turning the class into a "normal" class of which I instead make an instance and call the functions via the instance instead.
I have a FLA (say Main.FLA) document class with a child MovieClip on the stage: into the child MovieClip I load other swf files: each of the files contains its own Document Class (every swf is a somewhat independent application, say quizzes and so on).For some reason I must use the Main document class to store data (scores or so) from the child swfs loaded into the Main swf. HOW do I reference the Main class? I can't find a way.[code]and dispatch an Event this way from the loaded swf document class to the Main class:[code]Now, first of all I don't know if this could even work. Secondly, I tried to make it work by adding an event listener to my Main class but id did not work.
I have a class called shapeC that only creates a rectangle and then addChild(rectangle);. That class is instantiated on the main timeline. Currently, the only way you can see that rectangle is to add the instantiated class to the stage via addChild(shapeC);. My question is, is there a way that the shapeC class can add the rectangle to the root stage without requireing the instantiated class to be added to the stage?
So I had a working .fla, with a document class package in the same directory. Everything was fine, until moving both to a new directory, now the fla cannot find the document class. WTF?
I usually use if(object!=null) but it doesn't work well.
How can I verify if the class is instantiated. I want to get rid of the 'cannot access a property of a null object or reference'.
I mean, the 'var object:Object;' is just a reference to an Object class instance. When i initialize it with 'object = new Object()' it runs the code in the constructor, initializing it. How can I check if it has been initialized or not.
I've been struggling with this for almost a day now and I can't seem to find the problem. I'm making an instantiated FLV playback class in actionscript 2, but it just doesn't play the video
Here's the code, if I uncomment the camera parts and attach _cam instead of _stream it will show the camera feed, it just won't play my movie (and I get no errors whatsoever).
Im trying to get the value of the textWidth property in class Main from a text field created in an instantiated class Label. When I trace the value from within the Label class I have access to the values. I'm trying to use a get function to pass the value to the main class.[code]
Using Flash CS5 Professional I have created a symbol, dragged it onto the stage, and given it an instance name of GreenLight1. If I want to make this visible from the document class, I can simply do the GreenLight1.visible=true; and poof it's good to go when I test the file. As long as I stay in the document class I am good to go, but now I'm trying to move to another class and hitting ALL kinds of trouble just trying to get Flash to allow me to access this simple object. All I am looking to do is have this GreenLight1 go invisible (visible=false) when a certain condition occurs in this new class and Flash just won't let me access GreenLight1 at all. Things I've tried thus far:
stage is passed to the class and is referenced by _stage and is working just fine when I do _stage.addchild or anything like that. So I have tried "_stage.GreenLight1.visible=false;" and I get "ReferenceError: Error #1069: Property GreenLight1 not found on flash.display.Stage and there is no default value." My document class extends Sprite, so I figured I'd try the root function. So I tried "Sprite(root).GreenLight1.visible=false;" and I get "1119: Access of possibly undefined property GreenLight1 through a reference with static type flash.display:Sprite." I tried to create the Resource class as described therein. To which I came across the same problem that I started with in that it doesn't know what GreenLight1 is to begin with so I got "1120: Access of undefined property GreenLight1." Here is my code for Resource.as (am I supposed to pass something to this class from the document class?)
If I have a Document class that extends MovieClip, and I want to use it as the basis for another Document class, is it possible to create a subclass that extends the main document class and use that for a different FLA?
For example,
fla1.fla has a document class of MyMainClass: public class MyMainClass extends MovieClip fla2.fla has a document class of MySubClass: public class MySubClass extends MyMainClass
I've tried, but now I'm getting errors that all of my variables that reference stage instances aren't being found.
while working with a server side script (php or aspx) which returns some data(from the database) can we call it in a sub-class or do we have to make the calls in the document class itself?
Say i need to send some data from AS 3.0 to a server-side script for it to do some actions. Can I do this in a sub-class called some where in a document class or should we always do it in a document class?
Ok, this is one of those walls that I know once I can jump over it, I will be a much happier developer again.
I've done tons of reading, and think I have a firm understanding that the general consensus is that if you want to reference something, it needs to be added to the display list, using addChild().
I hate to be defiant, but what if I don't want to?
Or at the very least, what if I want to add a movieclip class to the stage using addChild, and then reference objects inside it?
It is much easier this way than what most people recommend - adding 15 objects via addChild, then setting the x and y for the, etc.
That said, I'm all about using classes and using as3 the way it was meant to be used. So what this is, is a best practices question I guess.
HERE ARE THE STEPS I'M TRYING:
- Create new flash document
- Draw graphic symbol bg, with text field over it, select them, convert to movieclip symbol, and export class name "box", then delete it from stage
- Add document class .as file, which simply adds that class "box" from the library, to the display list using a simple addChild()
- Set a name for that box using box.name = "test" let's say
- Do a simple trace like the following - "getChildByName('test').textFieldName" - it shows up great
- So then, I'll now try to set the text by doing this - getChildByName('test').textFieldName.text = "yo";
That last line above, is what doesn't work. I know I'm referencing wrong, but how would a pro as3 developer, reference something on the stage within a movieclip class, from the document class?
i have a movie clip which is exported for ActionScript , i want to get a variable from the document class into the movie clip class i tried this but there was an error !!
ActionScript Code: var main:Main=new Main(); main.txtScore.text="hello";
I found myself loading a new XML file every day and so decided to create a small class that will load the xml file and return it.The class is loading the file but when i try to return it to a new xml object in the document class it doesnt load it.I think that it's something to with the return being in a nested function but i tried a few ways and non seems to work.
[DOCUMENT CLASS] loadXML:LoadXML = new LoadXML(); myXML:XML = new XML(loadXML.loadXMLFile("gallery.xml"));[code].....
var pic:Picture = new Picture; // custom dynamic class with a method called onLoadPIc pic.onLoadPIc = onLoadMe; i'm trying to change the method of my custom class on the fly for this instance...
I have this class called Main. And inside this Main class I have a method that I'd like to call from a the main timeline.
So on the main timeline I have instatiate the Main class like this:
var mainClass:Class = flash.utils.getDefinitionByName("Main") as Class; var main:Sprite = new mainClass(this, mc1_mc, mc2_mc); addChild(main);
Then on the frame after that, I try to access a function inside that Class like this:
main.prepareGame();
But I get this error:
Scene 1, Layer 'Layer 2', Frame 3, Line 61061: Call to a possibly undefined method prepareGame through a reference with static type flash.display:Sprite.
I'm very confused. When I instantiate a class, I'm assuming that the object is reset to its default variable values. Not so. Consider this:
Code: class ClassTest extends MovieClip{ var arr:Array = new Array(); function ClassTest(){
[Code].....
...even if the two objects have different instance names! It's as if it is making my arr variable static by default, and not resetting it to a new array in the second instance. How do I get around this? Do I need to somehow have this code instantiate itself with something like "this = new ClassTest();"
h1. I have a Class (let's call it "Button") that is linked to a library item (a movieclip) which I have on the stage
2. Now this Button class, when clicked will produce an alert in the form of another class (let's call it "Alert").
3. There are two critical things in the way I want this to work:
A) I want Alert to be a child of the main stage, not the Button class
B) I want Alert to take care of adding itself to the display list, not have Button add it.
So in a nutshell, I want to call Alert like this:new Alert("Hello, Kirupa Forums!");
The Alert class needs to draw the shape (check), add the text (check), add itself to the display list (how? if I try to access "stage" I get a null reference, though I don't know why), and destroy itself (I'll burn that bridge when I get to it).
What I'd like to do is add an event listener to a class instantiated by my document class that listens for different key presses. So far I can't use stage.addeventListener or I get the null object error so my question is:
a: how do i add an event listener to the stage from another class and b: is it even necessary to add it to the stage , can I listen from that class?
I created a movieclip with a star. Then, I creates a document class and I named it as Main.as.
In that document class I wrote: package { import flash.display.*; public class Main extends MovieClip
[Code]....
I´m not posting here all the code, but you can easily see that I´m using x_origin and y_origin variables to keep the original position of the movie clip. I will use them to move the star back to the original position.
The problem is that instead of being 20 and 30 respectively, tracing them I see that they are both ZERO. So when I drop the star, instead of going to x=20,y=30 it moves back to x=0 and y=0. I´ve to get the star position relative to stage, but I can´t find anyway!
I am using the Loader class to load 3 external swfs: sharedTopics.swf (does not have a document class) fonts.swf (document class is FontManager) main.swf (document class is Main) The same loader is used to load all 3 assets.
How to access a display object on the stage in a class which is not a document class?
I am looking for a solution that doesn't involve passing the stage as a parameter to the class.
One solution i always come across is using "TopLevel.as". Is it a good method because as far as I have heard using global variables is not recommended because it might cause some problems while working on big projects.