Jump to content

Can't create sprite within own fucntion


DanielMontilla
 Share

Recommended Posts

Trying to spawn target periodically using .time.addEvent. But the callback function won't spawn the sprite. Console spits out TypeError: this.add is undefined. I'm fairly new to JavaScript sorry. (I've placed the sprite outside of the spawnTarget function and it works fine!)

    create() {
        let targetSpawnInterval = 350;

        function spawnTarget () {
            this.add.sprite(Phaser.Math.Between(0, 600), Phaser.Math.Between(0, 800), 'target');
        };
  
        this.time.addEvent({
            delay: targetSpawnInterval,
            callback: spawnTarget,
            loop: true
        });
    }

 

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