Jump to content

Class Self Destruct


greencoder
 Share

Recommended Posts

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();
    },
});

 

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...