Larry

Create and Destroy Sprites Using Functions

Recommended Posts

I'm trying to create platforms for my player to jump onto and also make collectible stars. I can create these sprites using functions, but I don't know how to make physics work on them so that the character can jump on the platforms and overlap with the stars. The problem is that the physics portion doesn't work when I call it in the update function. I am also not using tilesprites, so it can't work that way.

 

create: function() {
    addStar(star1);
},
update: function() {
    removeStar(star1);
}

addStar(name) {
    name = game.add.sprite(1700, 550, 'star');
    name.anchor.setTo(0.5, 0.5);
    this.game.physics.enable(name, Phaser.Physics.ARCADE);
    name.body.collideWorldBounds = false;
    name.enableBody = true;
    name.body.immovable = true;
}

removeStar(name) {
    if (game.physics.arcade.overlap(character, name) == true) {
        name.destroy();

        updateScore();
    }
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.