简体   繁体   English

如何防止 go 的图像超出屏幕边框?

[英]How to prevent image to go out of screen borders?

So I am creating a game where a spaceship moves with keyboard controllers.所以我正在创建一个游戏,其中宇宙飞船与键盘控制器一起移动。 However, when this icon is pushed towards the screen limits, it disappears.然而,当这个图标被推向屏幕边界时,它就会消失。 I did a code to prevent this but it does not work any idea why?我做了一个代码来防止这种情况,但它不知道为什么?

Code for spaceship宇宙飞船代码

let display = document.getElementById("body").style.width
let rect = document.getElementById("icon-p1")
let pos = {top: 85, left: 600}
const keys = {}
window.addEventListener("keydown", function(e) {keys[e.keyCode] = true})
window.addEventListener("keyup", function(e) {keys[e.keyCode] = false})
const loop = function() {
if (keys[37] || keys[81]) {pos.left -= 10}
if (keys[39] || keys[68]) {pos.left += 10}
if (keys[38] || keys[90]) {pos.top -= 1}
if (keys[40] || keys[83]) {pos.top += 1}
rect.style.left = pos.left + "px"; rect.style.top = pos.top + "%"}
let sens = setInterval(loop, 1000 / 40)

Code for background背景代码

width: 100%;
background-image: url(Photo/bg.jpg);
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
overflow: hidden; }

try using position:relative;尝试使用 position:relative; for container element用于容器元素

You need to correct top/left positions when they are out of bounds, fixing them to the limit border ( owidth / oheight are the container dimensions, iwidth / iheight are the element dimensions).当它们超出范围时,您需要更正顶部/左侧位置,将它们固定到限制边界( owidth / oheight是容器尺寸, iwidth / iheight是元素尺寸)。

 if (pos.left<0) pos.left=0;
 if (pos.top<0) pos.top=0;
 if (pos.left+iwidth>=owidth) pos.left=owidth-iwidth;
 if (pos.top+iheight>=oheight) pos.top=oheight-iheight;

Applied to the example code (I've changed % increments to px increments, to simplify the calculation).应用于示例代码(我已将%增量更改为px增量,以简化计算)。 Otherwise you need to convert percentage to top position in pixels using the container height.否则,您需要使用容器高度将百分比转换为以像素为单位的顶部 position。

 (function(){ let display = document.getElementById("body"); let rect = document.getElementById("icon-p1"); let pos = {top: 85, left: 100} const keys = {} window.addEventListener("keydown", function(e) {keys[e.keyCode] = true; }) window.addEventListener("keyup", function(e) {keys[e.keyCode] = false}) const loop = function() { if (keys[37] || keys[81]) {pos.left -= 10} if (keys[39] || keys[68]) {pos.left += 10} if (keys[38] || keys[90]) {pos.top -= 10} if (keys[40] || keys[83]) {pos.top += 10} var owidth=display.offsetWidth; var oheight=display.offsetHeight; var iwidth=rect.offsetWidth; var iheight=rect.offsetHeight; if (pos.left<0) pos.left=0; if (pos.top<0) pos.top=0; if (pos.left+iwidth>=owidth) pos.left=owidth-iwidth; if (pos.top+iheight>=oheight) pos.top=oheight-iheight; rect.setAttribute("data",owidth+":"+oheight); rect.style.left = pos.left + "px"; rect.style.top = pos.top + "px" } let sens = setInterval(loop, 1000 / 40) })();
 body{ margin:0; padding: 0; } #body{ width: 100%; height: 100vh; overflow: hidden; border: 1px solid red; } #icon-p1{ position: absolute; display: inline-block; width: 40px; height: 40px; border: 1px solid red; }
 <div id ="body" > <span id="icon-p1">ICON</span> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM