-
identifiers in an array
Is it possible to have identifiers stored in an array? In example:
Code:
list;
list[0] = "_root.MC1";
list[1] = "_root.MC2";
list[2] = "_root.MC3";
for(i in list) {
i.play();
}
If computer games affected us, would I not be running into walls popping strange yellow pills and listening to repetitive music?
-
Come on, it can't be that difficult! Is there any way of indexing identifiers then using functions on them.
If computer games affected us, would I not be running into walls popping strange yellow pills and listening to repetitive music?
-
If computer games affected us, would I not be running into walls popping strange yellow pills and listening to repetitive music?
-
Flashkit Veteran
code:
list = new Array();
list.mc1 = "_root.MC1";
list.mc2 = "_root.MC2";
list.mc3 = "_root.MC3";
for(i=1;i<3;i++) {
list["mc"+i].play();
}
-
but then, when I want to say something such as "leftWall" as the mc, then what do I do?
If computer games affected us, would I not be running into walls popping strange yellow pills and listening to repetitive music?
-
Senior Member
For a simpler method, just leave out the quotes:
code:
list = [];
list[0] = _root.MC1;
list[1] = _root.MC2;
list[2] = _root.MC3;
for(i in list) {
i.play();
}
Or, you can do it this way:
code:
list = [_root.MC1, _root.MC2, _root.MC3];
for(i in list) {
i.play();
}
-
thanks, I'll try that. That was actually my first guess, but since I was at a computer without Flash I posted it just to make sure I was correct. (The beauty of having nothing else to do )
If computer games affected us, would I not be running into walls popping strange yellow pills and listening to repetitive music?
-
Flashkit Veteran
I hate you..... I wish I had nothing else to do
-
ok.... DONT MAKE ME GET THE HOSE!
If computer games affected us, would I not be running into walls popping strange yellow pills and listening to repetitive music?
-
This is how I roll...
assuming these ACTIONS are at _root along with
- click_mc
- some_mcA
- some_mcB
- some_mcC
- some_mcD
Code:
_global.base = this; // used to control scope when storing mcs
doSomething_A = function(mc){ trace(mc); } // trace the property passed
doSomething_B = function(mc){ trace("this: " + this + ", mc: " + mc);} // show the scope of the function run and prop passed
doSomething_C = function(mc){ trace("doSomething_C with ::" + mc); } // nothing special
doSomething_D = function(mc){ trace("array id: " + mc.id + ", storing func: " + array[mc.id].func);} // show the id of the prop passed, and reference back to the func in the array
array = [
{mc:base.some_mcA, func:'base.doSomething_A'},
{mc:base.some_mcB, func:'base.doSomething_B'},
{mc:base.some_mcC, func:'base.doSomething_C'},
{mc:base.some_mcD, func:'base.doSomething_D'}
];
click_mc.onPress = function(){
var len = array.length;
for(var i =0; i < len; i++){
mc = eval(array[i].mc); // create a reference to the mc from the array
mc.id = i; // store the id in the mc
mc.func = eval(array[i].func); // store the function if needed
mc.func(mc); // run the stored function and pass the mc
}
}
-
a little more detail...
A year later, I am running across this post again from http://krazydad.com/bestiary/askjim.html. I have a vague idea of what I was thinking with my last reply, but it really has no context to this topic. Knowing what I know now, a few things come to mind about loops and enumerable properties.
@myself
For all the good things I was trying to accomplish, and while this is still overkill for a loop question, a simple mx.utils.Delegate would have been good enough - or there is always the ever clever proxy to retain scope using delegates - http://www.justgooddesign.com/blog/jgdelegate.htm.
Code:
function proxyBEFORE (s:Object, func:Function):Function
{
// SCOPED FUNCTION TO RUN PROXY ARGUMENTS WITH RUNTIME ARGUMENTS APPENDED TO THE END
// dFunc = proxyBEFORE ( this, o, 'Hello' );
// dFunc ( 'World.' ); // Hello World.
var a:Array = arguments.slice(2, arguments.length);
return function ():Void { func.apply(s, a.concat(arguments)); };
}
@joejoe2288
i<3 should be i<4 or i<=3 because you start with 1 and end with 3. Specifying less than 3 means you only loop through 1 and 2.
Also, by using list.mc1 = "_root.MC1"; you aren't adding to the array, but rather setting a property of the Array object. Properties of Objects can be enumerable with a for ( i in obj ) just like Array values but you loose any Array functionality like Array.sortOn().
An Array index is a Number, while a object property is a String that can't start with a Number. ( obj[4] could never exist while array[4] can )
Code:
// create new array
var list:Array = [ 'a', 'b', 'z' ];
// set array properties
list.propA = "first";
list.propZ = "z-second";
list.propB = "third";
//
// ARRAY PROPERTIES
//
for ( var i in list )
{
trace ( 'len: ' + list.length + ' i: ' + i + ' ' + list[i] );
}
// len: 3 i: propB third
// len: 3 i: propZ z-second
// len: 3 i: propA first
// len: 3 i: 2 z
// len: 3 i: 1 b
// len: 3 i: 0 a
//
// ARRAY ELEMENTS
//
var len = list.length;
for ( var i = 0; i < len; i++ )
{
trace ( 'i: ' + i + ' ' + list[i] );
}
// i: 0 a
// i: 1 b
// i: 2 c
A couple of interesting things crop up when you set properties of an Array along with adding Array elements.
First; a 'for loop' shows you all properties and array elements that you've set and groups properties separate from array elements.
Second; a 'for loop' starts the loop from the last item to the first, while a for ( var i=0; i<len; i++ ) starts from the beginning. You could actually get the same result by using a for ( var i = len-1; i>=0; i-- ). This is only important to note if order is important to your loop.
Code:
var startValue:Number = 0;
var increment:Number = .1
var destination:Number = .5;
for ( var num = startValue; num <= destination; num += increment )
{
trace ( num )
}
// 0, .1, .2, .3, .4, .5
var startValue:Number = .3;
var increment:Number = .1
var destination:Number = 0;
for ( var num = startValue; num >= destination; num -= increment )
{
trace ( num )
}
// .3, .2, .1, 0
Third; Elements in arrays display in the order you set them ( array[0] is always at the 0 spot), while a loop through the properties shows their order is determined by the order you originally set them. It's interesting to note whether that is reverse or forward depending on if you set them by { prop:'val' } or obj.prop = 'val' - they are never sorted alphabetically.
Code:
//
// ORDER IN OBJECT PROPERTIES
//
var obj = {
A:'first in set',
B:'second in set',
C:'third in set'
}
obj.D = 'forth value';
obj.E = 'last value';
obj.B = 'second in set reset';
for ( var i in obj ) {
trace ( i + ' ' + obj [i] );
}
// E last value
// D forth value
// A first in set
// B second in set reset
// C third in set
//
// ORDER IN ARRAY ELEMENTS
//
var array = [ ];
array.push ( 5 );
array.push ( 6 );
array.push ( 7 );
array.push ( 8 );
array.push ( 9 );
trace( array ) // 5,6,7,8,9
trace( array[0] ) // 5
trace( array[1] ) // 6
trace( array[2] ) // 7
trace( array[3] ) // 8
trace( array[4] ) // 9
for ( var i in array )
{
trace( 'i: ' + i + ' ' + array[i] );
}
// i: 4 9
// i: 3 8
// i: 2 7
// i: 1 6
// i: 0 5
for ( var i = 0; i < array.length; i++ )
{
trace( 'i: ' + i + ' ' + array[i] );
}
// i: 0 5
// i: 1 6
// i: 2 7
// i: 3 8
// i: 4 9
If you trace i right now you'll notice it is equal to 5.
trace( i ) // 5
Since 5 is the length of the array it no longer meets the conditional,
but it's still a variable in memory even if the loop is no longer running.
If you have a big list of array items and you are doing a loop from 0 to the length of the list, consider declaring the length outside of the loop. Because it doesn't have to calculate the length of the list on each loop;
Code:
var len = list.length;
for ( var i = 0; i < len; i++ )
is faster than,
for ( var i = 0; i < list.length; i++ )
Nothing will every trace in this because objects don't have lengths nor properties that start with numbers.
Code:
//
// OBJECT
//
var obj = {a:1, b:2, c:3}
var len = obj.length;
for ( var i = 0; i < len; i++ ){
trace ( 'i: ' + i + ' ' + obj[i] );
}
To show or hide a property from being seen in a for loop, consider using ASSetPropFlags - http://www.justgooddesign.com/blog/assetpropflags.htm
Code:
_global.ASSetPropFlags( _root,null,0,1); // show all
for ( var i in _root )
{
trace( i + ' ' + _root[i] );
}
// $appPath file:///C|/Programs/Macromedia/Flash%208/
// $version WIN 8,0,22,0
Continue and break are very big parts of using any loop. Continue jumps to the next item in the loop, while break stops the loop altogether;
Code:
var len = 20;
for ( var i = 0; i < len; i++ )
{
if ( i % 2 ) continue; // go to the next item in the loop
if ( i > 10 ) break; // stop all loops
trace( i );
}
// 0
// 2
// 4
// 6
// 8
// 10
_______________________________________
http://www.justgooddesign.com/blog/
_______________________________________
-
Senior Member
Lol, wow... Seriously, just wow. You go back over a year to correct yourself on a post that was made on a topic that has been answered 2 years prior. You're my new hero.
-
yea, just one of those days...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|