PDA

View Full Version : variables



KingKong
12-14-2004, 03:56 PM
how do you use variables in flash and also what is the variable for an objects location? For example: treelocation=20,30 where treelocation is the variable for the location of the tree and it being equal to 20,30 represents where it is.

NTD
12-14-2004, 05:41 PM
Hi,

Assigning a variable name and value is simply....

variableName = someValue;

You can use arrays and objects to store lists of information, but for simplicity, assigning a property value to a variable could look something like this.......


myMovieClipX=_root.mcName._x;
myMovieClipY=_root.mcName._y;
_root.mcName.onPress=function(){
startDrag(this);
trace(myMovieClipX);
}

KingKong
12-21-2004, 08:03 PM
What do I replace what you wrote with variable names and values?

NTD
12-21-2004, 08:49 PM
Hi,

Yes, just replace the 'mcName' with the name of your mc and that code will show you the corresponding _x and _y coordinates of the movieclip. The variable being defined is....

myMovieClipX....... given the value '_root.mcName._x'

and

myMovieClipY......... given the value '_root.mcName._y'

KingKong
12-22-2004, 07:52 PM
Can you give me an example of that code with numbers and actual and names? thanks.

NTD
12-22-2004, 09:33 PM
Hi,

The demo code should be pretty easy to understand the way it is. Try creating a movieclip named "box" and paste this onto the first frame of the test movie.....


_root.box.onPress = function() {
startDrag(this);
this.onEnterFrame = function() {
myMovieClipX = _root.box._x;
myMovieClipY = _root.box._y;
trace("My box x position is " + myMovieClipX);
trace("My box y position is " + myMovieClipY);
};
};
_root.box.onRelease = function() {
stopDrag();
};


Assigning values to any variable depends on the project. The demo here uses movieclip properties _x and _y, but variables can contain any data that you need to reference later....

myMoneyVariable = 0;
_root.onMouseDown = function(){
myMoneyVariable+=10;
trace(myMoneyVariable)
}

KingKong
12-23-2004, 05:19 PM
If myMovieClipX and myMovieClipY = the location of myMovieClip than shouldn't there be a number somewhere after the = sign?

KingKong
12-23-2004, 06:06 PM
what's supposed to happen. what was the code supposed to do because I don't think it worked.

NTD
12-24-2004, 02:35 AM
The code works correctly. It displays the _x and _y coordinates in the output window.