简体   繁体   English

简单的香草JavaScript游戏

[英]Simple vanilla javascript game

I am trying to make a simple slot machine game as the first one here . 我正在尝试制作一个简单的老虎机游戏,作为这里第一个游戏。

The one in the example above is a bit complex, but I only need mine to add a few classes here and there at simple click events. 上面示例中的那个有点复杂,但是我只需要在简单的单击事件中在这里和那里添加一些类即可。

Long story short, I have a play button which initiates the slot machine (shows it - this is done), and when the slot machine is shown, a SPIN button appears, and when I click that button, I can only make it add one class for the first combination (eg "bonus") which adds the CSS class and its respective transitions to the desired effect. 长话短说,我有一个播放按钮来启动老虎机(显示它-完成),当显示老虎机时,出现一个SPIN按钮,当我单击该按钮时,我只能使其添加一个第一个组合的class(例如“ bonus”),它将CSS类及其相应的过渡添加到所需的效果。

And now comes the tricky part for me, I need to click the same SPIN button again, but add a different class this time. 现在对我来说是棘手的部分,我需要再次单击相同的SPIN按钮,但是这次添加一个不同的类。 This has to work 3 times, while when I am pressing it for the 4-th time, I need it to add another class to the id='ad-cont' div. 这必须工作3次,而当我第四次按下它时,我需要它在id='ad-cont'添加另一个类。

How can I achieve this with vanilla JS? 如何使用香草JS实现此目的? I know there must be some sort of a loop here, but I can't figure it out. 我知道这里一定有某种循环,但我无法弄清楚。

<div id="ad-cont">
  <div id="slot-frame"><img class="frame" src="../frame.png">
    <div class="frame-item"></div>
    <div class="frame-item"></div>
    <div class="frame-item"></div>
    <div class="frame-item"></div>
    <div class="frame-item"></div>
  </div><a id="play" href="#">PLAY</a><a id="spin" href="#">SPIN</a>
</div>

And here is what I've got so far: 这是到目前为止我得到的:

var adContainer = document.getElementById('ad-cont');
var playBtn = document.getElementById('play');
var spinBtn = document.getElementById('spin');
var slotMachine = document.getElementById('slot-frame');

playBtn.addEventListener('click', function(){
  adContainer.classList.add('playing');
});

spinBtn.addEventListener('click', function(){
  slotMachine.classList.add('bonus');
});

Thank You all! 谢谢你们!

There's not enough code to really give a complete answer, but if I understand you right, I think the easiest thing to do would be to add a counter and an array of classes. 没有足够的代码来真正给出完整的答案,但是如果我理解正确,我认为最简单的方法是添加一个计数器和一组类。

var counter = 0;
var classes = ['playing', 'playing_again', 'another_class', 'bonus']

Then you can add classes as you play: 然后,您可以在播放时添加课程:

playBtn.addEventListener('click', function(){
  adContainer.classList.add(classes[counter]);
  // if you need to do other things here you
  // can check the counter value and proceed accordingly
  counter++
});

You just need to check that counter never gets higher than the last index of classes 您只需要检查counter永远不会比classes的最后一个索引高

Have you tried using a switch and a count that resets itself? 您是否尝试过使用可重置自身的开关和计数?

var count = 0;
spinBtn.addEventListener('click', function(){
    count++;
    switch(count) {
        case 1:
            slotMachine.classList.add('bonus');
            break;
        case 2:
            slotMachine.classList.add('bonus2');
            break;
        case 3:
            slotMachine.classList.add('bonus3');
            break;
        case 4:
            otherIdElement.classList.add('finished!');
            count = 0;
            break;
        default:
            alert("error!");
    } 
});

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

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