Skip to main content
Inspiring
May 24, 2022
Answered

Add and remove .png from project library to stage.

  • May 24, 2022
  • 1 reply
  • 303 views

Hi,

 

I have a few .png files in my project library. I'd like to add and remove them based on some other user input using JS. These are my functions.

function addMoleculeImage(name) {
	let file_extension = ".png";
	let temp_name = name.concat(file_extension).toString();
	stage.addChild(temp_name);
}

function removeMoleculeImage(name) {
	let file_extension = ".png";
	let temp_name = name.concat(file_extension).toString();
	stage.removeChild(temp_name);
}

 

I get the following error in the console:

Uncaught TypeError: Cannot create property 'parent' on string 'acetate.png'

 

Any ideas why this is happening? Am I misunderstanding how to call something from my project library (I'm guessing so!)? Thanks in advance for your help.

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

     

    You're trying to add a string to the stage. You can only add display objects to the display list.

     

    Also, you need the lib object to spawn Library instances at runtime and bracket notation to pick a symbol class dynamically . For example:

    var root = this;
    var molecule;
    var moleculeLinkage = "YourBitmapLinkage"; // the linkage string is set in the Library in the Linkage column
    
    function addMoleculeImage(linkage)
    {
    	if (molecule)
    		removeMoleculeImage(molecule);
    	
    	molecule = new lib[linkage](); // e.g.: new lib.YourBitmapLinkage();
    	root.addChild(molecule);
    }
    
    function removeMoleculeImage(instance)
    {
    	if (instance && instance.stage)
    		instance.parent.removeChild(instance);
    }
    
    root.addButton.on("click", function(){ addMoleculeImage(moleculeLinkage); });
    root.removeButton.on("click", function(){ removeMoleculeImage(molecule); });

     

    I hope it helps.

     

    Regards,

    JC

    1 reply

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    May 24, 2022

    Hi.

     

    You're trying to add a string to the stage. You can only add display objects to the display list.

     

    Also, you need the lib object to spawn Library instances at runtime and bracket notation to pick a symbol class dynamically . For example:

    var root = this;
    var molecule;
    var moleculeLinkage = "YourBitmapLinkage"; // the linkage string is set in the Library in the Linkage column
    
    function addMoleculeImage(linkage)
    {
    	if (molecule)
    		removeMoleculeImage(molecule);
    	
    	molecule = new lib[linkage](); // e.g.: new lib.YourBitmapLinkage();
    	root.addChild(molecule);
    }
    
    function removeMoleculeImage(instance)
    {
    	if (instance && instance.stage)
    		instance.parent.removeChild(instance);
    }
    
    root.addButton.on("click", function(){ addMoleculeImage(moleculeLinkage); });
    root.removeButton.on("click", function(){ removeMoleculeImage(molecule); });

     

    I hope it helps.

     

    Regards,

    JC

    Inspiring
    May 24, 2022

    It does a ton! I totally misread/misunderstood the docs. You also cleared up what the linkage string does in the library window pane. Thanks so much!