Groups | Blog | Home
all groups > flash actionscript > october 2004 >

flash actionscript : Help with a Function Scope


vectorman69
10/10/2004 7:21:36 PM
Hi!

I have a class "B", with a method "drawB", this method asigns the event
handlers for instances created from my class B, each of them call a function.
Now, the problem is where I place the function, ie. if I put the function
inside the brackets of the event handler caller, it works:
class B extends MovieClip{

public function drawB():Void{

[instanceName].onRelease = function():Void{

function1();

}
function function1(){ //code here }
}

} //end class

Now if I put the function "function1" outside the scope of the drawB function,
it doesn't work...

what's wrong with this?

any help is very welcome!

thanks in advance
M




vectorman69
10/12/2004 12:24:18 AM
Anybody?

vectorman69
10/12/2004 1:08:35 PM
Blade alSlayer
10/12/2004 2:58:04 PM
Try it this way:

class B extends MovieClip{
public function drawB():Void{
[instanceName].onRelease = function1; // no anonymous function here to
call function1, just a reference to it
// could it be: this.onRelease that you mean?!?!
}
public function function1():Void{
// your code here
}
}

-OR-
Do you really need function1? You could do it this way:
...
public function drawB():Void{
[instanceName].onRelease = function(){ // this.onRelease maybe?
// your code here
}
}
vectorman69
10/12/2004 4:39:16 PM
thanks for your answer... actually this is the way I'm doing, but I need to
reuse my function, that's way I want to put outside the brackets, and it
doesn't work, but I don't understand why.

regards
Blade alSlayer
10/14/2004 2:16:43 PM
vectorman69
10/14/2004 4:43:48 PM
Didn't work, it is reusable, but when I assign the function for the onPress, it seems that does not see the function...

Blade alSlayer
10/18/2004 9:46:18 AM
tusharwadekar
10/18/2004 1:32:02 PM
Here is a proper OOP way to solve this issue,

class B extends MovieClip{

public function drawB():Void{
var classInstance:Object = this;
[instanceName].onRelease = function():Void{
classInstance.function1();
}
function function1(){ //code here }
}

} //end class

basically you have store the class referance in a variable to which you will
have access in onRelease function.
vectorman69
10/18/2004 2:44:41 PM
tusharwadekar: thank you very much for your answer, it's what I was looking for.

thanks again!!!

Blade alSlayer
10/19/2004 2:32:51 PM
Could it be
this.function1();
instead of
tusharwadekar
10/21/2004 12:45:41 PM
nope..
kbrandon
1/31/2005 1:45:16 PM
On the Event Handler Scoping issue (example 2) I wanted to understand how
"this" loses its context upon exection. Event Scoping has been the issue of a
few thread, which all tend to believe "this" must be stored in a local variable
to preserve the value at the time of event handler execution. The Answer
response in the Link below makes this exact cliam with the same snipet of code.
Basically do you believe this to be the best and only solution to the delima?

<a
href="http://www.macromedia.com/cfusion/webforums/forum/messageview.cfmcatid=288
&threadid=903202">http://www.macromedia.com/cfusion/webforums/forum/messageview.
cfm?catid=288&threadid=903202 </a>

Thanks again for sticking with this issue...
Jeckyl
2/1/2005 9:18:59 AM
See my reply in your other thread.
--
All the best,
Jeckyl

AddThis Social Bookmark Button