I want to swap a bitmap in symbol with another bitmap,and I found the the function

swapElement

This is what I have tried

var elements = item.timeline.layers[0].frames[0].elements;//item is the symbol

for (var j = 0; j < elements.length; j++) {

    var el = elements[j];

    if (el.instanceType == "bitmap") {
        el.selected = true;//this line not work, so I want a way to make the element selected

        dom.swapElement(targetName);//targetName is another bitmap name that I wanted
    }
} 
有帮助吗?

解决方案

Setting selected to true of an element, will only select the element if the element is on screen (and the layer is unlocked and visible). So before selected is set to true library.editItem(item.name) should be called. If the element is in frame x, frame x has to be made the current frame with document.getTimeline().setSelectedFrames(parseInt(x), parseInt(x) + 1, true). (I use parseInt(x) as a cast because of a bug in setSelectedFrames). To be sure the element is the only element that is selected document.selectNone() should be called before setting selected to true.

Every instance has a libraryItem so maybe replacing the libraryItem is easier. The code here can be adapted for a more selective replace.

其他提示

From library? Then following snippet could help you

var libItems = fl.getDocumentDOM().library.items;

for (i = 0; i < libItems.length; i++){
if(libItems[i].itemType == “bitmap”){
//found bitmap :)
}

OK, what about:

fl.selectElement(el); //instead of el.selected = true;

dom.swapElement(targetName);//assuming you have targetName

This works if there is anything (one item) selected that is bitmap it will replace it with library item named "image4":

var dom = fl.getDocumentDOM();
var selection = dom.selection;

if(selection.length == 1 && selection[0].instanceType == "bitmap")
{
    dom.swapElement("image4");
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top