Legomite Posted November 8, 2014 Share Posted November 8, 2014 How do I detect when a sprite is on a sprite or in a certain place?I want to make it so that I can drag items unto something then delete the selected sprite. Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted November 8, 2014 Share Posted November 8, 2014 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; } } } Quote Link to comment Share on other sites More sharing options...
Legomite Posted November 8, 2014 Author Share Posted November 8, 2014 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? Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted November 8, 2014 Share Posted November 8, 2014 Yup, sprite2 is the sprite the player is colliding with. // Do something -> is where your code goes. What do you want to happen? colliding = true makes sure it only collides once and doesn't keep occuring. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.