Home | Documentation | Atomic Chat | Github

What would be the correct way to remove a sprite from screen?


#1

I’m still newly looking at Atomic Game Engine and I have run into a snag. I am creating a child to a node when a key is pressed to put a ‘bullet’ sprite on the screen. That I have working. Now I am working on how to handle the removal of the ‘bullet’ sprite from the screen. I thought I could just use remove() but that removes both the parent and the child. I did see Component Cleanup but cannot understand how to get it to function as that post mentioned.

This is what I have tried:
.
.
.
self.update = function(timeStep) {
handleInput(timeStep);
};
.
.
.
//create child and display on screen
if (up){
var newSprite = self.node.createChild(newSprite);
newSprite.position = [pos[0], pos[1], z];
var newSprite = newSprite.createComponent(“StaticSprite2D”);
newSprite.sprite = Atomic.cache.getResource(“Sprite2D”, “Sprites/blue_beam.png”);
}

//this is where I want to remove newSprite by pressing down key
if (down){
self.node.remove(); //which removes both parent and child
}
// variants I have tried are as follows
// self.newSprite.remove();
//self.node.remove(newSprite);
//self.node.newSprite.remove();
//self.node.newSprite.remove(newSprite);
//newSprite.remove();
//self.component.remove();
//etc…
}

Thanks in advance.


#2

So far I did get the functionality or outcome I was looking for in this extremely simple example. I found that self.node.removeAllChildren(); did it for now. I wonder how this will change as I begin to work with multiple sprites?

In case others would like to know my deduction process I looked at the C++ documentation under nodes and for whatever reason that documentation format made it click for me.


#3

You probably want to do something along these lines:

self.newSprite = self.node.createChild(newSprite);
// < ... >
self.newSprite.remove();

You were on the right track, but you forgot to actually save newSprite on the self instance and that is why self.newSprite.remove(); failed.


#5

I have made another stride in this. I found that:
self.newSprite.removeChildren(“Sprite2d”);
and also:
self.newSprite.removeChildren(“StaticSprite2D”);
did remove the child. This seems like the more ‘correct’ way to accomplish this rather than indiscriminately removing all children to a node.

By the way:
self.newSprite = self.node.createChild(newSprite);
does seem necessary.

So for those needing this info in the future the code from my original post becomes:

. // this is where globals (variables) could go
.
.
self.update = function(timeStep) {
handleInput(timeStep);
};
. // this is where input handlers could get set up
.
.
//create child and display on screen
if (up){
var newSprite = self.node.createChild(newSprite);
self.newSprite = self.node.createChild(newSprite);
//newSprite.position = [pos[0], pos[1], z]; //unnecessary if you want your child sprite to be put in the middle of the node parent sprite
var newSprite = newSprite.createComponent(“StaticSprite2D”);
newSprite.sprite = Atomic.cache.getResource(“Sprite2D”, “Sprites/blue_beam.png”);
}

//this is where I want to remove newSprite by pressing down key
if (down){
self.node.removeChildren(“Sprite2D”)
}

}

Thank you rokups for your help.
I look forward to more comments and replies on this post.


#6

You create two children here. I believe you do not need the first line.

Then self.node.removeChildren(“Sprite2D”) should become self.newSprite.remove()

P.S. when posting code snippets wrap them in three backquotes like so:

```js
function foo() { }
```

Result:

function foo() { }

Replace js with cs or cpp or whatever other language name you are posting snippet for.