Jump to content

Check if sprite is already added


Recommended Posts

Is there a way to check if a sprite is already added? for example check if a player and a enemy overlap if they do, then it would add a sprite somewhere on the screen 

but since this should propably be in the update function, only add the sprite once and not like 1 every millisecond.

Link to comment
Share on other sites

10 hours ago, VitaZheltyakov said:

if (typeof sprite !== "undefined")



1 hour ago, drhayes said:

Pretty sure what Vita is saying is store the sprite in a variable somewhere and check if that variable is undefined or not before adding it again. If it's around but "alive === false", you can ".revive()" it to get it back.

Is there a way to check if this is undefined, then add the image, and then move it on the y axis? i tried it but for some reason i couldn't get the arcade physics working with this in there. Also for some reason it didn't add the sprite at all. Can you help me?. I tried this:

if(game.physics.arcade.distanceBetween(player, enemy) < 60)

{ sprite.body.velocity.y = 2 }   

if(game.physics.arcade.distanceBetween(player, enemy) < 80)

{ if (typeof sprite !== "undefined")


sprite = game.add.sprite(498, 110, 'sprite')
game.physics.enable(sprite, Phaser.Physics.ARCADE)
game.physics.arcade.enable(sprite) }   }


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.

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.


  • Recently Browsing   0 members

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