Be a Supporter!

AS: Variables

  • 7,951 Views
  • 30 Replies
New Topic Respond to this Topic
Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
AS: Variables 2005-08-09 08:58:51 Reply

AS: Main, OMG OMG OMG!

Variables

So, why am I writing this? Well, for starters, there have been many complaints about the existing AS: Variables, no offense to you -Toast-.
Also, variables is an elementary part of actionscripting.

So, what is a variable?
Basically, it's a word that contains information. It can be a string, a number, or any other kind of data.
Each variable can hold one value only, but the value can be changed during runtime, allowing Flash Player to save data such as score, name, speed of an object etc. Flash Player remembers the value the variable holds, and anywhere you type the name of the variable, it recognizes the value and uses it.

Sounds good. So, how do I use these variables?
Well, first of you must declare the variable, in other words create or initialize it. This is because the variable's value might change during run-time. For example, if you have to add 10 to a variable named myScore, and myScore doesn't already have a value, Flash Player doesn't know what the new value should be.

Declaring by scoping

Whaddaya mean, scoping?
A variable's scope is a reference to where the variable "lives", in other words where it's value can be retrieved or changed. Three types of scopes exists:
Local,
Global,
and Timeline variables.
Each one of these is declared in their own way.

Local variables "lives" in one function only, the one they're declared in. When that function ends, the variable expires, it can no longer be retrieved or changed. You declare a local variable like this (Also see Strict data typing further down):
var variableName = value;
For example:
var myName = "John";
Two advantages with local variables: They make the function independent from other code in the Flash file, and there can be many local variables with the same name, assuming they are declared in different functions.
Learn more about functions in AS: Functions by Inglor.

Global variables are variables that are accessible from everywhere in your Flash file. They can be changed and read from every timeline and scope, as long as you don't have a local or timeline variable with the same name. In that case, the global variable can only be accessed from outside the other variable's scope. When you declare a global variable, you can't use var, which means you can't apply Strict data typing to a global variable. Instead, you use the word _global when declaring. Like this:
_global.variableName = value;
For example:
_global.myPassword = "3ehjTEx56";
A global variable can be declared anywhere, just remember, and this goes for all scopes, that the variable must be declared before you can access it.

Timeline variables "lives" on one timeline, the one they are declared on. I would point you to a hierarchy tutorial, but I don't know of any good one.
The variable can be accessed from anywhere on that timeline, BUT!, it can also be accessed from another timeline, using dot syntax (.). A timeline variable is declared the same way as a local variable, with the difference that it's not declared in a function (Strict data typing is applicable to timeline variables too, see further down):
var variableName = value;
For example:
var myDate = new Date();

When you have declared a timeline variable, it can be accessed from another movieclip like this:
targetPath.variable
For example, this adds the value of mySpeed on the main timeline (_root) to the property _x:
_x += _root.mySpeed;

It's also worth to note that a movieclip is also a kind of variable (Thanks Inglor for that one =P). In fact, everything that returns something else than itself can be considered, since it holds a value.

Strict data typing

When you define a local or timeline variable, you can assign them a specific data type using Strict data typing. This helps a great deal when you debug your code. Instead of having to look through a whole Flash file, a message tells you the exact spot of the error.
The pattern for defining a variable using Strict data typing is:
var variableName:variableType = value;

var tells Flash Player that a new variable is being declared.

variableName is what you want your variable to be called. This is the name you will use to access the variable's value, and the name you will use to change the value that the variable contains.

variableType tells Flash Player what kind of data the variable is supposed to hold. Use this to avoid giving a variable a value of the wrong class. Let me give an example:
var myScore:Number = 5;
This gives the variable myScore the value 5. But suppose that you later in the Flash file write this:
myScore = true;
When you then test your movie, Flash will give you an error saying "Type mismatch" and the place where you typed wrong, which helps an awful lot when you look for errors in your code.

value is, you guessed it, the value that you want to give your variable.

Naming a variable

When naming a variable, remember to always give unique names, which doesn't interfere with any of Flash's Keywords (built-in properties, methods etc.). A good way to avoid interfering with those is to use variable names like:
myName
myDate
myScore
myAge0
myAge1
myAge2
...

Also, remember that variable names is case sensitive! myScore is NOT the same as Myscore.

Deleting a variable

This is really simple. Sometimes you will not need a variable anymore, and want to free up the memory it uses. Then, simply type:
delete variableName;
For example:
delete myScore;
Simple as that.

Phew! I guess that's it.The hardest part of writing this was to structure it and put it in a good order, so I'm sorry if it seems a little messy. I guess I should've made a disposition, but... Oh well. Any questions, ideas or corrections? Post them here!

PS. I'm Swedish, so my use of English may be bad...


BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Variables 2005-08-09 09:03:31 Reply

Great work, I'll just add properties:

Properties

Each movieclip or object also has it's properties properties are like built in variables that effect what happens, some of those properties are "read only " (like _version _currentframe and _totalframes)) and some effect all of the objects at once (like _quality)

Properties are a different kind of variables, they differ from attributes (which are really any variables a object-type (class) has), they in flash actually effect stage actions.

for example, and object's _y property will effect how high it is and it's _x where it is too.

movieclip objects have many properties like _alpha _visible and more, some properties like "_depth" are "invisible" to you and you have to see but you can still access them through methods (explained in AS: Functions).

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Variables 2005-08-09 09:06:06 Reply

At 8/9/05 09:03 AM, Inglor wrote: Great work, I'll just add properties:

Thanks!


BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Variables 2005-08-09 09:08:48 Reply

A good variable type for storing data is an array, these come very handy, read more!

Denvish's AS: Arrays

Kipling
Kipling
  • Member since: May. 4, 2004
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to AS: Variables 2005-08-09 10:32:53 Reply

Finally. Toast can eat his heart out.

bladeofluigi
bladeofluigi
  • Member since: May. 15, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Variables 2005-08-26 05:10:20 Reply

So wait, how were the variables used in another weird ordeal and health bars in sidescrollers, etc.etc?


Like the moon over
the day, my genius and brawn
are lost on these fools
~Bowser, in Super Mario RPG

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Variables 2005-08-26 05:15:47 Reply

At 8/26/05 05:10 AM, bladeofluigi wrote: So wait, how were the variables used in another weird ordeal and health bars in sidescrollers, etc.etc?

Ask properly, because being specific will get you the answer you want, a question like that will only result in confusion for all and if anybody told you an answer, chances are it would be completly inane.


Sup, bitches :)

BBS Signature
bladeofluigi
bladeofluigi
  • Member since: May. 15, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Variables 2005-08-26 06:01:44 Reply

oK, umm, I was working on a sidescroller, and a friend of mine told me I could give him a health bar by making sidescrollers, so that whenever an enemy hit him, he would lose 1/3 hp. Also, you may have already played, "http://www.newgrounds.com/portal/view/272
37" What were the variables behined that game?


Like the moon over
the day, my genius and brawn
are lost on these fools
~Bowser, in Super Mario RPG

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Variables 2005-08-26 06:07:23 Reply

At 8/26/05 06:01 AM, bladeofluigi wrote: oK, umm, I was working on a sidescroller, and a friend of mine told me I could give him a health bar by making sidescrollers, so that whenever an enemy hit him, he would lose 1/3 hp.

Ok, well just have var health:Number=100 on the frame and then on the hitTest put _root.health-=whatever

l so, you may have already played, "http://www.newgrounds.com/portal/view/272
37" What were the variables behined that game?

I don't know what variables the guy used, try e-mailing the author.


Sup, bitches :)

BBS Signature
bladeofluigi
bladeofluigi
  • Member since: May. 15, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Variables 2005-08-26 06:23:31 Reply

You never explained about another weird ordeal


Like the moon over
the day, my genius and brawn
are lost on these fools
~Bowser, in Super Mario RPG

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Variables 2005-08-26 08:33:10 Reply

You can't know what variables another author uses unless you ask him, besides there's not many particular "ways" to use variables. Explain what you want to know about it, and I might be able to help you. Or, e-mail the author like liam said.


BBS Signature
BrknPhoenix
BrknPhoenix
  • Member since: Dec. 1, 2005
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to AS: Variables 2005-12-26 13:39:38 Reply

Sorry to dredge up an old topic, but could someone provide a better explanation of timeline scope variables? Timeline is a scope that is unique to Flash and no other programming language, and yet does not have a very detailed description here as to where exactly a timeline variable is declared, and what is considered the root timeline when using _root. to reference it.

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Variables 2005-12-26 13:43:57 Reply

At 12/26/05 01:39 PM, BrknPhoenix wrote:

This is whereAS: Hierarchy comes in handy.


BBS Signature
sysrq868
sysrq868
  • Member since: Feb. 9, 2005
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to AS: Variables 2006-01-23 11:14:07 Reply

Can and how can variables contain multiple lines/rows?


I'm built from the leftover parts.

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Variables 2006-01-23 11:23:41 Reply

At 1/23/06 11:14 AM, sysrq868 wrote: Can and how can variables contain multiple lines/rows?

Depends how you mean. If you mean a variable with string data, then yes it can have multiple rows. Either by using the newline character (\n):

var myString:String = "This is a string.\nThis is the same string but another row";

You can also use the constant newline:

var myString:String = "This is a string." + newline + "This is the same string but another row";

I guess that's what you mean =)


BBS Signature
Storm
Storm
  • Member since: May. 19, 2006
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS: Variables 2006-06-13 08:19:23 Reply

Do you think an adventure game could be made using variables?

Like making variables called HP and Charm and Strength etc...

Adventure game such as Pico Sim Date or any of those dating sims games

MadGiraffe
MadGiraffe
  • Member since: Jan. 7, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS: Variables 2006-06-13 08:24:59 Reply

At 6/13/06 08:19 AM, Sstorm wrote: Do you think an adventure game could be made using variables?

Like making variables called HP and Charm and Strength etc...

Adventure game such as Pico Sim Date or any of those dating sims games

Yes, you can. They are.

phyconinja
phyconinja
  • Member since: Sep. 18, 2004
  • Offline.
Forum Stats
Member
Level 25
Blank Slate
Response to AS: Variables 2006-06-13 08:45:54 Reply

At 6/13/06 08:19 AM, Sstorm wrote: Do you think an adventure game could be made using variables?

Like making variables called HP and Charm and Strength etc...

Adventure game such as Pico Sim Date or any of those dating sims games

shure!! go ahead!!

Bezman
Bezman
  • Member since: Aug. 16, 2001
  • Offline.
Forum Stats
Member
Level 29
Melancholy
Response to AS: Variables 2006-06-27 20:27:04 Reply

At 8/9/05 08:58 AM, Rantzien wrote: The pattern for defining a variable using Strict data typing is:
var variableName:variableType = value;

Sorry for digging, but when did strict data typing come about?

I mean what version of Flash?

Also, are the only valid 'argument-majigies' for 'variableType' either 'number' or 'string'?

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Variables 2006-08-13 05:54:46 Reply

At 6/27/06 08:27 PM, Bezman wrote: Sorry for digging, but when did strict data typing come about?
I mean what version of Flash?

I haven't dug into it, but I believe it came with ActionScript 2.0. Not sure about that though.

Also, are the only valid 'argument-majigies' for 'variableType' either 'number' or 'string'?

Nope, there are loads of datatypes:

# Accessibility
# Array
# AsBroadcaster
# BevelFilter (flash.filters.BevelFilter)
# BitmapData (flash.display.BitmapData)
# BitmapFilter (flash.filters.BitmapFilter)
# BlurFilter (flash.filters.BlurFilter)
# Boolean
# Button
# Camera
# Color
# ColorMatrixFilter (flash.filters.ColorMatrixFilter)
# ColorTransform (flash.geom.ColorTransform)
# ContextMenu
# ContextMenuItem
# ConvolutionFilter (flash.filters.ConvolutionFilter)
# CustomActions
# Date
# DisplacementMapFilter (flash.filters.DisplacementMapFilter)
# DropShadowFilter (flash.filters.DropShadowFilter)
# Error
# ExternalInterface (flash.external.ExternalInterface)
# FileReference (flash.net.FileReference)
# FileReferenceList (flash.net.FileReferenceList)
# Function
# GlowFilter (flash.filters.GlowFilter)
# GradientBevelFilter (flash.filters.GradientBevelFilter)
# GradientGlowFilter (flash.filters.GradientGlowFilter)
# Key
# LoadVars
# LocalConnection
# Locale (mx.lang.Locale)
# Math
# Matrix (flash.geom.Matrix)
# Microphone
# Mouse
# MovieClip
# MovieClipLoader
# NetConnection
# NetStream
# Number
# Object
# Point (flash.geom.Point)
# PrintJob
# Rectangle (flash.geom.Rectangle)
# Selection
# SharedObject
# Sound
# Stage
# String
# StyleSheet (TextField.StyleSheet)
# System
# TextField
# TextFormat
# TextRenderer (flash.text.TextRenderer)
# TextSnapshot
# Transform (flash.geom.Transform)
# Video
# XML
# XMLNode
# XMLSocket
# XMLUI

More info on data types


BBS Signature
Siza
Siza
  • Member since: Jun. 16, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Variables 2006-08-30 13:14:47 Reply

hey thanks for the help i really appreciate it

Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS: Variables 2006-08-30 13:27:48 Reply

At 8/13/06 05:54 AM, Rantzien wrote: big list

I'd actually quite like to see an explaination of what all those do. I can recognise most of them, but a lot seem very obscure. Also, what the hell is an Accordion?


BBS Signature
Depredation
Depredation
  • Member since: Sep. 5, 2005
  • Offline.
Forum Stats
Member
Level 17
Game Developer
Response to AS: Variables 2006-08-30 14:38:01 Reply

At 8/30/06 01:27 PM, Brother_Paranoia wrote: I'd actually quite like to see an explaination of what all those do. I can recognise most of them, but a lot seem very obscure. Also, what the hell is an Accordion?

Accordian Menu Item Possibly? I'm not sure, but that seems to make sense, lol. Yeah, i've never come across some of those datatype wtf is DisplacementMapFilter?

Or am i stupid?

BBS Signature
Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Variables 2006-08-30 19:47:10 Reply

At 8/30/06 01:27 PM, Brother_Paranoia wrote: Also, what the hell is an Accordion?

That's the Accordion component's class.

At 8/30/06 02:38 PM, Depredation wrote: wtf is DisplacementMapFilter?

It's a filter that can only be used through AS, it can be applied to BitmapData objects or movie clips.

If you wonder about any more, click the link in my last post in this thread, wait for it to load (takes a while), click ActionScript 2.0 Language reference, click ActionScript classes, and there you have them. Click one for it's reference.


BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS: Variables 2006-08-30 19:48:28 Reply

At 8/30/06 07:47 PM, Rantzien wrote:
At 8/30/06 01:27 PM, Brother_Paranoia wrote: Also, what the hell is an Accordion?
That's the Accordion component's class.

Uh uh. And what is the accordion component?


BBS Signature
Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Variables 2006-08-31 02:24:32 Reply

At 8/30/06 07:48 PM, Brother_Paranoia wrote: Uh uh. And what is the accordion component?

It's a component that does this. It is mostly used for forms.


BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS: Variables 2006-08-31 08:09:14 Reply

At 8/31/06 02:24 AM, Rantzien wrote:
At 8/30/06 07:48 PM, Brother_Paranoia wrote: Uh uh. And what is the accordion component?
It's a component that does this. It is mostly used for forms.

Oh, cool. I see where it gets its name now.

Components are beginning to look pretty nifty.


BBS Signature
mayoarm11
mayoarm11
  • Member since: Jun. 13, 2007
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Variables 2007-11-06 17:59:47 Reply

At 8/9/05 08:58 AM, Rantzien wrote: Global variables are variables that are accessible from everywhere in your Flash file. They can be changed and read from every timeline and scope, as long as you don't have a local or timeline variable with the same name. In that case, the global variable can only be accessed from outside the other variable's scope. When you declare a global variable, you can't use var, which means you can't apply Strict data typing to a global variable. Instead, you use the word _global when declaring. Like this:
_global.variableName = value;
For example:
_global.myPassword = "3ehjTEx56";
A global variable can be declared anywhere, just remember, and this goes for all scopes, that the variable must be declared before you can access it.

can someone please help me with a problem...
I have two movie clips, one of my main character, who walks with the arrow keys, and one of a stamina bar (with the width tweened accordingly). This is the actionscript I have for the movie clips. What I want to know is why doesn't my stamina bar shrink as my character walks.

stamina bar
onClipEvent(load){
this._width=200;
}
onClipEvent(enterFrame){
this._width=_root.stamina*2;
}

main character dude
onClipEvent(load){
_global.stamina = 100;
}
onClipEvent(enterFrame){
if (Key.isDown(Key.LEFT)){
this._x-=speed;
stamina--;
}
if (Key.isDown(Key.RIGHT)){
this._x+=speed;
stamina--;
}
if (Key.isDown(Key.UP)){
this._y-=speed;
stamina--;
}
if (Key.isDown(Key.DOWN)){
this._y+=speed;
stamina--;
}
}

The reason I put this post in this thread and not in the bars tutorial is because I am quite sure the problem is the global variable.


Awesome turret game in the making, featuring 85 uniqe turrets: News post

BBS Signature
neon-dude
neon-dude
  • Member since: Mar. 15, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Variables 2007-12-15 11:20:47 Reply

This may sound like a n00bish question, but once you've made a variable, how do you display it? For example, how would you show a var in a textbox? Please help, I are confused.


"just because idiots are great in number does not mean they're not idiots" - alicetheDroog
The Atheist Army|English Gentleman's Club
Sig censored by: SevenSeize

BBS Signature
under-siege
under-siege
  • Member since: May. 11, 2007
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to AS: Variables 2007-12-15 11:36:39 Reply

Neon dude - you should post another topic and ask that.

Did notice one thing about this - it doesn't mention that variables names can only start with letters and _ (not numbers)