dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: referencing objects via an array

  1. #1
    eLearning Dev Monkey
    Join Date
    Oct 2002
    Location
    Manchester, UK
    Posts
    28

    referencing objects via an array

    hello

    i am trying to create a fake email inbox for an eLearning simulation. the brief is thus:

    the inbox lists emails. emails are added as the user progresses through the game. for each email, its variables are displayed (from, subject, date received).

    i have an array to store the emails in, and an index to keep count:

    Code:
    // Keeps track of number emails so that they can be put into an array
    //and referenced.
    emailCount = 0;
    
    // Array to store emails in.
    emails = new Array();
    i have a constructor to create a new email object:

    Code:
    function email(newID, newFrom, newSubject, newBody, newDate) {
    	ID = newID;
    	emailCount ++;
    	from = newFrom;
    	subject = newSubject;
    	body = newBody;
    	thisDate = newDate;
    	read = false;
    	emails.push(ID);
    }
    i also have a function to display the emails:
    Code:
    displayEmails.onPress = function() {
    	for(i = 0; i < emails.length; i++) {
    		attachMovie("emailListMember", ["emailListMember" + i], i);
    		with(["emailListMember" + i]) {
    			from = _root.emails[i].from;
    			subject = _root.emails[i].subject;
    			thisDate = _root.emails[i].thisDate;
    			ID = _root.emails[i].ID;
    			_x = 30;
    			_y = 30 * i + 10;
    		}
    	}
    }
    after creating a couple of email instances, and trying to display them, i find their variables are not set. i think its because the array values do not point to the email instance itself, and merely holds a string.


    cheers in advance
    www.MultimediaMarketing.Com
    Global Learning - Better Marketing

  2. #2
    Senior Member
    Join Date
    Feb 2001
    Posts
    1,835
    Hi,

    in your constructor function, you need to use 'this', for example:

    this.id = newID;

    and so on. Otherwise those variables won't be saved as properties in the object instance but on the main timeline.

    If you really want to go down the object-oriented path, create an 'Inbox' class, that holds the array of emails and has a method to display the emails.

    Also, using 'with' in that way is expensive, since the reference gets evaluated again and again. You also have square-brackets in strange places. Instead:

    Code:
    var tmpObj;
    var dataObj = _root.emails[i];
    attachMovie("emailListMember","emailList" + i, i);
    tmpObj = this["emailList" + i];
    with(tmpObj)
    {
      from = dataObj.from;
      .... and so on...
    }
    That should sort it out...

    - n.

  3. #3
    eLearning Dev Monkey
    Join Date
    Oct 2002
    Location
    Manchester, UK
    Posts
    28
    cheers,

    i got it sorted. i just used the constructor straight from the array element creation (?!):

    Code:
    function createNewEmail(newFrom,
    			newSubject,
    			newBody,
    			newReceivedDate) {
    	emails[emailCount] = new email();
    	emails[emailCount].from = newFrom;
    	emails[emailCount].subject = newSubject;
    	emails[emailCount].body = newBody;
    	emails[emailCount].receivedDate = newReceivedDate;
    	emails[emailCount].ID = emailCount;
    	emailCount ++;
    }
    i will be putting fully object-oriented as there will be several interactions and all sorts going on, so will have to structure it well.

    thanks again!
    www.MultimediaMarketing.Com
    Global Learning - Better Marketing

  4. #4
    Senior Member
    Join Date
    Feb 2001
    Posts
    1,835
    hi,

    actually, this new approach is quite interesting. It works but in the end what is the point of having a custom object in the first place, if you need to do it all by hand?

    You don't need to change it of course but I would suggest just using 'this.' inside the constructor - that's the standard approach and the actual 'point' of having a constructor, if you see what I mean, and is probably good to get used to for future coding.

    - n.

  5. #5
    eLearning Dev Monkey
    Join Date
    Oct 2002
    Location
    Manchester, UK
    Posts
    28
    cool, thanks for the tips! i am starting to look into how processor hungry my coding is becoming, now that i am writing large programs. any ports of call you can recommend?
    Last edited by vince_mmc; 01-17-2003 at 08:01 AM.
    www.MultimediaMarketing.Com
    Global Learning - Better Marketing

  6. #6
    Senior Member
    Join Date
    Feb 2001
    Posts
    1,835
    hmmm. well, most things you just kind of pick up by reading a lot of people's posts. were-here have quite a good list of links on code optimisation but they seem to be down at the moment (bit out of touch with that site, are they actually still alive?)

    maybe try the macromedia site too, there could be a few articles there.

    sorry I can't help more! - n.

  7. #7
    eLearning Dev Monkey
    Join Date
    Oct 2002
    Location
    Manchester, UK
    Posts
    28
    that's cool, thanks again for your help.
    www.MultimediaMarketing.Com
    Global Learning - Better Marketing

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