Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-09-2002, 12:59 PM   #1
hayored
Registered User
 
Join Date: Dec 2002
Posts: 21
Default Where to define variables and functions?

Where in the ActionScript I should define the variables and the functions?
I mean I am looking something like Initilization paragraph in other languages. When I define it let say for a specific frame, what happens this frame is executed again? That means all variables are reinitialized again? And what will happen to the function definition?

Thanks,

YH
hayored is offline   Reply With Quote
Old 12-10-2002, 10:29 AM   #2
Grifter
hopeful
 
Join Date: Apr 2002
Posts: 248
Default

as you've pointed out, re-initialising would be hassle, so flash doesn't require you to initialize the variables that you use. instead, you can create and alter them on the fly.
so you can just assign a string or number (or whatever) to a variable like so:
myVariable = "Flash is so great";
myVariable2 = 100;
myVaribale3 = myVariable + ",it really is";

and when you go over the same frame, then the value is assigned to the variable again, so you can use an iterative thing to increase the value of a variable:
myVariable += 100; //this would add 100 to the variable each time the frame is run

another thing you might be interested to know is that variable numbers begin as 0, so if you were to do myVar +=100, it would add 100 to 0 for the first value. Boolean variables begin as false.

i'm sure there's a better explanation somewhere, but i think this covers it.

hope you understand
Grifter

Last edited by Grifter; 12-10-2002 at 10:34 AM.
Grifter is offline   Reply With Quote
Old 12-10-2002, 02:15 PM   #3
tg
done
 
Join Date: Jun 2001
Location: portland, or
Posts: 8,106
Default

make a note tho. if you dont use the key word 'var' all variables you creat will have scope at the _root level. this can create problems if you are not careful. using the 'var' to declare your variables gives them scope (and thats always a good).
__________________
tg
---
what the hell was i thinking?
tg is offline   Reply With Quote
Old 12-10-2002, 03:33 PM   #4
red penguin
[^\d\D]
 
red penguin's Avatar
 
Join Date: Jun 2001
Location: Brooklyn, NY
Posts: 3,254
Default

Another thing to do would be to, on the first frame, create an INIT function amd then call it using a simple if statement (if (init != true)), setting a var 'init' to true in the function, and thus that function would initialize all vars once and once only...

Make sense? Bad explanation for the idea but I hope it came across. Caffeine inna blood....
__________________

komielan.com
red penguin is offline   Reply With Quote
Old 12-15-2002, 11:33 PM   #5
Mortimer Jazz
Thing
 
Mortimer Jazz's Avatar
 
Join Date: Jun 2001
Location: UK
Posts: 2,418
Default

Quote:
if you dont use the key word 'var' all variables you create will have scope at the _root level
May get some people a little confused........

Place an empty mc on stage and inside it put _root.scopeMeUpBaby=10;

Then place a trace on the main timeline: trace(scopeMeUpBaby);
This traces it fine. Now take away the "_root." part and you just get 'undefined'.

See thread below
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
__________________
............................
FLEX/FLASH BLOG
Previous Flash Tutorials

............................

Last edited by Mortimer Jazz; 12-16-2002 at 08:21 AM.
Mortimer Jazz is offline   Reply With Quote
Old 12-16-2002, 04:04 AM   #6
red penguin
[^\d\D]
 
red penguin's Avatar
 
Join Date: Jun 2001
Location: Brooklyn, NY
Posts: 3,254
Default

Better not mix wine and AS, Morty.

*Red finishes off his beer.*

I can't explain variable scope right now. Too much to get out... That will be undefined as without the '_root' in there, you are declaring that variable on that clip. Thus it lives there.

*Red cracks open another beer*

Come on! It's Sunday night!
__________________

komielan.com
red penguin is offline   Reply With Quote
Old 12-16-2002, 08:26 AM   #7
Mortimer Jazz
Thing
 
Mortimer Jazz's Avatar
 
Join Date: Jun 2001
Location: UK
Posts: 2,418
Default

Now that I'm as sober as a judge I am able to edit the post and make it more clear what I was getting at

I already know that definin' a variable with var in a function which is on the root confines it to that function (and it only lives whilst the function is running) whilst defining it on a clip should contain that variable to the clip's timeline.
Maybe that's with tg was getting at?


I remember the moock book saying something about 'var' not working quite as intended in flash in certain situations but I'll be darned if I can find it!

Red, better lay off the drink yourself if your avatar is anything to go by
What's it like to be home and surrounded by compadres again?
Send us a mail old boy - I can't remember which address you're at now!
__________________
............................
FLEX/FLASH BLOG
Previous Flash Tutorials

............................

Last edited by Mortimer Jazz; 12-16-2002 at 08:33 AM.
Mortimer Jazz is offline   Reply With Quote
Old 12-16-2002, 09:15 AM   #8
pixelwit
village halfwit
 
pixelwit's Avatar
 
Join Date: Jul 2001
Location: USA, PA
Posts: 3,330
Default

I think I'm with Mort on this one.

A variable in a clip without the "var" declaration automatically takes the scope of the clip in which the code is currently executing.

On a different note...

As Mort said previously, a var declared within a function takes the scope of that function and is deleted after the function has executed. If however you would like to keep the variable within the function after it has executed, you need to define the variable in a different fashion. It goes a little like this:
ActionScript Code:
Math.unique = function(){     return ++Math.unique.num; } onEnterFrame = function(){     trace(Math.unique());     trace(Math.unique.num)     trace(_root.num);     trace(""); }
I used something similar to this in the infamous onLoad thread.

Hope it helps,

-PiXELWiT
http://www.pixelwit.com
__________________
There are no answers, only choices.
pixelwit is offline   Reply With Quote
Old 12-16-2002, 02:38 PM   #9
tg
done
 
Join Date: Jun 2001
Location: portland, or
Posts: 8,106
Default

yep mort your right... i should have said it gets the scope of the current (damn... see now this can be confusion too) timeline. sorry for the confusion....
i still think it is always a good idea to scope all your variables with 'var'... you'll never have this worry.
__________________
tg
---
what the hell was i thinking?

Last edited by tg; 12-16-2002 at 02:44 PM.
tg is offline   Reply With Quote
Old 12-16-2002, 03:02 PM   #10
Mortimer Jazz
Thing
 
Mortimer Jazz's Avatar
 
Join Date: Jun 2001
Location: UK
Posts: 2,418
Default

I know you knew what you meant , and I think you know I knew you knew what you meant. It's just that I knew hayored might not know what you meant, so I thought i'd make it clear.

Meanwhile pixelwit and red know everything.

Hmmm, I've just seen hayored crying in the corner, mumbling something about only having asked a simle question. Sorry dude!
__________________
............................
FLEX/FLASH BLOG
Previous Flash Tutorials

............................
Mortimer Jazz 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:23 AM.

///
Follow actionscriptorg on Twitter

 


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