Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Appear text when one div is on top of the other

I am trying to make some kind of "game" and what I want to do is to when I move around a div, to get notified when the moving red div is on top of the static green div.

By "notified" I mean setting the paragraphs display property to "block" or even my a console.log() , it’s not that important how I get the message.

PS: You can move around the red block with your up,left,right,down arrows.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s my code:

var keys = {};
        keys.UP = 38;
        keys.LEFT = 37;
        keys.RIGHT = 39;
        keys.DOWN = 40;

    /// store reference to character's position and element
    var character = {
      x: 100,
      y: 100,
      speedMultiplier: 2,
      element: document.getElementById("character")
    };

    /// key detection (better to use addEventListener, but this will do)
    document.body.onkeyup = 
    document.body.onkeydown = function(e){
      if (e.preventDefault) { 
        e.preventDefault();
      }
      else {
        e.returnValue = false; 
      }
      var kc = e.keyCode || e.which;
      keys[kc] = e.type == 'keydown';
    };

    /// character movement update
    var moveCharacter = function(dx, dy){
      character.x += (dx||0) * character.speedMultiplier;
      character.y += (dy||0) * character.speedMultiplier;
      character.element.style.left = character.x + 'px';
      character.element.style.top = character.y + 'px';
    };

    /// character control
    var detectCharacterMovement = function(){
      if ( keys[keys.LEFT] ) {
        moveCharacter(-5, 0);
      }
      if ( keys[keys.RIGHT] ) {
        moveCharacter(5, 0);
      }
      if ( keys[keys.UP] ) {
        moveCharacter(0, -5);
      }
      if ( keys[keys.DOWN] ) {
        moveCharacter(0, 5);
      }
    };

    /// update current position on screen
    moveCharacter();

    /// game loop
    setInterval(function(){
      detectCharacterMovement();
    }, 1000/24);
    
        #character {
    position: absolute;
    width: 42px;
    height: 42px;
    background: red;
    z-index:99;
}
#container{
  width: 250px;
  height: 250px;
  background: black;
  position: relative;
  overflow: hidden;
}
#character2{
    position:absolute;
    width:50px;
    height:50px;
    background-color: yellowgreen;
    top:50px;
    left:50px;
}
#notifier{
    display:none;
}
<div id="container">
        <div id="character"></div>

        <div id="character2"></div>
    </div>
    <p id="notifier"> Red div is on top of the yellow div</p>

>Solution :

I have adapted this answer so that the message is displayed when the red div is on top of the other :

var keys = {};
keys.UP = 38;
keys.LEFT = 37;
keys.RIGHT = 39;
keys.DOWN = 40;

/// store reference to character's position and element
var character = {
  x: 100,
  y: 100,
  speedMultiplier: 2,
  element: document.getElementById("character")
};

var is_colliding = function(div1, div2) {
  var d1_height = div1.offsetHeight;
  var d1_width = div1.offsetWidth;
  var d1_distance_from_top = div1.offsetTop + d1_height;
  var d1_distance_from_left = div1.offsetLeft + d1_width;

  var d2_height = div2.offsetHeight;
  var d2_width = div2.offsetWidth;
  var d2_distance_from_top = div2.offsetTop + d2_height;
  var d2_distance_from_left = div2.offsetLeft + d2_width;

  var not_colliding =
    d1_distance_from_top <= div2.offsetTop ||
    div1.offsetTop >= d2_distance_from_top ||
    d1_distance_from_left <= div2.offsetTop ||
    div1.offsetLeft >= d2_distance_from_left;

  return !not_colliding;
};

/// key detection (better to use addEventListener, but this will do)
document.body.onkeyup = 
document.body.onkeydown = function(e){
  if (e.preventDefault) { 
    e.preventDefault();
  }
  else {
    e.returnValue = false; 
  }
  var kc = e.keyCode || e.which;
  keys[kc] = e.type == 'keydown';
};

/// character movement update
var moveCharacter = function(dx, dy){
  character.x += (dx||0) * character.speedMultiplier;
  character.y += (dy||0) * character.speedMultiplier;
  character.element.style.left = character.x + 'px';
  character.element.style.top = character.y + 'px';
  
  if(is_colliding(character.element, document.getElementById("character2"))) {
    document.getElementById("notifier").style.display = "block";
  } else {
    document.getElementById("notifier").style.display = "none";
  }
};

/// character control
var detectCharacterMovement = function(){
  if ( keys[keys.LEFT] ) {
    moveCharacter(-5, 0);
  }
  if ( keys[keys.RIGHT] ) {
    moveCharacter(5, 0);
  }
  if ( keys[keys.UP] ) {
    moveCharacter(0, -5);
  }
  if ( keys[keys.DOWN] ) {
    moveCharacter(0, 5);
  }
};

/// update current position on screen
moveCharacter();

/// game loop
setInterval(function(){
  detectCharacterMovement();
}, 1000/24);
#character {
    position: absolute;
    width: 42px;
    height: 42px;
    background: red;
    z-index:99;
}
#container{
  width: 250px;
  height: 250px;
  background: black;
  position: relative;
  overflow: hidden;
}
#character2{
    position:absolute;
    width:50px;
    height:50px;
    background-color: yellowgreen;
    top:50px;
    left:50px;
}
#notifier{
    display:none;
}
<div id="container">
    <div id="character"></div>

    <div id="character2"></div>
</div>
<p id="notifier"> Red div is on top of the yellow div</p>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading