How to delete a sprite from the scene

I tried using this.removeChild(this.circleSet); where this is referring to my Layer, but the sprite image still appears on the screen.

I never used javascript, but to remove sprites in C++ (therefore, I think, in js too) you must call the method removeFromParent() from the Sprite object you want to remove.

1 Like

I think you might need to add the true parameter to your call:

this.removeChild(this.circleSet, true);

removeFromParent() is not a function in the javascript version

EDIT: Oh actually, it just wasn’t working circleSet because circleSet was a class I made by extending Class and not Node and removeFromParent only works on Nodes.
var CircleSetSprite = cc.Class.extend({
So I just made another function inside my CircleSetSprite class that goes through removes the sprites inside my circleSet.
As a note, my CircleSetSprite class just creates a collection of smaller circcle sprites. Not sure if extending Class was the best option.
But removing them works now.

Thanks!

Adding true didn’t work

Hello,

Am using cocos2d-JS Version 3.7.

this.removeChild(this.circleSet);

This code will works fine for me.

Can you Confirm once this.circleSet is added to Layer.

If the image is added to another layer and you are trying to remove from this layer then image will still appear. Image won’t delete.

removeFromParent() function is present on javascript version.

this.circleSet.removeFromParent();

You can delete of various forms. Before you need create a layer correctly.

var x;
var y;
var size;
var sprite;
var layer;

var gameScene = cc.Scene.extend({
     onEnter:function () {
     this._super();  
     var gameLayer = new game();
     gameLayer.init();
     this.addChild(gameLayer);
     }
});

var game = cc.Layer.extend({
     init:function () {
     this._super();

     size = cc.winSize; 
         x = size.width / 2;
         y = size.height / 2; 

     layer = cc.LayerColor.create(new cc.Color(0,0,0,250), 960, 640); // R+G+B+Opacity+X+Y
     this.addChild(layer); // add layer to game

     var sprite = cc.Sprite.create("res/HelloWorld.png");
     sprite.setTag(1);
     sprite.setPosition(x,y);
     layer.addChild(sprite,0);  // add sprite to layer  

     setTimeout(function(){
         //layer.removeChild(sprite); // remove sprite of layer
         //layer.removeChildByTag(1); // remove sprite by tag
         //layer.removeAllChildren(); // remove all children
         //layer.removeFromParent(); // remove from parent
         }, 3000); // after 3 seconds
     }
});

Update now here
http://cocos2d-js.wikidot.com/sprites

2 Likes

Hi all I am adding sprite dynamicaly where I have assigned tag to particular sprite and i wan to remove this sprite form another layer on specific condition ,can you help me out here ??

@vishalp_tnex,

Can you access Parent Layer,

then you can do like:

var sprite = parentLayer.getChildByTag(TAG_SPRITE5);
** parentLayer.removeChild(sprite, false);**

Hi I am using const texture = cc.textureCache.addImage(getResourceURL(url)); to create frame animation but its giving unwanted flash effect when frame goes small in size.

is there any method so that i can clear this.??