Topic: erm... attachMovie in class constructors? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=11466" title="Pages that link to Topic: erm... attachMovie in class constructors? (Page 1 of 1)" rel="nofollow" >Topic: erm... attachMovie in class constructors? <span class="small">(Page 1 of 1)</span>\

 
Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 10-19-2003 03:05

So, I've got this class, which is supposed to load, place, and control all the graphics in the game. Let's call it "GameStage." I'm trying to get its constructor to summon up and place a bunch of MovieClip elements, then there are a bunch of public instance methods that move the elements around.

So, for example....

code:
[i]// load all graphics[/i]
stage = new GameStage();

[i]// move graphics around[/i]
stage.moveGraphics();



My class is looking something like this...

code:
class GameStage
{
[i]/* CONSTRUCTOR */[/i]
function GameStage()
{
graphic_mc = attachMovie("graphic_library_name", "instance_name", 10);
graphic_mc._x = 100;
graphic_mc._y = 100;
graphic_mc._visible = true;
}

private var graphic_mc:MovieClip;
}



And when I try it out, I'm informed that there is no such method as "attachMovie." Fine, I think -- attachMovie should be in the global scope, but apparently it doesn't exist in packaged classes.

So then I try this:

code:
graphic_mc = MovieClip.attachMovie(blah blah blah);



And I get an error saying that the attachMovie method is not static. Okay...

code:
graphic_mc = MovieClip.prototype.attachMovie(blah blah blah);



No compile-time errors... but trace() shows that graphic_mc is still undefined. I've tried tossing "graphic_mc = new MovieClip();" in front of the constructor a few times, but no go.

Any ideas? In Java, the correct practice is to create an object for the view (i.e. graphics, animation, user actions) and an object for the model (i.e. the "document" being worked on), then the "main code" is just a simple control loop... but if Flash doesn't want to give class objects access to library assets, then that's pretty much out the window. I might as well just make a big list of functions that get imported to the main timeline. I hope, of course, that I'm just missing something.

Bwa-ha! I was in fact missing something! I'll post this just so that anyone with the same problem can find the answer here. My code should have been this:

code:
graphic_mc = _root.attachMovie(args);



This might be obvious to anyone with a little more experience than me, but for those with less, here you go.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

Steve
Maniac (V) Inmate

From: Boston, MA, USA
Insane since: Apr 2000

posted posted 10-19-2003 19:36

PT - just for the sake of clarity in other posts - are you currently using MX 2004? If no, there are some big problems in this code. (MX doesn't have a "class" keyword, and isn't strongly typed to give two examples of what I mean.)

If so, I don't know how helpful I can be in the short term future, not having it myself.

If the goal of the class GameStage is to place a bunch of clips on stage and move them around, the snippet you showed looks strangely rigid, but I suppose it was just an early test to get attachMovie working from inside a function.

two thought; - use "this", and "with" cuts down some on typing:

code:
function GameStage()
{
graphic_mc = attachMovie("graphic_library_name", "instance_name", 10);
graphic_mc._x = 100;
graphic_mc._y = 100;
graphic_mc._visible = true;
}



might become:

code:
function GameStage()
{
this.graphic_mc = _level0.attachMovie(args);
with (this.graphic_mc){
_x = 100;
_y = 100;
_visible = true;
}
}


(_level0 and _root are essentially the same thing. Another handy trick is to put near the beginning of the first frame of the main timeline a persistant reference to _root:

code:
_global.mainTL = this;


You have added a property ("mainTL") to the _global variable, and that property points to _root. Now you can reference _root anywhere, anytime by calling mainTL (or whatever you choose to call it.)

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 10-19-2003 23:36

Hey, quite handy -- I didn't know about with to set a bunch of properties (or, I assume, access a bunch of methods) at once. And yes, I'm using MX 2004 for this project (or else I'd still be frustrated, because it still wouldn't work!) The strict typing is unnecessary for this project, but I'm trying to teach myself good habits. And making a global reference to _root is an excellent idea, especially since I'm going to be doing some nesting. Thanks for the tip.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 10-20-2003 03:07

Well, back to scope problems -- and this is MX-relevant, so I hope you can give me a hand. It's very simple: I have a generic object acting as a keyboard handler, and within the onKeyDown inline function I want to refer to an object on the same level as the handler object. An example should make it more clear:

code:
var test = 12;

var watch_keyboard = new Object();
watch_keyboard.onKeyDown = function()
{
trace("Key pressed.");
trace(test);
}
Key.addListener(watch_keyboard);



When I do this, hitting any key displays "Key pressed" as it should, but the variable test is undefined. So I tried _parent.test, and _parent._parent.test (just to see), and nope. Same with _root.test, and so forth. I even tried this._parent.test.

A newly-created object should be able to access variables that are available to the scope at which it was created, right? There has to be a way to do it, I just don't know what it is.

edit: Sorry, a little itchy on the Post finger.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

[This message has been edited by Perfect Thunder (edited 10-20-2003).]

[This message has been edited by Perfect Thunder (edited 10-20-2003).]

Steve
Maniac (V) Inmate

From: Boston, MA, USA
Insane since: Apr 2000

posted posted 10-20-2003 03:45

I copied your code, pasted it unmodified into a new Flash MX movie, and the output window displayed:

quote:
Key pressed.
12



Which is what I presume you were expecting.
Added a line:
trace(typeof test);
Output window:

quote:
Key pressed.
12
number


Tried using the variable in a calculation:

code:
trace("Key pressed.");
trace(test+16);
trace (typeof test);



output:

quote:
Key pressed.
28
number



Likewise, adding
trace(Key.getCode());
worked.
Wonder why it's not working on your system.



[This message has been edited by Steve (edited 10-20-2003).]

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 10-20-2003 04:09

(sigh) I guess it's something 2004-related after all. I should probably just code to MX practices until 2004 has been out long enough to have a reasonable penetration -- it'll make it more likely that my problems can be resolved with simple Googling.

The event handler object gets created in a class constructor, and the function tries to access a variable that exists within the scope of the class. It doesn't make a difference whether I declare the variable as private, public, or default. I'll post the problem on the forum of one of the bigger Flash sites, where there's sure to be a higher Flash guru density, and get back here with the solution. You've been very helpful already, Steve, I don't want to researching new things just for me.

Example code, just in case you notice something:

code:
class TheClass
{
[i]/* constructor */[/i]
public function TheClass()
{
handler.onKeyDown = function()
{
trace(test);
}
Key.addListener(handler);
}

[i]/* variables */[/i]
public var test = 12;

private var handler = new Object();
}



Then of course it's invoked with a little "thing = new TheClass()" statement in the main program logic.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

Steve
Maniac (V) Inmate

From: Boston, MA, USA
Insane since: Apr 2000

posted posted 10-20-2003 04:54

Beyond me tonight.

I've always had a good experience at We're Here.
Try their ActionScript 2 forum.

Good luck.

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 10-20-2003 23:56

Well, I found a solution!

code:
[i]// in the class constructor...[/i]
this.onKeyDown = function()
{
[i]// actions referring to variables within constructor's scope[/i]
}

[i]// in variable declaration [/i]
var onKeyDown:Object;



Essentially, I'm registering the class itself as a listener -- which is what I should do anyway, I suspect, since the class is the "view" element of a model-view architecture. I have one class that sets up and animates graphic and UI element; one class that accepts user input (this one), and one class that governs the underlying data model of the program. As long as it works, and as long as I think I'm going by the book, I'm happy.

Sleeping on it was all it took for me to get the idea. Now, I did sleep fourteen hours (eep!), so I damn well better have come up with some ideas...

Cell 1250 :: alanmacdougall.com :: Illustrator tips

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 10-30-2003 16:01

For anyone who might be using this thread for help, I feel like mentioning something I only found out after some troubleshooting... to register a new variable as a property of an object, you can't use with(). For example, this code...

code:
with (existing_object)
{
new_var = 0;
}



...will NOT create new_var as a property of existing_object. It'll just be a free-floating variable (and, by the way, it'll escape MX 2004's compile-time property-checking -- you can define it in a with() statement without using a formal var statement. You can only make it a property of an object with...

code:
existing_object.new_var = 0;



I assume that you could also do...

code:
var existing_object.new_var;
with (existing_object)
{
new_var = 0;
}



After Steve suggested the with() syntax, I started using it for a lot of things, but didn't know about this particular hitch.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

Steve
Maniac (V) Inmate

From: Boston, MA, USA
Insane since: Apr 2000

posted posted 10-31-2003 17:58

I had never tried to use "with" to declare a new variable/property either, so I didn't know it couldn't be done.

I've always used it to apply values to existing properties. It's a convience, only having to type the path to an object once. The path and object identifier can get pretty lengthy sometimes.

Ah - this triggered a distant memory of an article on scope chain.

Way down near the bottom is an interesting nugget about "with" and the scope chain. This is a pretty in depth article. I assume much of it still applies to MX04, even though the prototype paradigm has changed. Useful read.

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 11-02-2003 19:59

(sigh) Okay, I glanced at the article without reading it, and then I did some programming... how do you like that, I can't access private variables from inside a with() statement! Looks like I'd better read the article ASAP... thanks for the pointer, Steve.

edit: Aha! The Actionscript Dictionary itself says this:

quote:
To set a variable inside a with action, the variable must have been declared outside the with action or you must enter the full path to the Timeline on which you want the variable to live. If you set a variable in a with action without declaring it, the with action will look for the value according to the scope chain. If the variable doesn't already exist, the new value will be set on the Timeline from which the with action was called.



Although that doesn't explain why the with() statemetn can't access private variables on the ... oh. Yeah. Yeah it does.

If I have a class property that is a reference to an object, that object does NOT automatically have access to private properties of the class. (To put it another way, the object is not part of the class or class instance, the class simply uses it.) This is a natural result of the object model. What's more, it starts to explain some of the other things I'd just been working around.

(sigh) Time to rejigger my program yet again... thanks for setting me on the right track, Steve.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

[This message has been edited by Perfect Thunder (edited 11-02-2003).]

[This message has been edited by Perfect Thunder (edited 11-02-2003).]



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu