cocos creator dynamically creates cc.Sprite

cc.loader.loadRes

 First, you need to use this function to load the image file. It should be noted here that the image must be relative to the resources folder.

 

 

//Load assets/resource/123.png file here

cc.loader.loadRes('123', cc.SpriteFrame,function(err,spriteFrame){

            //Create a new node, because cc.Sprite is a component that cannot be directly mounted on the node, but can only be added to a component that is a node
            var node = new cc.Node ('myNode')
            //Call the addComponent function of the newly created node, which will return a sprite object
            const sprite=node.addComponent(cc.Sprite)
            //Assign value to sprite's spriteFrame property
            sprite.spriteFrame=spriteFrame

            //Append the new node to the self.node node. self.node is the node that the script mounts

            self.node.addChild(node)


        
       })

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326361209&siteId=291194637