Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 1.0 (and below)

Reply
 
Thread Tools Rate Thread Display Modes
Old 06-19-2004, 05:32 AM   #1
bianster
Defender of Obviousness
 
Join Date: Dec 2003
Posts: 21
Default Pass variable reference into function, not by value

hi,

is there a way to pass the reference of a variable(a) into a function so that when a's value is changed in the function, the actual variable value is changed and not only the a in the function?

i have this code snippet below =>
ActionScript Code:
//this code is on frame 1 of the main timeline logoFaded = false; function alphaFaded(obj, targ, speed, dir, flag){     obj.onEnterFrame = function(){         temp = obj._alpha + speed*dir;         obj._alpha = Math.min(100,Math.max(temp,0));         if(obj._alpha >= targ){             if(flag !== undefined){                 flag = true;                 trace(flag);             }             delete this.onEnterFrame;         }     }; } //this code is on frame 2 of the main timeline //its purpose is to monitor the alpha tween carried out by alphaFaded() _root.createEmptyMovieClip("logoFadeListener_mc", 1); logoFadeListener_mc.onEnterFrame = function(){     if(logoFaded == true){         trace("Logo fade in complete...");         gotoAndPlay("mainTransition");         delete this.onEnterFrame;         this.removeMovieClip();     } }; alphaFaded(_root.logo_mc, 100, 5, 1, logoFaded); stop();

the variable "logoFaded" that's declared and initialised on frame 1 is being passed into alphaFaded() by value currently and only the variable "flag" is set to true.

i tried to attach a variable to logoFadeListener like this but it didnt work as well
ActionScript Code:
_root.createEmptyMovieClip("logoFadeListener_mc", 1); logoFadeListener.logoFaded = false; alphaFaded(_root.logo_mc, 100, 5, 1, logoFadeListener.logoFaded);
bianster is offline   Reply With Quote
Old 06-19-2004, 06:41 AM   #2
Crismo
Senior Member
 
Crismo's Avatar
 
Join Date: Jul 2003
Location: Norway
Posts: 296
Send a message via ICQ to Crismo Send a message via Skype™ to Crismo
Default

Flash always passes primitives by value, so the solution is to put it into an object.
This is what you tried to do whit the last attempt, but when you pass logoFadeListner.logoFaded to the function your parsing a Boolean value (primitive).
If it is to work you have to pars just logoFaderListner and then within your function ask for the variable.

ActionScript Code:
var myobject  = {status:false} //------- This is what you are doing ------------- function changeStatus(val) {     return val = true;     } var mytest = changeStatus(myobject.status) trace("Returnd value : " + mytest); trace("Object value : " + myobject.status); //-------- This is what you should try function changeObjectStatus(obj) {     return obj.status = true;     } mytest = changeObjectStatus(myobject) trace("Returnd value : " + mytest); trace("Object value : " + myobject.status);

By the way the variables parsed to the function “alphaFaded” are not within the scope of the onEnterFrame function of the Logo_mc.
Crismo is offline   Reply With Quote
Old 06-19-2004, 06:54 AM   #3
bianster
Defender of Obviousness
 
Join Date: Dec 2003
Posts: 21
Default

hi Crismo,

thanks for the heads up! it worked after i wrapped logoFaded into an object and passed the object into the function

but 1 thing is, u mentioned that the alphaFade parameters are not scoped to the onEnterFrame handler, i tried to test that out with a trace of 2 parameters in the event handler and the values were correct...
bianster is offline   Reply With Quote
Old 06-20-2004, 01:02 AM   #4
bianster
Defender of Obviousness
 
Join Date: Dec 2003
Posts: 21
Default

Quote:
Originally Posted by bianster
but 1 thing is, u mentioned that the alphaFade parameters are not scoped to the onEnterFrame handler, i tried to test that out with a trace of 2 parameters in the event handler and the values were correct...
hi Crismo,

i think i misunderstood u when u said that the function parameters were not scoped to the onEnterFrame handler. Did u mean that the parameters will not be unique to the event handler?
bianster is offline   Reply With Quote
Old 06-20-2004, 08:40 AM   #5
Crismo
Senior Member
 
Crismo's Avatar
 
Join Date: Jul 2003
Location: Norway
Posts: 296
Send a message via ICQ to Crismo Send a message via Skype™ to Crismo
Default

Nah just my old OOP lessons that are interfering with the ”whimsical magical” ways of actionscripting.

Looking at it from a purly OO angle, the moviclip function should not be able to see the variables parsed to the alphaFaded function.
But then this is what I love about Actionscript, the possibility to cheat when your in a hurry .

I have given an example of the problem that bugs me with this kind of solution.
If actionscript had been consistent, the 2 functions should both have been able to access the parsed variables, but they are not.
It could of course bee some feature of anonymous inner classes / functions etc.
But since I don’t know for sure, I’m just going to write it up as a practical feature of actionscript

It’s really not that important as long as we can choose our self to be consistent or not.

ActionScript Code:
function Test(b_mc,c,d) {     b_mc.onEnterFrame = function ()     {     trace("C : " + c);     trace("D : " + d);     } } Test(_root.my_mc,5,6); //------------------------------------------------------ function Test_2(b_mc,c,d) {     b_mc.onEnterFrame = frameFunction; } function frameFunction () {     trace("C : " + c);     trace("D : " + d); } Test_2(_root.my_mc,5,6);
Crismo is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:03 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.