A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: I want a Child to change Parent's Variable

  1. #1
    Junior Member
    Join Date
    Apr 2007
    Posts
    9

    I want a Child to change Parent's Variable

    I understand how scope, passing by reference, and passing a variable to Child/Parent work, but I'm stuck trying to combine the three.

    On the main timeline, I have some variables that are instances of a custom class I made.

    var customClass1:CustomClass = new CustomClass();
    var customClass2:CustomClass = new CustomClass();

    Inside a movie clip I made, i have a variable.

    var customClass:CustomClass;

    Back to the main timeline, I gave the Children (two instances of the movie clip I mentioned) the main timeline's variables:

    mc1.customClass = customClass1;
    mc2.customClass = customClass2;

    Now I want to make it so any changes to mc1.customClass affect customClass1, and any changes to mc2.customClass affect customClass2.

    mc1.customClass = mc2.customClass
    trace(mc1.customClass.data);//displays the data from customClass2 (like I want it to)
    trace(customClass1.data);//displays the old data from customClass1


    Any thoughts? Hopefully it is clear.

  2. #2
    Junior Member
    Join Date
    Apr 2007
    Posts
    9
    I figured out a workaround, but I'd still like to know how to do this whole child/parent reference variable thing.

    My workaround was to call a function that updates the main timeline's variables whenever I change the child variables, i.e.

    function updateCustomClasses(){
    customClass1 = mc1.customClass;
    customClass2 = mc2.customClass;
    }

    But like I said, this is only a workaround and I have a feeling that I'm doubling the amount of memory being used by my customClasses since they are all independent instances (instead of simple pointers to the same memory location, like I would want).

  3. #3
    Junior Member
    Join Date
    Apr 2007
    Posts
    9
    Ha! I figured it out. I forgot that the assignment operator does stuff by reference, even when I want a hard copy.
    Code:
    //I wanted to do a soft copy
    mc1.customClass = customClass1;
    mc2.customClass = customClass2;
    
    //I wanted to do a hard copy, but forgot (somehow) that it will still do a soft copy
    mc1.customClass = mc2.customClass
    So the solution was to make a function inside of CustomClass that will do hard copies, example:
    Code:
    //inside main time line:
    mc1.customClass.HardCopy(customClass2);
    //inside the class:
    public function HardCopy(c:CustomClass):void{
    this.data1 = c.data1;
    this.data2 = c.data2;
    //and so on...
    }

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