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?
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?)
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.
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
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.
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.
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]");
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");