greencoder Posted March 24, 2018 Share Posted March 24, 2018 Hello, Sorry for the complete noob query, completely new to javascript and HTML5 programming. Here my bullet class which gets dynamically when I need a bullet and it setup at init to tween till the target location and destruct at the target location if not collided with anything on the way. But I seem to hit a blocker where I can't self destruct this object. It might be that I am not aware of the syntax or something, please help! game.createClass('gBullet', { init: function(aStage, aStartPos, aGCWidth, aGCHeight) { this.mSprite = new game.Sprite('Bullet_Tex.png'); this.mSprite.addTo(aStage); this.mSprite.scale.set(10,20); this.mSprite.position.set(aStartPos.x + aGCWidth/2 - this.mSprite.width/2 ,aStartPos.y + aGCHeight/2 - this.mSprite.height/2); this.mTween = new game.Tween(this.mSprite.position); this.mTargetPosition = {x: aStartPos.x + aGCWidth/2 - this.mSprite.width/2, y: -50}; this.mTween.to(this.mTargetPosition, 1000); //this.mTween.onComplete(this.remove()); this.mTween.start(); }, }); Quote Link to comment Share on other sites More sharing options...
enpu Posted March 25, 2018 Share Posted March 25, 2018 game.createClass('gBullet', { init: function(aStage, aStartPos, aGCWidth, aGCHeight) { this.mSprite = new game.Sprite('Bullet_Tex.png'); this.mSprite.addTo(aStage); this.mSprite.scale.set(10,20); this.mSprite.position.set(aStartPos.x + aGCWidth/2 - this.mSprite.width/2 ,aStartPos.y + aGCHeight/2 - this.mSprite.height/2); this.mTween = new game.Tween(this.mSprite.position); this.mTargetPosition = {x: aStartPos.x + aGCWidth/2 - this.mSprite.width/2, y: -50}; this.mTween.to(this.mTargetPosition, 1000); this.mTween.onComplete(this.remove.bind(this)); this.mTween.start(); }, remove: function() { this.mSprite.remove(); } }); This would remove the sprite from it's parent once the tween is complete (so the sprite won't get rendered anymore). greencoder 1 Quote Link to comment Share on other sites More sharing options...
greencoder Posted March 25, 2018 Author Share Posted March 25, 2018 Thanks a lot for answering, that definitely works! Noob question again, so if there are more variables like this.mTargetPosition, you don't have to release that memory? Quote Link to comment Share on other sites More sharing options...
enpu Posted March 25, 2018 Share Posted March 25, 2018 Correct, no need for that. Quote Link to comment Share on other sites More sharing options...
greencoder Posted March 25, 2018 Author Share Posted March 25, 2018 Cool. Thanks! Quote Link to comment Share on other sites More sharing options...
greencoder Posted March 28, 2018 Author Share Posted March 28, 2018 Even though it worked, I am still curious about the logic that worked out here : this.mTween.onComplete(this.remove.bind(this)); I understand this.mTween.onComplete part, what I am confused is this.remove.bind(this), what is actually happening here? Oh and if I try to have an update function to this class, on debug, the count of update keeps on increasing the more bullets I fire, so does that mean it's not fully destroyed? Quote Link to comment Share on other sites More sharing options...
enpu Posted March 29, 2018 Share Posted March 29, 2018 bind function is used to define where 'this' keyword refers inside that function. Try to console.log(this); inside the remove function and you will notice that it does not refer back to the class if you don't use the bind function. If you have classes that does have update function and you wan't the scene to stop calling it, you should use game.scene.removeObject function: https://www.panda2.io/examples#scene-removeObject Quote Link to comment Share on other sites More sharing options...
greencoder Posted March 30, 2018 Author Share Posted March 30, 2018 Thanks a lot, that answers both of my questions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.