PDA

View Full Version : Declaring function in flash form app


meteoric
09-18-2005, 02:53 PM
May I know the different between the following function declaractions:-

==========
Style 1
==========

function test():Void {
trace("success");
}

==========
Style 2
==========

this.test = function():Void {
trace("success");
}

==========

It seems like there are some scoping differences.

I created a flash form app, where I have a root form (called "rootapp") and this root form has a child form (called "childform").

In my childapp, I'm trying to access a function from rootapp, but it seems like I can only get it to work with "style 2", but I don't know the reason.

==========
rootapp
==========

on (load) {
// style 1: cannot be refered from childapp
function test():Void {
trace("success");
}

// style 2: can be refered from childapp
this.test = function():Void {
trace("success");
}
}

==========
childapp
==========

on (reveal) {
_level0.rootapp.test();
}


Also, is there a way to reference rootapp without specifying "_level0.rootapp"? I tried "_root" and "super", but it doesn't work for me.

By the way, I'm using MX 2004 Pro.

Thanks for the help.

Xeef
09-18-2005, 03:07 PM
one diference by the function declaration is the order they are procesed :


X();
Y();
Y = function () {
trace("Y");
};
function X() {
trace("X");
}


the abowe will trace "X" just "X" bacause function "Y" isn't available at the point a call it

meteoric
09-18-2005, 03:16 PM
Hello Xeef,

Thanks for your reply.

I tried doing a trace on both rootapp and childapp's on(load) and on(reveal), it seems like rootapp's on(load) was executed before childapp's on(reveal). So, in my case both function declaractions (style 1 and 2 ) should work for me, because the function was declared before I invoked it.

However, I could only get it to work with style 2.