Created
July 19, 2017 17:32
-
-
Save DestinyLuong/0e2f64d8415b03f449e5babc3c28074a to your computer and use it in GitHub Desktop.
Exported from Popcode. Click to import: https://popcode.org/?gist=0e2f64d8415b03f449e5babc3c28074a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>X movement Collision Left Only</title> | |
| </head> | |
| <body> | |
| <!-- Put your page markup here --> | |
| <div class="box" id="blue"></div> | |
| <div class="box" id="red"></div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {"enabledLibraries":["jquery"]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function checkCollision() { | |
| var blueLeft = $("#blue").offset().left; | |
| var redLeft = $("#red").offset().left; | |
| var blueRight = blueLeft + $("#blue").width(); | |
| var redRight = redLeft + $("#red").width(); | |
| var blueTop = $("#blue").offset().top; | |
| var redTop = $("#red").offset().top; | |
| var blueBottom = blueTop + $("#blue").height(); | |
| var redBottom = redTop + $("#red").height(); | |
| if(blueRight > redLeft && blueLeft < redRight && blueBottom > redTop && blueTop < redBottom) { | |
| alert("Touching"); | |
| $("#blue").hide(); | |
| } | |
| } | |
| $("body").keydown(function(event) { | |
| //moves left | |
| if (event.which === 37) { | |
| $("#blue").css("left", $("#blue").offset().left - 40); | |
| //moves right | |
| } else if (event.which === 39) { | |
| $("#blue").css("left", $("#blue").offset().left + 40); | |
| } else if (event.which === 38) { | |
| $("#blue").css("top", $("#blue").offset().top - 40); | |
| } else if (event.which === 40) { | |
| $("#blue").css("top", $("#blue").offset().top + 40); | |
| } else { | |
| return; | |
| } | |
| //Checks for position relative to each other | |
| checkCollision(); | |
| }); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .box { | |
| height: 100px; | |
| width: 100px; | |
| position: absolute; | |
| } | |
| #blue { | |
| background-color: rgba(0, 0, 255, 0.7); | |
| z-index: 1; | |
| } | |
| #red { | |
| background-color: red; | |
| left: 50%; | |
| right: 50%; | |
| top: 50%; | |
| bottom: 50%; | |
| margin-left: -50px; | |
| margin-right: -50px; | |
| margin-top: -50px; | |
| margin-bottom: -50%; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment