all groups > macromedia flash sitedesign > september 2004 >
You're in the

macromedia flash sitedesign

group:

classes and variables


classes and variables mtx
9/4/2004 10:43:09 AM
macromedia flash sitedesign:
I have defined a class containing global variables that I want to access
from within a MovieClip object's onEnterFrame function.

The class looks something like this:

class MyClass
{
var MyMovie:MovieClip;
var i:Number = 10;

function MyClass(MyMovie:MovieClip)
{
this.MyMovie = MyMovie;
myFunction();
}

function myFunction()
{
MyMovie.onEnterFrame = function() {
trace(i);
};
}

}

How do I access variable 'i' from within the 'MyMovie' movie clip's
onEnterFrame function? If I try this i is listed as 'undefined'.

Thanks.


Re: classes and variables Pierre Canthelou
9/5/2004 8:39:57 PM
First, when you wrote "MyMovie.onEnterFrame = function() { ... }, you are
within the MyMovie object, no more a MyClass object.

So, you have to insert a new variable in the MyMovie, says "i"

function myFunction()
{
MyMovie.i = this.i;
...
}

But another way is to make your Movie inherit your MyClass class !

class MyClass
{
var aNumber : Number = 10;

function onEnterFrame()
{
trace( this.aNumber );
}
}

Shorter, nicier !

Pierre Canthelou
http://www.codesign.fr


[quoted text, click to view]

Re: classes and variables mtx
9/5/2004 8:53:37 PM
How do I make the movie inherit 'MyClass'? What I want to do is attach a
MovieClip that exists in the library and set certain parameters using a
class. So, for example, I want to do it like this:

var myNewMovieInstance:MyClass = new MyClass(300,400);

which would create a new movieclip (that exists in the library) and place it
on the stage at the supplied x and y positions. How should this be done
using classes?


[quoted text, click to view]


AddThis Social Bookmark Button