A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Can you rename an instance???

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    16

    Can you rename an instance???

    I have a movieclip instance named tooth4 which is a white square. I've written the following function to replace it with the color square I've selected from a list below it. What I would like to know is if there is a way to rename the new instance back to tooth4 so that I can repeat this function as many times as I would like and have it replace the square with the color I choose everytime. I've finally got this program done if renaming an instance is possible!

    function tth4(e:MouseEvent):void {
    text_tooth.text ="4";
    removeChild(tooth4);
    var square = new bracketColor();
    addChild(square);
    square.x = 20.4;
    square.y = 32.4;
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes, easily. Just change the name property. But your code doesn't use the name property, it uses a tooth4 variable.

    What you asked for (won't work):
    Code:
    function tth4(e:MouseEvent):void {
      text_tooth.text ="4";
      removeChild(tooth4);
      var square = new bracketColor();
      addChild(square);
      square.x = 20.4;
      square.y = 32.4;
      square.name = "tooth4";
    }
    What you meant to ask for (will work):
    Code:
    function tth4(e:MouseEvent):void {
      text_tooth.text ="4";
      removeChild(tooth4);
      var square = new bracketColor();
      addChild(square);
      square.x = 20.4;
      square.y = 32.4;
      tooth4 = square;
    }

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    16

    Still doesn't work

    I tried both of those and it still didn't work. Perhaps it would help if I posted the whole thing. I have shortened it only by the amount of colors (3 instead of the 14 I have) and the number of teeth (1 instead of the 20 I have) but this should give you the idea. Basically I start out ith an instance of the movieclip named White with an instance name of tooth4. The only way I can figure out to replace it is removing it and placing the new one. The only way I can figure to place the new one is giving it a new variable which I'm assuming is what instance name it gives it??? It works great if I only pick a color to replace it with once, but if I want to pick a new color and replace it it no longer works. Any ideas? Here's the code:



    var bracketColor:Class = White;

    black.addEventListener(MouseEvent.CLICK, onBlack);
    blue.addEventListener(MouseEvent.CLICK, onBlue);
    white.addEventListener(MouseEvent.CLICK, onWhite);

    tooth4.addEventListener(MouseEvent.CLICK, tth4);


    function onBlack(e:MouseEvent):void {
    bracketColor = Black;
    }
    function onBlue(e:MouseEvent):void {
    bracketColor = Blue;
    }

    function onWhite(e:MouseEvent):void {
    bracketColor = White;
    }


    function tth4(e:MouseEvent):void {
    removeChild(tooth4);
    var square = new bracketColor();
    addChild(square);
    square.x = 20.4;
    square.y = 32.4;
    }

  4. #4
    Junior Member
    Join Date
    Jun 2009
    Location
    London
    Posts
    12
    instance names and variable names (references) are two completely separate things.

    A variable cares no more about the "name" property of an object than it does about its "color" property, and variable name and instance name are not synonymous.

    Code:
    var x:Number = 4.3; // does this give the number a name of x? (tip: Numbers don't have names!)
    var m:MovieClip = new MovieClip(); // or this?
    m.name = 'LookAtMeMa'; // what about this?
    Finally, you can't rename items that have been manually place on the stage in the IDE, only items that have been added by code.

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    16

    naming instances when placing them?

    Thanks for the help. So then after I've removed the original instance and place the new one, is there a way to name that instance the same thing as the old one when placing it with ActionScript?

  6. #6
    Junior Member
    Join Date
    Jun 2009
    Location
    London
    Posts
    12
    Why do you need to rename it?

    Just store a timeline or class-level reference (outside the event handler) to the current object that you can always access:

    Code:
    currentTooth = getChildByName('whateverNameYouWant'); // or new YourCustomObject(), whatever...
    currentTooth.x = 100;

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