A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Array question

  1. #1
    Senior Member TheLostGuru's Avatar
    Join Date
    Aug 2004
    Location
    I live on this webpage...
    Posts
    784

    Array question

    I have a dynamic array. This is my code thus far.
    Code:
    add_student = function () {
    	var gender = _root.gender_text.text;
    	var Name = _root.name_text.text;
    	if(gender == "Male") {
    		var length = _root.boys.length;
    		_root.boys[length] = (Name);
                    //Is this not possible?
    		_root.boys[length][0] = "test";
    		_root.boys[length][1] = "test2";
                    //traces undefined
    		trace(_root.boys[length][1]);
    		
    	}
    
    }
    I am trying to hurry and do this for a school project, but can't get the nested arrays to work. Anybody know what I am doing wrong?
    "If I have seen further it is by standing on the shoulders of giants." Isaac Newton
    ------------------------------------------------------------------------------

  2. #2
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    try this:
    _root.boys[length][0] = (Name);
    _root.boys[length][1] = "test";
    _root.boys[length][2] = "test2";
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  3. #3
    Senior Member TheLostGuru's Avatar
    Join Date
    Aug 2004
    Location
    I live on this webpage...
    Posts
    784
    No that did not work, although I realize one of my mistakes.
    What I want is to create a list of names.
    Each of those names has proporties though. So for instance, I want this to happen.
    array = [
    ["Joe", 1, 4, 3];
    ["Jim", 3, 2, 1]
    ]
    Code change
    Code:
    add_student = function () {
    	var gender = _root.gender_text.text;
    	var Name = _root.name_text.text;
    	if(gender == "Male") {
    		var length = _root.boys.length;
    		_root.boys[length][0] = (Name);
    		_root.boys[length][1] = _root.choice_1.text;
    		_root.boys[length][2] = _root.choice_2.text;
    		trace(_root.boys[length][0]);
    	}
    }
    "If I have seen further it is by standing on the shoulders of giants." Isaac Newton
    ------------------------------------------------------------------------------

  4. #4
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    Try:

    add_student = function () {
    var gender = _root.gender_text.text;
    var Name = _root.name_text.text;
    if(gender == "Male") {
    _root.boys.push([Name, _root.choice_1.text;, _root.choice_2.text;]);
    trace(_root.boys[_root.boys.length-1]);
    }
    }
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  5. #5
    Senior Member
    Join Date
    May 2003
    Location
    Austria
    Posts
    146
    Did you init the array...

    Code:
    add_student = function () {
    	var gender = _root.gender_text.text;
    	var Name = _root.name_text.text;
    	if(gender == "Male") {
    		var length = _root.boys.length;
                      _root.boys[length] = [];
    		_root.boys[length][0] = "NAME";
    		_root.boys[length][1] = _root.choice_1.text;
    		_root.boys[length][2] = _root.choice_2.text;
    		trace(_root.boys[length][0]);
    	}
    }

  6. #6
    trashtalkthedollar.com
    Join Date
    Jan 2007
    Location
    Fairfax, VA
    Posts
    188
    try something like this

    Code:
    if(gender == "Male"){
        _root.boys.push(new Array(Name, _root.choice_1.text, _root.choice_2.text));
    }

    Quote Originally Posted by Viza
    Use all of renders ideas, and the game could be kickass

  7. #7
    Senior Member TheLostGuru's Avatar
    Join Date
    Aug 2004
    Location
    I live on this webpage...
    Posts
    784
    Thanks for the help. Kortex's method worked. Interestingly enough this works.
    Code:
    _root.boys[length].push([Name, _root.choice_1.text]);
    _root.boys[length][2] = _root.choice_2.text;
    .
    Also, andreaskrenmair's way worked. This way makes the most sense. Johny, I didn't check out your method, but it looks like it would also work. Thanks again, for the help. The project is underway.
    "If I have seen further it is by standing on the shoulders of giants." Isaac Newton
    ------------------------------------------------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center