My first though is that you would have your class structure as:
/[ClassTest].fla
/test/Test.as
/test/Model.as
But that would give you a compiler error, because you're not importing Model. So
you must be using
/[ClassTest].fla
/test/Test.as
/Model.as
Here's the files I am using;
/Test_class_scope.fla : (same as you described)
-----------------------------------------------
import test.Test;
var test:Test = new Test(this);
test.init( );
/test/Test.as : (same as you described)
---------------------------------------
class test.Test
{
private var m:Model;
private var clip:MovieClip;
function Test(clip:MovieClip)
{
this.clip = clip;
trace ("test");
trace (this instanceof Test);
}
public function init():Void
{
trace (this instanceof Test);
m = new Model();
trace ("m = " + m);
}
}
/Model.as :
--------------------------------
class Model
{
function Model()
{
trace("Model instantiated");
}
}
And the trace I get is:
test
true
false
Model instantiated
m = [object Object]
...but when I change your timeline code to this:
import test.Test;
var classTest:Test = new Test(this);
classTest.init( );
...the trace I get is:
test
true
true
Model instantiated
m = [object Object]
The reason for this is that in AS2, type checking occurs at compile-time, not at
runtime, so when the code is run, there are a few things that can get
potentially confused:
- your instance name is 'test'
- your package name is 'test'
- your class name is 'Test'
All of which may look to be all the same at runtime. I'm surprised this runs,
frankly. If it were in a more complex application you would most likely get
runtime errors which would be extremely hard to track down.
In AS3 there is runtime type checking, and the runtime would not get confused,
and know that 'test' and 'Test' are different, or throw an error. As it is the
AS2 runtime has loose type checking, and does not detect anything amiss, but you
get unpredictable results.
My advice would be to use best practises, and keep package names, class names
and instance names unique.
Tracing (or type checking) a Model instance or any other custom class for that
matter will always return a type of 'object' in AS2; you need to use
'instanceof' to determine the class. (m instanceof Model) will return true. In
AS3 there are far better tools, such as parsing the describeType() XML output.
Also, I'm not sure what you are trying to do with 'this.clip = clip;', but you
cannot instantiate or associate a MovieClip inside an Object (which Test is),
and have successful access to its properties and methods. MovieClips in AS2 are
these weird hybridized creatures: not quite built-in objects, not quite OOP
objects. You would have to declare 'class Test extends MovieClip', and
associate the test class with a clip in the library and instantiate that symbol
on the timeline at authortime, or at runtime with createEmptyMovieClip(). Other
runtime-instantiated classes will need to be associated with an MC library
symbol, instantiated from Test via createEmptyMovieClip().
Because of the way everything in AS2 is centred around the MovieClip class, in
order to create any kind of MVC architecture, which I gather you are trying to
do, it's all about the MovieClip. You can have your Model be an Object, but your
View and Controller classes, and any other visual display class need to be
MovieClips, as a general rule.
_______________________________________________________________
Joseph Balderson, Flash Platform Developer | http://joeflash.ca
paul cunningham wrote:
If I have this on the flash timeline, (CS3 or Flash 8)
import test.Test;
var test:Test = new Test(this);
*test.init( );*
----------------------------------------------------------------------
and then the class code has this
class test.Test
{
private var m:Model;
private var clip:MovieClip;
function Test(clip:MovieClip)
{
this.clip = clip;
trace ("test");
trace (this instanceof Test);
}
public function init():Void
{
trace (this instanceof Test);
m = new Model();
trace ("m = " + m);
}
----------------------------------------------------------------------
it traces:
test
true
false
m =
----------------------------------------------------------------------
but if I the init call originates from the constructor, instead of from the
main timeline, ie
function Test(clip:MovieClip)
{
this.clip = clip;
trace ("test");
trace (this instanceof Test);
*init( );*
}
it traces:
test
true
true
m = [object Object]
----------------------------------------------------------------------
does anyone know why the "m" value disappears when the init method is called
from the timeline?
.
Thanks in advance.
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders