Question

I've just started working with JSFL in Adobe Flash and I'm trying to add a text field to a newly created layer and then change its instance name to "properties".

Here's my code:

fl.outputPanel.clear();

var document = fl.getDocumentDOM();

var timeline = document.getTimeline();

var layers = timeline.layers;

var propertiesLayer = null;

var propertiesTextBox = null;

var layerIndex = 0;

fl.trace("Searching For Properties Layer");

for (var index in layers)
{
    if(layers[index].name == "properties")
    {
        propertiesLayer = layers[index];
        layerIndex = index;
    }
}

if(!propertiesLayer)
{
    fl.trace("Creating Properties Layer");
    timeline.addNewLayer("properties");
    layerIndex = timeline.findLayerIndex("properties");

    fl.trace("Properties Layer Index is " + layerIndex);
    propertiesLayer = layers[layerIndex];

    timeline.setSelectedLayers(layerIndex * 1);

    fl.trace("Creating Properties Text Field");
    document.addNewText({left:0, top:-100, right:100, bottom:0});

    propertiesLayer.frames[0].elements[0].name = "properties";

    fl.trace("Setting Properties Text Field's Instance Name");
    //propertiesTextBox.name = "properties";

    fl.trace("Setting Properties Text Field");
    document.setTextString("test");
}

My output window displays: Searching For Properties Layer Creating Properties Layer Properties Layer Index is 0 Creating Properties Text Field

And I get the following error when testing on a new flash document TypeError:propertiesLayer.frames[0].elements[0] has no properties

I'm at a loss and any help would be very much appreciated.

Was it helpful?

Solution

Modified your code and it works. Following is only a part where I modified the code. Need some more improvements though.

if(!propertiesLayer)
{
      timeline.addNewLayer("properties");
      layerIndex = timeline.findLayerIndex("properties");

      propertiesLayer = layers[layerIndex];

      timeline.setSelectedLayers(0);

      document.addNewText({left:0, top:-100, right:100, bottom:0});

      document.setTextString("test");

      var element = document.selection[0];

      document.selection[0].textType = "dynamic";
      element.name = "properties";   

 }

OTHER TIPS

When calling addNewText() the type of text field created is the last type that was created. So if you created a static field then ran your code it would error out as you have seen because static text fields can't have instance names ( which is set using the "name" property ). Here is a quick fix for your problem:

propertiesLayer.frames[0].elements[0].textType = "dynamic";
propertiesLayer.frames[0].elements[0].name = "properties";

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top