Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 2.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 12-07-2004, 05:39 PM   #1
jasonyen
Registered User
 
Join Date: Dec 2004
Posts: 5
Default problem creating mc instance names inside a function

Hi there,

Somewhat of a newbie again here in the sense that I'm getting back into using Flash and AS. Last used MX and am now using MX2004 (my AS skills have always been pretty rudimentary).

My issue regards creating new movieclip instance names inside a function based on an argument passed to that function, and then telling those mc instances to do things. Right now, I can only get those mc instances to do things if the argument I pass to the function is the full name of the mc instance without quotation marks, i.e. not a string. For example, as follows, the function works like I want it to:

function moveBall (movieName) {
movieName._x += 200;
}
moveBall(ball01_mc); // call the above function

On the stage I have the mc instance called ball01_mc, and as a result of calling the function using moveBall, ball01_mc moves 200 pixels to the right.

However, if I have a lot of balls on the stage, I'd prefer to just use the number of the ball instance as the argument in the function call, and then have the function build the instance name using that number. So the function (he so innocently presumes) would then look like:

function moveBall (movieNumber) {
var movieName = "ball" + movieNumber + "_mc";
movieName._x += 200;
}
moveBall("01"); // If I don't use quotes, the value returned is 1 without the leading zero.

Nothing happens, and I get frustrated because I'm up against the wall of my own ignorance. Is this a data type issue? Do I somehow need to convert the string which is stored in the variable movieName into a movieclip datatype?

I'm guessing there is a tutorial that probably addresses my issue directly (even using a ball_mc), but I haven't yet found it. Thanks for any help you can provide.

Sincerely,

Jason

Last edited by jasonyen; 12-07-2004 at 05:42 PM. Reason: to get email notification when a response is posted
jasonyen is offline   Reply With Quote
Old 12-07-2004, 06:22 PM   #2
Xeef
Off-Line
 
Xeef's Avatar
 
Join Date: Aug 2004
Location: Ibiza/Spain language :Hungarian/German/ abit English
Posts: 6,539
Default

hi

try :

PHP Code:
_root[movieName]._x 
_root coud by diferent depends where the BALL is
__________________

test your self -->http://www.actionscript.org/forums/s...86&postcount=8
Xeef is offline   Reply With Quote
Old 12-07-2004, 06:49 PM   #3
jasonyen
Registered User
 
Join Date: Dec 2004
Posts: 5
Default

Ah. That indeed does work. Thank you. What do the squarebrackets do in this case?
jasonyen is offline   Reply With Quote
Old 12-07-2004, 07:05 PM   #4
Xeef
Off-Line
 
Xeef's Avatar
 
Join Date: Aug 2004
Location: Ibiza/Spain language :Hungarian/German/ abit English
Posts: 6,539
Default

Hmmm
good question

MM help say :

ActionScript Code:
[] (array access) Availability Flash Player 4. Usage myArray = [a0, a1,...aN] myArray[i] = value myObject[propertyName] Parameters myArray The name of an array. a0, a1,...aN Elements in an array; any native type or object instance, including nested arrays. i  An integer index greater than or equal to 0. myObject The name of an object. propertyName A string that names a property of the object. Returns Usage 1: A reference to an array. Usage 2: A value from the array; either a native type or an object instance (including an Array instance). Usage 3: A property from the object; either a native type or an object instance (including an Array instance). Description Operator; initializes a new array or multidimensional array with the specified elements (a0, and so on), or accesses elements in an array. The array access operator lets you dynamically set and retrieve instance, variable, and object names. It also lets you access object properties. Usage 1: An array is an object whose properties are called elements, which are each identified by a number called an index. When you create an array, you surround the elements with the array access ([]) operator (or brackets). An array can contain elements of various types. For example, the following array, called employee, has three elements; the first is a number and the second two are strings (inside quotation marks): var employee:Array = [15, "Barbara", "Jay"]; You can nest brackets to simulate multidimensional arrays. You can nest arrays up to 256 levels deep. The following code creates an array called ticTacToe with three elements; each element is also an array with three elements: var ticTacToe:Array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; // Select Debug > List Variables in test mode // to see a list of the array elements. Usage 2: Surround the index of each element with brackets ([]) to access it directly; you can add a new element to an array, or you can change or retrieve the value of an existing element. The first index in an array is always 0, as shown in the following example: var my_array:Array = new Array(); my_array[0] = 15; my_array[1] = "Hello"; my_array[2] = true; You can use brackets ([]) to add a fourth element, as shown in the following example: my_array[3] = "George"; You can use brackets ([]) to access an element in a multidimensional array. The first set of brackets identifies the element in the original array, and the second set identifies the element in the nested array. The following lines of code send the number 6 to the Output panel. var ticTacToe:Array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; trace(ticTacToe[1][2]);// output: 6 Usage 3: You can use the array access ([]) operator instead of the eval() function to dynamically set and retrieve values for movie clip names or any property of an object. The following line of code sends the number 6 to the Output panel. name["mc" + i] = "left_corner"; For more information, see "Operator precedence and associativity" in Using ActionScript in Flash. Example The following example shows two ways to create a new empty Array object; the first line uses brackets ([]): var my_array:Array = []; var my_array:Array = new Array(); The following example creates an array called employee_array and uses the trace() statement to send the elements to the Output panel. In the fourth line, an element in the array is changed, and the fifth line sends the newly modified array to the Output panel: var employee_array = ["Barbara", "George", "Mary"]; trace(employee_array); // output: Barbara,George,Mary employee_array[2] = "Sam"; trace(employee_array); // output: Barbara,George,Sam In the following example, the expression inside the brackets ("piece" + i) is evaluated and the result is used as the name of the variable to be retrieved from the my_mc movie clip. In this example, the variable i must live on the same Timeline as the button. If the variable i is equal to 5, for example, the value of the variable piece5 in the my_mc movie clip is displayed in the Output panel: myBtn_btn.onRelease = function() {   x = my_mc["piece"+i];   trace(x); }; In the following example, the expression inside the brackets is evaluated, and the result is used as the name of the variable to be retrieved from movie clip name_mc: name_mc["A" + i]; If you are familiar with the Flash 4 ActionScript slash syntax, you can use the eval() function to accomplish the same result: eval("name_mc.A" & i); You can use the following ActionScript to loop over all objects in the _root scope, which is useful for debugging: for (i in _root) {   trace(i+": "+_root[i]); } You can also use the array access ([]) operator on the left side of an assignment statement to dynamically set instance, variable, and object names: employee_array[2] = "Sam";
__________________

test your self -->http://www.actionscript.org/forums/s...86&postcount=8
Xeef is offline   Reply With Quote
Old 12-07-2004, 07:29 PM   #5
cancerinform
Senior Member
 
Join Date: Nov 2003
Posts: 859
Default

The square brackets replace eval()
__________________
Website - FLASH XML Applications
cancerinform is offline   Reply With Quote
Old 12-07-2004, 07:48 PM   #6
Xeef
Off-Line
 
Xeef's Avatar
 
Join Date: Aug 2004
Location: Ibiza/Spain language :Hungarian/German/ abit English
Posts: 6,539
Default

@cancerinform
thats a by wague

and it's not true in all cases
__________________

test your self -->http://www.actionscript.org/forums/s...86&postcount=8
Xeef is offline   Reply With Quote
Old 12-07-2004, 08:53 PM   #7
cancerinform
Senior Member
 
Join Date: Nov 2003
Posts: 859
Default

http://www.macromedia.com/support/fl...ionary200.html
__________________
Website - FLASH XML Applications
cancerinform is offline   Reply With Quote
Old 12-07-2004, 09:27 PM   #8
Xeef
Off-Line
 
Xeef's Avatar
 
Join Date: Aug 2004
Location: Ibiza/Spain language :Hungarian/German/ abit English
Posts: 6,539
Default

PHP Code:
0;
= [];
A[X] = "HALLO";
function 
Bla(X) {
    
//let's see how you repleace this whit eval  :)
    
trace(A[X]);
}
Bla(X); 
__________________

test your self -->http://www.actionscript.org/forums/s...86&postcount=8
Xeef is offline   Reply With Quote
Old 12-07-2004, 11:25 PM   #9
cancerinform
Senior Member
 
Join Date: Nov 2003
Posts: 859
Default

That is an array, what you are showing and the array carries a string, which stays a string.
A=[];
is the same as
A=new Array();

Your function also works this way:
X = 0;
A = [];
A[X] = "HALLO";
function Bla() {
trace(A);//this shows it is an array and the brackets are used for that
}
Bla();

The way I understand eval is that you have a string for example, which when used with eval is recognized by the player as a different type of object, if it exists on stage. For example
i=1;
trace(_root["mc"+i]);//gives _level0.mc1
is the same as this:
trace(eval("mc"+i));//gives _level0.mc1
trace("mc"+1);//gives a string: mc1
__________________
Website - FLASH XML Applications
cancerinform is offline   Reply With Quote
Old 12-07-2004, 11:34 PM   #10
jasonyen
Registered User
 
Join Date: Dec 2004
Posts: 5
Default

thanks for the additional info. I'm going to have to study up more on syntax to really understand the why's of this stuff.
jasonyen 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 09:30 PM.

///
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.