Variables Scope, _root, _parent, this in SWiSH MAX Script - SWiSHMax Tutorials
 
  Buy Ebooks, Video Games, DVDs, Electronics and more !
Home | Help Forums | Web Design Tutorials | Free Swishmax Downloads Make Money Online Blog . Free Image and Photo Sharing and Hosting

 

 

Company

Home

Nework Details

Tutorials

SWiSH 2

SWiSHMax

Flash MX

PHP & MySQL

HTML / JScript

Adobe Photoshop

Corel Draw

Gimp

Miscellaneous

Downloads

Templates

Plugins

Fonts

Wallpapers

Free Images

Scripts and Codes

Products

E-Books

SWiSH BB

SWiSH Templates

SWiSH-DB Newsletter
Subscribe to our newsletter : GO
  Partners / Affiliates

   Free image hosting
   Web Design Pakistan
   Swish Templates
   SWiSH Climax
   Talkfreelance Community
   Template Desire
   Lite boards
   Script Sector
   GimpTalk
   13dots
   Idea Designs
   PHP Cafe
   Sposatoettore
   Free swishmax templates
   Swishzone

Manage your site with Flax Article Content Management System
Variables Scope, _root, _parent, this in SWiSH MAX Script
Category : SWiSHMax | Level : Intermediate | Language : English
Ask Question | Read Comments | Post tutorial | Previous | Next

Web www.swish-db.com

In SWiSH MAX and Flash MX, the variables always have a path, where
they are defined/creaetd. And they visible (by other scripts in movie)
in that path by default.

For example if we define a variable as x=15; on first
frame of the movie (in main timeline), all the scripts (either on frames
or even buttons' action), will be using variable 'x' directly.

CODE


e.g.

x=x+10;

or

x+=10;



Note : Read this part more carefully.

1. Now the problem is "how to use variable 'x' in sprite
which is created within the main timeline?".
The answer is very simple! Just use path of the variable 'x' to access
it as :

CODE


_root.x=25;



Why did we attach '_root.' with variable name?
Because the variable 'x' was defined in main timeline (root of movie).


2. If you have defined a variable 'y' in the timeline of
a sprite 'myspr' and you want to use variable 'y' in main timeline script
(either on frame or on button's action);
It is so simple;
CODE


use

[color=blue]myspr.y=135;[/color]



The path to variable is actaully '_root.mysprite.y' in current level (_level0.mysprite.y).
[quote]
Note : if you want to learn about levels please [CLICK HERE
[/quote]

We referred only 'myspr.y' because our script (accessing the variable) was
in root, so there is no need to add compelte path.

In case, we have another sprite 'myspr2', and from within that we want to
access variable 'y' previously defined in sprite 'myspr';

WHAT WILL WE DO ?

We will use absolute path of variable 'y' which becomes
CODE


_root.myspr.y=786;



or



_parent.myspr.y=786;



WHAT IS '_parent'?
_parent is used to reffer the any object or variable created in Parent Path
to current Path (either _root or sub sprite).

Concept would be more clear by below image
[img]http://www.swish-db.com/Board/attach/tut4i1.gif[/img]

For example you have a sprite in main timeline named 'pspr' which contains
further one sprite in it named 'cspr'.
Now you have defined a variable in timeline of 'pspr' as
CODE


onframe(1) {

var_name = "the value is swish-db";

}



and you wish to access it in 'cspr', HOW TO DO THAT ?
Whenever we need to reffer any variable or object in 'pspr' from 'cspr'
(in timeline of 'cspr'), we will be using
CODE


_parent.var_name = "now the value is db-board";




3. Uses of 'this'

'this' reffers to current sprite, as compairing to _parnet and _root, which
reffer to some absolute path in movie.
It is especially used when we need to load or send variables from
out-resources (may be a PHP/ASP script) to SwiSH MAX.

CODE


e.g.1.



this.loadVariables("script.asp",'GET');



// To send variables of current path to 'script.asp' via GET method.






CODE


e.g.2.



this.myvar = "this is my new value";



// Assign a text/string value to variable in current path!





Using paths with variables or objects is a good programming approach,
which prevents/eleminates the chances ambiguity or tow/more variables
of same name defined in different paths.


© 2002-2005 Flaxweb Network | Article Manager | Gimp Tutorials Library | RAD C++ Library | Free C/C++ Sourcecode |
Sites of interest : Web Design Blog | Swish Templates | Swishmax Ebook | Photoshop Templates | Gimp Tutorials | Text Forum | Make Money Online