简体   繁体   English

使用对象的javascript函数

[英]javascript function using objects

I am designing a sort of game where there are team bases that surround a hexagonal board. 我正在设计一种游戏,其中有围绕六边形板的团队基地。 The idea is that when a team base is clicked, it will be that team's turn. 这个想法是,当单击一个团队基础时,该轮到该团队了。 I have: 我有:

    $('.team').click(function(){
        var teamID=$(this).attr('id');
        explore(teamID);
    });

I then use the teamID to find the index of the team that was clicked, which is stored as an object from a json file with attributes such as teamname, score, etc. 然后,我使用teamID查找被单击的团队的索引,该索引作为json文件中的对象存储为对象,该文件具有teamname,score等属性。

    function explore(index){         // the game portion
        var turn=file[index];        // finds the team's info from json file
        $('.hex').click(function(){  // when a hex is clicked.... play the game
            alert(turn.teamname);       
            // game elements 
    }

This always works the first time around, however if I click on a different team box and then a hex, oftentimes it thinks that it is the turn of the box I clicked before. 这总是在第一次出现时起作用,但是,如果我单击其他团队框,然后单击一个十六进制,通常会认为这是我之前单击的框的转折。 I added the alert(turn.teamname) to try to debug. 我添加了警报(turn.teamname)以尝试调试。 If I'm clicking a different box, it will always alert the first box, and then send a second alert with the different box. 如果我单击其他框,它将始终向第一个框发出警报,然后使用该不同框发送第二个警报。 I can't figure out why there would be two alerts? 我不知道为什么会有两个警报? So it will always alert 'team1' and then 'team1','team2'. 因此它将始终警告“ team1”,然后警告“ team1”,“ team2”。 as I click more boxes it keeps alerting until it just alerts all of them. 当我单击更多框时,它会一直保持警报状态,直到仅警告所有这些框。 Additionally, if I have clicked more than two boxes before, even if I keep clicking the same hex it seems like it alternates between alerting me that it is 'team1' and 'team2'. 另外,如果我之前单击了两个以上的框,即使我一直单击相同的十六进制,似乎在提醒我是“ team1”和“ team2”之间也交替出现。

This is my first stackoverflow post so I hope it makes sense! 这是我的第一个stackoverflow帖子,所以我希望这很有道理! Thanks! 谢谢!

The reason you get this behavior is that you add event handlers to dom elements but never remove them. 出现此行为的原因是,您向dom元素添加了事件处理程序,但从未删除它们。 You need to change the way you handle clicks on the hexes. 您需要更改十六进制点击的处理方式。 You may add an event handler once with http://api.jquery.com/one/ to the parent element that holds all hexes and check which hex is clicked inside the event handler. 您可以使用http://api.jquery.com/one/将事件处理程序添加一次到包含所有十六进制的父元素,然后检查在事件处理程序中单击了哪个十六进制。 Or you can try a more trivial solution where you add an event listener once to the hexes and check if there is a selected team: 或者,您可以尝试更简单的解决方案,在该解决方案中,将事件侦听器一次添加到十六进制,然后检查是否有选定的团队:

var turn;
var teamSelected = false;
function explore(index){         // the game portion
    teamSelected = true;
    turn=file[index];        // finds the team's info from json file
}
$('.hex').click(function(){  // when a hex is clicked.... play the game
    if (teamSelected) {
        alert(turn.teamname);       
        // game elements 
        teamSelected = false;
    }
}

For something like this, I would recommend getting into meteor. 对于这样的事情,我建议您进入流星。 The reactive programming model is much cleaner than an imperative one (especially in a game where it can quickly get complex). 反应式编程模型比命令式编程模型干净得多(尤其是在游戏中它可能会很快变得复杂)。

I feel that this example illustrates what can be done very quickly using this framework (closest to your question's example). 我认为该示例说明了使用此框架可以非常快地完成的工作(最接近您的问题的示例)。

To dive in, I'd recommend looking at the intro video , then proceed to the docs and a recent book about it. 要深入了解,我建议您观看介绍视频 ,然后继续阅读文档和有关该视频最新书籍

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

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