Jump to content

Detect when sprite is on top of a sprite


Legomite
 Share

Recommended Posts

Hi @Legomite

 

So, I think you are basically looking for a simple hit-test without using physics.

 

Here is the code I use ...

var player = new game.Sprite('player.png');player.position.set(240*game.scale, 160*game.scale);player.anchor.set(0.5, 1);this.stage.addChild(player);game.scene.addObject(player); player.update = function() {   if (!colliding) {     if (((player.x - player.width/2) < (sprite2.x + sprite2.width/2)) &&         ((player.x + player.width/2) > (sprite2.x - sprite2.width/2)) &&         ((player.y - player.height/2) < (sprite2.y + sprite2.height/2)) &&         ((player.y + player.height/2) > (sprite2.y - sprite2.height/2))) {         // Do something         colliding = true;     }   } }
Link to comment
Share on other sites

Hi @Legomite

 

So, I think you are basically looking for a simple hit-test without using physics.

 

Here is the code I use ...

var player = new game.Sprite('player.png');player.position.set(240*game.scale, 160*game.scale);player.anchor.set(0.5, 1);this.stage.addChild(player);game.scene.addObject(player); player.update = function() {   if (!colliding) {     if (((player.x - player.width/2) < (sprite2.x + sprite2.width/2)) &&         ((player.x + player.width/2) > (sprite2.x - sprite2.width/2)) &&         ((player.y - player.height/2) < (sprite2.y + sprite2.height/2)) &&         ((player.y + player.height/2) > (sprite2.y - sprite2.height/2))) {         // Do something         colliding = true;     }   } }

sprite2 is the sprite that's suppose to be the other one?

Do something under colliding = 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...