• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

jsfl, 'swapElement' requires a selection.

Explorer ,
Jan 04, 2023 Jan 04, 2023

Copy link to clipboard

Copied

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');

 

Views

266

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Jan 05, 2023 Jan 05, 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();
...

Votes

Translate

Translate
Engaged ,
Jan 05, 2023 Jan 05, 2023

Copy link to clipboard

Copied

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 1998
Member of Flanimate Power Tools team - extensions for character animation

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 05, 2023 Jan 05, 2023

Copy link to clipboard

Copied

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');
	

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 05, 2023 Jan 05, 2023

Copy link to clipboard

Copied

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');




 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 05, 2023 Jan 05, 2023

Copy link to clipboard

Copied

LATEST

On further investigation, we didn't manage to set the selected property of an element that is not in the current timeline - our tests were being run inside the 'main' symbol. So disregard these results. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines