A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Object Class Initialization

  1. #1
    Member
    Join Date
    Jul 2003
    Posts
    63

    Object Class Initialization

    I'm new at using classes in ActionScript. Basically what I'm trying to do is create an array of objects using a Constructor and attendant methods. I'm using trace to try and return position properties so that I know whether what I am doing is working. Obviously, it is not working, because it is returning "undefined" for each value. Other than that, I'm not getting any errors, and I can't find the glitch.

    The file is attached.
    Attached Files Attached Files
    Last edited by The Public; 11-19-2003 at 04:01 AM.

  2. #2
    Member
    Join Date
    Jul 2003
    Posts
    63
    Move to top of forum.

  3. #3
    Member
    Join Date
    Aug 2003
    Posts
    30
    here yago...

    I renamed your class from "Module" to "myModule" (thinking there may be a reserved word, maybe not...)

    You need to call methods by using the classname first, in your loop:

    myModule[i].getPos(0);
    myModule[i].getPos(1);

    And, you had functions returning 'this.xPosition' which is where you were getting your 'undefined' variables. Flash didn't know that variable existed. I changed it to 'return this._x' and voila!

    There's traced statements in there to show you the result.

    Also, as it stands, you'll need to add more code to set your colors... (create a color object)

    Good luck!
    -Justin
    Attached Files Attached Files

  4. #4
    Member
    Join Date
    Jul 2003
    Posts
    63
    Thank you.

    For some reason, I can't open the file that you reposted. FlashMX says "Unexpected File Format Encountered." Could you maybe post the code directly to the forum. I tried making the changes to my file based on what you described, but it doesn't work.

    Thanks again.

    Joshua
    Last edited by The Public; 11-19-2003 at 11:03 PM.

  5. #5
    Member
    Join Date
    Aug 2003
    Posts
    30
    sorry, I posted it in mx04 format, try this:

    Let me know how it worked.
    Attached Files Attached Files

  6. #6
    Member
    Join Date
    Aug 2003
    Posts
    30
    Here's the actionscript, in case the file didn't work
    code:

    /*Module Object Class Constructor/Methods*/
    function myModule(xPos, yPos, col){
    this._width = 10;
    this._height = 200;
    //initialization
    this.setColor(col);
    this.setPos(xPos, yPos);
    }

    myModule.prototype.setColor = function(newCol){
    this.color = newCol;
    }

    myModule.prototype.setPos = function(newXPos, newYPos){
    this._x = newXPos;
    this._y = newYPos;
    }

    myModule.prototype.getPos = function(axis) {
    if (axis == 0) return this._x;
    //{return this.xPosition;}
    else if (axis == 1) return this._y;
    //{return this.yPosition;}
    }

    /* end Module Object Class constructor/methods*/

    var objMyModule = new Array(10);
    var i=0, count;
    var act = 0xFFFFFF, inact = 0x999999;

    /*initialize array of Modules*/
    for(i=0;i<10;i++)
    {
    objMyModule[i] = new myModule(i*50, 200, inact);
    trace("objMyModule["+i+"].getPos(0): "+objMyModule[i].getPos(0));
    trace("objMyModule["+i+"].getPos(1): "+objMyModule[i].getPos(1));
    trace("");

    }
    /*end initialize Modules*/


  7. #7
    Member
    Join Date
    Jul 2003
    Posts
    63
    Yes, that works. I'm not sure why the corrections that I made didn't work, because I did the same thing. I must have had a small hidden error somewhere.

    Thanks again.

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