按下空格键时在图像之间切换 [英] Switch between images when space bar pressed

查看:19
本文介绍了按下空格键时在图像之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在创建一个视频游戏,其中的图标随 PC 控制器一起移动.另外,我希望当我按下空格键时,我的图标(随控制器移动的飞船)在样式之间切换.我已经设法做到了,但它只能工作一次,而且我无法回到初始图像.我想要的是它在四个图像之间切换,并在最后返回原始样式.

So I am creating a video game where an icon moves with pc controllers. Also, I want that when I press the space bar, my icon (spaceship which moves with controllers) switches between styles. I have manged to do that but it only works one time and I can't go back to the initial image. What I would like to have would be that it switches between four images and at the end of it returns to the original style.

这是控制器的代码:

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) 

图像:

<img src="Photo/Spaceship.png" id="icon-p1" style="display:none">
<img src="Photo/Spaceship1.png" id="icon-p2" style="display:none">
<img src="Photo/Spaceship2.png" id="icon-p3" style="display:none">
<img src="Photo/Spaceship3.png" id="icon-p4" style="display:none">

我切换图片的代码:

document.addEventListener("keydown", function(event) {
if (event.keyCode === 32) {
event.preventDefault();
rect = document.getElementById("icon-p2")
document.getElementById('icon-p2').style.display = 'block'
document.getElementById('icon-p1').style.display = 'none'}})

延迟图标:

setTimeout(function() {
document.getElementById('icon-p1').style.display = 'block'}, 4000)}

谢谢!

推荐答案

使用 DOM

使用您的 DOM,您必须跟踪当前显示的是哪个 img.为此,我添加了两个类来切换(NoneBlock),您可以重命名/重新设置您认为合适的样式.

Using your DOM

Using your DOM you have to keep track which img is currently displayed. For that I added two classes to toggle (None and Block), which you can rename/restyle how you see fit.

//REM: Shows the first hidden image
function showFirst(){
  //REM: Get the first "none" element (=.None)
  var tFirst = document.querySelector('.None');

  //REM: Show it
  if(tFirst){
    tFirst.classList.remove('None');
    tFirst.classList.add('Block')
  }
};

document.addEventListener("keydown", function(event){
  if(event.keyCode === 32){
    event.preventDefault();
    
    //REM: Get the current "display" element (=.Block) or the first "none" one (=.None)
    var tCurrent = document.querySelector('.Block');
    
    //REM: If found...
    if(tCurrent){
      //REM: Hide current
      tCurrent.classList.remove('Block');
      tCurrent.classList.add('None');
    
      //REM: Get the next image in the markup
      var tNext = tCurrent.nextElementSibling;
      
      //REM: Show it, if exists
      if(tNext && tNext.tagName === 'IMG'){
        tNext.classList.remove('None');
        tNext.classList.add('Block')
      }
      //REM: Else show the first image again
      else{
        showFirst()
      }
    }
    //REM: Else show the first image
    else{
      showFirst()
    }
  }
})

.None{display: none}
.Block{display: block}

<img src="Photo/Spaceship.png" id="icon-p1" class="None" alt="1">
<img src="Photo/Spaceship1.png" id="icon-p2" class="None" alt="2">
<img src="Photo/Spaceship2.png" id="icon-p3" class="None" alt="3">
<img src="Photo/Spaceship3.png" id="icon-p4" class="None" alt="4">

第二种方法是只有一个image,然后把那个arrcoding的src改成一个对象.

The second way is to have only one image and change the src of that one arrcoding to an object.

var imgs = [
  {src: '', alt: '1', active: true},
  {src: '', alt: '2', active: false},
  {src: '', alt: '3', active: false},
  {src: '', alt: '4', active: false}
];

document.addEventListener("keydown", function(event){
  if(event.keyCode === 32){
    event.preventDefault();
    
    //REM: The element
    var tIMG = document.getElementById('icon-p1');
    if(tIMG){
      //REM: Getting index of current
      var tIndex = imgs.findIndex(element => element.active);

      //REM: Getting the next or first object
      var tNext = (tIndex < imgs.length-1) ? imgs[tIndex + 1] : imgs[0];
      
      //REM: Assigning the properties
      tIMG.src = tNext.src;
      tIMG.alt = tNext.alt;
      
      //REM: Toggling the activity in the object
      imgs[tIndex].active = false;
      tNext.active = true
    }
  }
})

<img src="Photo/Spaceship.png" id="icon-p1" alt="1">

请注意,这些都是基本示例,并不是高效的代码.

Be aware that those are all basic examples and not productive code.

这篇关于按下空格键时在图像之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆