A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Accessing the components of a cloned layer

  1. #1

    Accessing the components of a cloned layer

    I have a layer with several text elements. First I do

    var PntInfo = element ("PntLayer");

    to get a pointer to the layer. Then

    PntInfoList = PntInfo.clone();

    to create a new copy of the layer.
    Now I want to change the text in PntName, which is a text element on PntLayer. How do I get a pointer to the specific PntName that is on PntInfoList- the clone I created?

  2. #2
    Quick followup-
    I also have a button on each of these layers. There will be several clones of the layer. I need to know which button the user clicked (was it from clone 2 or clone 3?)

  3. #3
    Senior Member kusco's Avatar
    Join Date
    Nov 2001
    Posts
    681
    In the Features list, the first feature is called "Variable name". You can use it have a child element assigned to a variable of its parents. This allows cloned elements to get handles to their own children.

    You can then access a clone child as ...

    var my_clone = element ("A").clone()
    var clone_child = my_clone.child_variable_name;

    Unfortunately, you cannot use 'this' in a button script to get a handle to the button itself, only to its parent, so you cannot pass the cloned button handle to a root function without first assigning a variable name to the button. Then the button can pass its handle using its variable name.
    Cheers,
    kusco
    (3DFA Support Team)

  4. #4
    Ok, I'm getting closer, but not quite there. What am I missing?
    The attached picture shows the setup of "Layer 2", which is probably where the problem lies.

    var Text2 = element("Layer 2").RoundButton; //this gets a handle to nothing
    Text2.y += 50;

    This gets me the error-
    Null cannot be appended with a value on line 2 Text2.y += 50
    Cannot assign property 'y' to undefined variable on line 2 Text2.y += 50
    Attached Images Attached Images

  5. #5
    Even without cloning, I'm still having trouble accessing the components of any layer. I attached a demo movie. I want to change the text of the small text element on the layer.
    Attached Files Attached Files

  6. #6
    DJ Rico
    Join Date
    Jun 2005
    Location
    Anchorage, Alaska
    Posts
    5

    It Works! (For Me)

    I tried your file on 4.9.7.3 and it works for me.

    I used
    var sText = element('txtChange");
    sText.htmlText = "it works!";

    I used the element button on the script editor and I thought it would give me the FirstLayer.txtChange but it just gave me txtChange.

    Here it is for you to try:
    Attached Files Attached Files
    Last edited by AlaskaBorg; 12-05-2007 at 07:58 AM.

  7. #7
    Thanks AlaskaBorg!

    Okay, now I can access the components of layers. What I really want to do is access the components of cloned layers. So I tried-

    var TheLayer = element ("FirstLayer");
    var SecondLayer = TheLayer.clone();
    SecondLayer.x = 200; //separate the layers so we can see the dif

    var sText = element("txtChange");
    sText.htmlText = "it works!";

    //this part doesn't work
    var SmallText = SecondLayer.txtChange;
    SmallText.htmlText = "it works!";

    The error reads- "'txtChange' is not a property of Layer:'FirstLayer_copy' on line 6 SmallText = SecondLayer.txtChange"

    This changes the text of the original layer, but how do I access the txtChange of SecondLayer?

  8. #8
    DJ Rico
    Join Date
    Jun 2005
    Location
    Anchorage, Alaska
    Posts
    5

    From the 3DFA Help

    I was reading the help file and under the heading: Scrips and elements, the way it describes the clone-child access issue makes sense and it looks like your code is missing the "Variable Name" feature.

    Here's what the help says:

    The 'element' function only allows you to get handles to original elements. It does not allow you to get a handle to any of the children of a cloned element.

    However, you can use the "Variable Name" feature to assign a variable name to the child element. You can then directly access the child as a property of the clone.

    cloned_element = original_element.clone();
    child_of_clone = cloned_element.specified_variable_name;
    I noticed that you didn't have the 'Elements With Properties' button pushed on your txtChange object. After turning that on, your code worked.

    I believe that the steps for success would be to:
    • Make sure that the children have unique names
    • Create the clone
    • Address the children using the "Variable Name"


    I will have to do some testing of my own but after reading and trying it a few times, it seems that the help does not completely align with the reality of coding it and making it work.

    I made a clone and was able to move it but not assign any text to the child elements.

    I get the error: Property 'htmlText' used without first declared in strict mode on line 8 TheChild.htmlText = "Clone Here";

    Here is my code for testing:
    var TheLayer = element ("FirstLayer");

    var sText = element ("txtChange")
    var TheClone = TheLayer.clone();
    var TheChild = TheClone.element ("txtMoreChange");

    var twoChild = link ("root[FirstLayer@0][txtMoreChange@2]");

    TheClone.x = 40;
    TheClone.y = 20;

    TheChild.htmlText = "Clone Here";

    twoChild.htmlText = "<p>The Clone Lives</p>";
    sText.htmlText = "It finally works!";

    I look forward to answers and would like to know if any of the samples show how this is done.

  9. #9
    DJ Rico
    Join Date
    Jun 2005
    Location
    Anchorage, Alaska
    Posts
    5

    Curious Clone Behavior!?

    I have been playing with the clone commands and I seem to be able to move, change color and size of a child but not set it's text!

    I took apart the clone ball sample and was able to duplicate but it does not assign text to clones.

  10. #10
    DJ Rico
    Join Date
    Jun 2005
    Location
    Anchorage, Alaska
    Posts
    5

    Clone Mystery ???

    I took your original Test7 file and replaced the Layers with child movies and the cloning works!

    The other thing I did was to clone the small text field instead of the layer or the movie and that worked for me.

    I was able to move it and replace the text. It appears that the movie moves with the small text field, the point is that the cloning worked.

    I don't know if this helps but I set the Cloning option, set the Variable Name and all is right in my world...

    I don't know why I keep getting the error:
    Property "htmlText" used without first being declared in strict mode on line 29 childMovie.htmlText = "ChildMovie";

    Here is the code:

    var LMovie = element ("lilMovie");
    var CMovie = LMovie.clone();
    var childMovie = CMovie.element ("lilText");
    var mChild = element ("lilText");

    var zChild = mChild.clone();

    childMovie.htmlText = "ChileMovie";

    I'm including my movie file for review.
    Attached Files Attached Files

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