PDA

View Full Version : am i doing this wronlgy?



kneural
January 27th, 2008, 12:44 AM
Hi,

Ok, i have a MovieClip in my library with a function that uses variables declared just above it like so;




var myString:String = "this is a string";

function traceVal():void
{
trace(myString);
}

traceVal(); // Returns "this is a string"

okay. this works after i drag and drop the mc from the library to the stage.

later on, i right clicked on the linkage properties of the mc and made it into a Class called "Tracer" with a base class of "flash.display.MovieClip", and removed the traceVal(); caller,

back on stage, when i tried to make an instance of the Class and call a nested function in it. It was unable to return me the variables which was declared in the MovieClip




var my_tracer:Tracer = new Tracer();

addChild(my_tracer);

my_tracer.traceVal(); // Retruns "null"

am i doing this wrongly? can anyone help me?

daleth
January 27th, 2008, 03:47 PM
You can't call my_tracer.traceVal(); if you haven't defined a method for that class called traceVal, which is why it's returning "null."

You'd have to do something like this:


function traceVal(){
trace("test");
}
my_tracer.traceVal = traceVal;
my_trace.traceVal(); //returns "test"


In general AS3 discourages this. You can't do this with Sprites for instance, because it (like most AS3 classes) is static. They kept MovieClip dynamic mostly to make it more comfortable for people used to the AS2 way of doing things. Kind of annoying, but it's for solid reasons that have to with the performance of the AS3 virtual machine.