简体   繁体   English

需要由2次不同的点击触发2次点击事件监听器。 (JS)

[英]Need 2 Click Event Listeners to be Triggered by 2 Different Clicks. (JS)

I'm currently trying to make a checkers game (with no AI) as something to help me learn JS and HTML events and animations. 我目前正在尝试制作一款没有AI的跳棋游戏,以帮助我学习JS和HTML事件以及动画。 This is homework. 这是功课。 Forgive me and point me in the right direction if this is something that has been answered elsewhere. 原谅我,并指出正确的方向,如果这已经在其他地方得到了回答。

This is the relevant code for this problem: 这是此问题的相关代码:

canvas = document.getElementById('board');
        canvas.addEventListener('click', funcMovePiece);
function funcMovePiece(event)
    {
        var legalMove = false;
        var DXClick = 0;
        var DYClick = 0;
        var Drow = 0;
        var Dcol = 0;
        var pieceColor;
        //First Click
        var SXClick = event.pageX;
        var SYClick = event.pageY;
        var Scol = parseInt(SXClick / 100);
        var Srow = parseInt(SYClick / 100);

            canvas.addEventListener('click', funcGetSecondClick); 
            //Problem: First click gives me SX and SY, then skips this function. Browser finishes interpreting.
                    //Second click skips SX and SY (leaving them undefined) and triggers this function. Browser finishes interpreting.
            function funcGetSecondClick(event)
            {
                //Second Click
                DXClick = event.pageX;
                DYClick = event.pageY;
                Dcol = parseInt(DXClick / 100);
                Drow = parseInt(DYClick / 100);
            }

By col(umn) and row, I mean the column and row in an 8x8 2D array. 用col(umn)和row表示8x8 2D数组中的列和行。 The 2 sets of row and column variables will eventually be used as indexes for conditional statements. 这两组行和列变量最终将用作条件语句的索引。 I can include the rest of my code if it is deemed relevant. 如果认为相关,我可以包括其余代码。 I do have a jquery link called. 我确实有一个jquery链接。 The problem I am having is, once the first piece is clicked and the click event triggers, it is skipping the second click event altogether. 我遇到的问题是,一旦单击了第一部分并且触发了click事件,它将完全跳过第二个click事件。 My X and Y for the DXClick and DYClick are not being changed. DXClick和DYClick的X和Y未被更改。 If I click again, it re-interprets the program, goes into the second click event function, leaving SXClick and SYClick undefined. 如果再次单击,它将重新解释该程序,进入第二个单击事件功能,而SXClick和SYClick仍未定义。 S stands for Source and indicates the first click, while D stands for destination and stands for the second click. S代表源,表示第一次点击,而D代表目的地,代表第二次点击。 My intent here is that someone can click on one checker piece, then click an empty red square. 我的目的是让某人可以单击一个棋子,然后单击一个空的红色正方形。 The program determines if it is a legal move or not and moves the piece (haven't gotten that far in the code yet). 程序将确定是否合法移动并将其移动(代码中尚未达到该距离)。

canvas = document.getElementById('board');
canvas.addEventListener('click', funcMovePiece);
    function funcMovePiece(event)
        {
            var legalMove = false;
            var DXClick = 0;
            var DYClick = 0;
            var Drow = 0;
            var Dcol = 0;
            var pieceColor;
            //First Click
            var SXClick = event.pageX;
            var SYClick = event.pageY;
            var Scol = parseInt(SXClick / 100);
            var Srow = parseInt(SYClick / 100);
             alert('first click');

             canvas.removeEventListener('click', funcMovePiece); 
             canvas.addEventListener('click', funcGetSecondClick);              
        }

 function funcGetSecondClick(event)
                {
                    alert('second click');
                    DXClick = event.pageX;
                    DYClick = event.pageY;
                    Dcol = parseInt(DXClick / 100);
                    Drow = parseInt(DYClick / 100);                   

                    canvas.removeEventListener('click',funcGetSecondClick); 
                    canvas.addEventListener('click', funcMovePiece); 
                }

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

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