PDA

View Full Version : Where to define variables and functions?


hayored
12-09-2002, 12:59 PM
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

Grifter
12-10-2002, 10:29 AM
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

tg
12-10-2002, 02:15 PM
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).

red penguin
12-10-2002, 03:33 PM
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....

Mortimer Jazz
12-15-2002, 11:33 PM
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 :)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

red penguin
12-16-2002, 04:04 AM
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!

Mortimer Jazz
12-16-2002, 08:26 AM
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!

pixelwit
12-16-2002, 09:15 AM
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: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 (http://www.actionscripts.org/forums/showthread.php3?threadid=13830&perpage=15&highlight=pixelwit%20onLoad&pagenumber=2).

Hope it helps,

-PiXELWiT
http://www.pixelwit.com

tg
12-16-2002, 02:38 PM
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.

Mortimer Jazz
12-16-2002, 03:02 PM
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!

tg
12-16-2002, 03:36 PM
its always good when someone comes behind me and cleans up my messes. heh.

Mortimer Jazz
12-16-2002, 03:50 PM
Yeah, just call me Mr Mop 'n' Glow ;)

tg - you have made about 20 million posts. You have no avatar and you still come up as 'new member'.

Your mission, should you choose to accept it, is to sort that out!
This thread will self-destruct in 5 seconds .....

*cue music*

tg
12-16-2002, 03:58 PM
hows that?
better?

i'm not much for graphics, so an avatar may take me a while.

"mao tse tung said 'change must come at the barrel of a gun'" *Alabama 3*.

red penguin
12-16-2002, 04:08 PM
Red tosses Morty a gun with a very long barrel.

Red penguin says to tg, "He'll use it, he will..."

Nyar!?!?

tg
12-16-2002, 04:18 PM
what? it changed.. hehheh.

i'm workin on an avetar, but the scan job i did of the artwork i have looks like crap. and since i don't know squat about manipulating graphics, i can't get it to look good.

time to learn some new stuff i guess.

Mortimer Jazz
12-16-2002, 04:33 PM
**locks n loads **
Too right Red!

Hey tg, we're always on hand to help you make an idiot of yourself. Here you go - I quite like number 3 (zip attached)

[all work is copyright of their respective author blah blah yada yada]

red penguin
12-16-2002, 04:58 PM
Go for #3!

tg
12-16-2002, 05:19 PM
hehheh.. #3 is nice.

i'ld use it, but i hear morts rates are high.

Mortimer Jazz
12-16-2002, 06:00 PM
i'ld use it, but i hear morts rates are high

Who told you about that? I was young and I need the money oka......
Ah, you mean the pictures. Oh well, I nicked it that one off of b3ta.com I think. I wish I knew who to credit, as it's a fine piece of work.

I don't want to pressure you into having an avatar if you don't want one. It's just that ...well....Red(neck) Penguin will kill you if you don't

*hums the 'Deliverence' theme tune and walks off*

tg
12-16-2002, 06:19 PM
nah... i'm not too worried about red. he's from new york. there mostly all bleedin heart liberals up there. now if it were vilehelm, then i'ld be worried. them fellas from the south are all born with NRA memberships.

Mortimer Jazz
12-16-2002, 06:48 PM
Don't say that! He's comin' to England tomorrow. Jeez, and I just arranged to meet him and pinkaboo on the 27th.

- help, I need some good excuses! Get me outta this mess!

tg
12-16-2002, 07:02 PM
hehhehheh... good luck.