Switch between images when space bar pressed

北城以北 提交于 2020-08-09 08:45:08

问题


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.

Here is the code for controllers:

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) 

Images:

<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">

My code for switching images:

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'}})

Delaying the icon:

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

Thanks!


回答1:


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">

Using an object

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.



来源:https://stackoverflow.com/questions/63259437/switch-between-images-when-space-bar-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!