Skip to main content
Inspiring
January 5, 2023
Answered

jsfl, 'swapElement' requires a selection.

  • January 5, 2023
  • 1 reply
  • 828 views

When selecting a timeline from a library symbol with 'dom.library.getSelectedItems()[0].timeline', 'swapElement' throws the error ''swapElement' requires a selection', even though it has been selected and does exist with fl.trace("element = " + element + " selected = " + element.selected)  = 'element = [object SymbolInstance] selected = true'. 

Any ideas? Here's the code.

var dom = fl.getDocumentDOM();
var tl = dom.getTimeline();

dom.library.selectItem("main") 
var seqTL		= dom.library.getSelectedItems()[0].timeline;	
var seqLayers 		= seqTL[1];
	
var element = seqLayers[0].frames[0].elements[0];
element.selected = true
fl.trace("element = " + element + " selected = " + element.selected) 
fl.getDocumentDOM().swapElement('Symbol 1');

 

    This topic has been closed for replies.
    Correct answer Vladin M. Mitov

    Hi,
    Unfortunately, setting the selected property of an element, that is not in the current timeline, do not affect the selection property of the document. You can try to add to your code the following line:

     

    fl.trace( dom.selection );

     


    and you'll notice that even though the element is selected, the selection property of the document is empty.
    So, you need to place the symbol "main" on the stage, make its timeline a current one and do the symbol swap in that timeline.

     

     

    var dom = fl.getDocumentDOM();
    
    dom.library.addItemToDocument( {x:0, y:0}, "main" );
    
    dom.enterEditMode( "inPlace" );
    	var seqTL = dom.getTimeline();
    	var seqLayers = seqTL[1];
    	var element = seqLayers[0].frames[0].elements[0];
    	element.selected = true;
    	fl.getDocumentDOM().swapElement( "Symbol 1" );
    dom.exitEditMode();
    
    dom.deleteSelection();

     

     

     

    1 reply

    Vladin M. Mitov
    Vladin M. MitovCorrect answer
    Inspiring
    January 5, 2023

    Hi,
    Unfortunately, setting the selected property of an element, that is not in the current timeline, do not affect the selection property of the document. You can try to add to your code the following line:

     

    fl.trace( dom.selection );

     


    and you'll notice that even though the element is selected, the selection property of the document is empty.
    So, you need to place the symbol "main" on the stage, make its timeline a current one and do the symbol swap in that timeline.

     

     

    var dom = fl.getDocumentDOM();
    
    dom.library.addItemToDocument( {x:0, y:0}, "main" );
    
    dom.enterEditMode( "inPlace" );
    	var seqTL = dom.getTimeline();
    	var seqLayers = seqTL[1];
    	var element = seqLayers[0].frames[0].elements[0];
    	element.selected = true;
    	fl.getDocumentDOM().swapElement( "Symbol 1" );
    dom.exitEditMode();
    
    dom.deleteSelection();

     

     

     

    - Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation
    Inspiring
    January 5, 2023

    Thanks Vladin, that works even though it seems like a work-around. Interestingly, you can set the selected property of an element that is not in the current timeline by adding a new element into the same layer and frame as an existing element. Then delete that new element, and suddenly you can access that existing element. Any ideas why this now works?

    var dom = fl.getDocumentDOM();
    var tl = dom.getTimeline();
    
    dom.library.selectItem("main") 
    var seqTL = dom.library.getSelectedItems()[0].timeline;	
    var seqLayers = seqTL[1];
    
    ///////////////////////
    		
    // Select the layer and frames
    seqTL.setSelectedLayers(0)
    seqTL.currentLayer = 0
    
    // Add 'Symbol 1' into layer 1, frame 1 where there is already a symbol, now there are 2.
    
    fl.getDocumentDOM().library.addItemToDocument({x:0, y:0}, 'Symbol 2');
    
    var eles = seqLayers[0].frames[0].elements
    var element = eles[1]
    
    element.selected = true
    
    // Delete this new element, now there's 1 again.
    
    dom.deleteSelection()
    
    // Now you can access the original element
    
    element = eles[0]
    element.selected = true
    
    fl.getDocumentDOM().swapElement('Symbol 1');
    	

     

    Inspiring
    January 5, 2023

    Or also - you can also move the element which allows it to then be swapped. So something is wrong with the swap function.

    // Select the layer and frames
    seqTL.setSelectedLayers(0)
    seqTL.currentLayer = 0
    
    var eles = seqLayers[0].frames[0].elements
    var element = eles[0]
    
    element.selected = true
    
    var origX = element.x
    element.x = 0
    element.x = origX
    fl.getDocumentDOM().swapElement('Symbol 1');