Ok guys and gals,
I'm working with an amazing website of my client's that is written entirely in actionscript 3.0 with xml references. The previous designer set up a mind-warping (for me) set of functions that is totally new to me, but probably normal practice for those familiar with c-based programming. anyhow, there is a sub navigation menu that populates based on this array defenition... so i am facing a problem:
subnavData = new Array({"title":"OVERVIEW", "func":this.changeSlide, "param":this.showServices, "hasChild":true},
{"title":"CREATIVE DIRECTION", "func":this.changeSlide, "param":this.showBranding, "childOf":0},
{"title":"SOCIAL INTERACTIVE", "func":this.changeSlide, "param":this.showOnline, "childOf":0},
{"title":"LIVE EVENTS", "func":this.changeSlide, "param开发者_运维问答":this.showLiveEvents, "childOf":0},
{"title":"CONTENT STRATEGY", "func":this.changeSlide, "param":this.showPerformance, "childOf":0},
{"title":"PROCESS", "func":this.changeSlide, "param":this.showProcess},
{"title":"CASE STUDIES", "func":this.changeSlide, "param":this.showCaseStudies},
{"title":"CLIENTS", "func":this.changeSlide, "param":this.showClients}
);
the menu item on top has children, hence making it number 0 in the array... now the only way i have gotten this to work so far has been to set the "childOf" to '0', that is the string '0', not the value 0. i am normally a php programmer, so i have experienced particular trouble with the inherent tendency for references to the 0 spot not to register from other functions. so, this is primarily what i believe is the issue here, as the string declaration of '0' is not working for an iterative process in another function, as it is expecting a number and since 0 does not work for me, the menu won't display if i set it as that... see?
so what i am wondering is how i might start the ordering from 1 instead of 0 for this array. i don't know how this is done when each instance in the array is contained in braces ( { } ). in php, i would do something like this (hypothetically, if braced values were accepted by php):
subnavData = array(1 => {"title":"OVERVIEW", "func":this.changeSlide, "param":this.showServices, "hasChild":true}, etc...
);
but when trying this in flash:
subnavData = new Array(1 => {"title":"OVERVIEW", "func":this.changeSlide, "param":this.showServices, "hasChild":true}, etc...
);
it yields nothing. please help. thanks!
although the syntax of your initial array is a little funky (convention is to not use string literals for field names. for example { title: 'mytitle'}
, not {'title': 'mytitle'}
), you do have a valid array of objects.
as mentioned before, in Actionscript, an Array
cannot have custom keys; Array elements keys are just integers that point to the desired slot in the array. An Object
is the data type that lets you use custom property names like obj.myPropName
or obj.title
.
DON'T go skewing the array positioning, you'll only confuse the next developer to touch the code! :)
From your snippet, I am guessing the the childOf
property in your objects is supposed to reference an array index. If that is so, I bet you are running into issues checking for a valid childOf
value. If you are iterating through the array checking each object for a valid childOf
property like so:
for(var i:int = 0, l:int = subnavData.length; i < l; i++ )
{
if( subnavData[i].childOf )
{
trace(i + ' is child of ' + subnavData[i].childOf)
}
}
^traces nothing
then you will not get any matches. But clearly you have several entries with an childOf = 0
. The boolean if(subnavData[i].childOf)
check will fail if the value does not exist on the object OR if it is set to 0
( 0 == false
). you need to make a distinction between 0
and undefined
. By changing your check statement to: if( subnavData[i].childOf != undefined )
you can make that distinction.
ie)
for(var i:int = 0, l:int = subnavData.length; i < l; i++ )
{
if( subnavData[i].childOf != undefined )
{
trace(i + ' is child of ' + subnavData[i].childOf)
}
}
^ traces
1 is child of 0
2 is child of 0
3 is child of 0
4 is child of 0
I am taking a stab at what you are trying to accomplish, but think I see what might be tripping you up. Hopefully it helps :)
I am not entirely sure what you want to do here.
But if you want to create an array that you can start counting from 1, you could make the 0 element be a blank array.
subnavData = new Array({}, {"title":"OVERVIEW", "func":this.changeSlide, "param":this.showServices, "hasChild":true}, etc... );
But if you are just having trouble with accessing the elements in the multidimensional array, the standard way to do in in AS3 is:
something = subnavData[0][0];
someOtherThing = subnavData[0][1];
you could also load the array like this, skipping 0:
var subnavData:Array = new Array();
subnavData[1] = {"title":"OVERVIEW", "func":this.changeSlide, "param":this.showServices, "hasChild":true};
subnavData[2] = {"title":"CREATIVE DIRECTION", "func":this.changeSlide, "param":this.showBranding, "childOf":0}
subnavData[3] = etc...
And by the way in flash {} creates an object, not an array. So you are making a 1 dimensional array with objects for values. So to access that data it would look like this:
subnavData[1].title;
// or you can do this
subnavData[1]['title']
those braces within the array represent a new Object:
subnavData = new Array({"title":"OVERVIEW", "func":this.changeSlide});
could also be written like this (though usually wouldn't be since it's verbose):
var navObject:Object = new Object();
navObject["title"] = "OVERVIEW";
navObject["func"] = this.changeSlide;
subnavData = new Array(navObject);
it's actually more common to write object keys without quotes (though in some situations it can be useful or is required):
subnavData = new Array({title:"OVERVIEW", func:this.changeSlide});
or less terse with the dot operator:
var navObject:Object = new Object();
navObject.title = "OVERVIEW";
navObject.func = this.changeSlide;
subnavData = new Array(navObject);
The Object class is the base class in AS3 , in some ways , similar to stdClass in PHP. You can create a new Object in two ways.
var object:Object = new Object();
var object:Object = {};
You can assign properties dynamically, like this:
object.title = "OVERVIEW";
// or this
object = {title: "OVERVIEW" , ...}; // as in your example
//you could also use the Array notation
//but it is believed to be a lot slower
object["title"] = "OVERVIEW";
You don't really need to mess with the Array, but you can easily change the childOf property if you're not comfortable with the 0 value. In your case , 0 is an integer, so when you iterate thru the Objects properties, the following should work:
for each( var obj:Object in subnavData )
{
if( obj.childOf == 0 )
//do whatever
}
But if you change the childOf property to "OVERVIEW" for instance, you could do this:
for each( var obj:Object in subnavData )
{
if( obj.childOf == "OVERVIEW" )
//do whatever
}
Practically change the childOf property to whatever you're comfortable with (provided of course that it's not being used anywhere else!!! the same goes for the Array indexing by the way) , although using 0 as an integer should work here. Can you post an example of the code you use when you iterate thru the Array?
精彩评论