【问题标题】:matrix game, condition when user selects 2 same colored balls it should destroy the 2 same color patterns矩阵游戏,当用户选择 2 个相同颜色的球时,它应该破坏 2 个相同颜色的图案
【发布时间】:2014-08-23 08:01:42
【问题描述】:

我正在尝试一个矩阵游戏,条件是:

  • 当用户选择 2 个相同颜色的球时,会破坏 2 个相同颜色的图案。

我已经正确地完成了水平和垂直选择。但是当我尝试交叉选择(对角线)时,它不起作用,我想,我在对角线选择条件下犯了一个错误。

这是我的编码,交叉选择不匹配相同的颜色模式。这是我的 对角线选择编码,下面的条件是否正确?

    onCheckPattern: function(pPattern) {
    if (pPattern != null) {
        this.mPromptTimerTally = 0;
        this.mPromptMarkSpr.setPosition(-1000.0, -1000.0);

        if (this.mFirstCheckPattern === null) {
            this.mFirstCheckPattern = pPattern;
            this.mCheckMarkSpr.setPosition(this.mPatternsPos[this.mFirstCheckPattern.m_nRowIndex][this.mFirstCheckPattern.m_nColIndex]);
        } else {
            this.mSecondCheckPattern = pPattern;
            if (this.mSecondCheckPattern === this.mFirstCheckPattern) {
                //                    this.mSecondCheckPattern = null;
                //                    return;
            }

            var isAdjacent = false;

            //HORIZONTAL& VERTICAL
            if (this.mFirstCheckPattern.m_nRowIndex == this.mSecondCheckPattern.m_nRowIndex) {
                if (this.mFirstCheckPattern.m_nColIndex > 0 &&
                    this.mFirstCheckPattern.m_nColIndex - 1 == this.mSecondCheckPattern.m_nColIndex)
                    isAdjacent = true;

                else if (this.mFirstCheckPattern.m_nColIndex + 1 < this.m_nMatrixCol &&
                    this.mFirstCheckPattern.m_nColIndex + 1 == this.mSecondCheckPattern.m_nColIndex)
                    isAdjacent = true;
            } else if (this.mFirstCheckPattern.m_nColIndex == this.mSecondCheckPattern.m_nColIndex) {
                if (this.mFirstCheckPattern.m_nRowIndex > 0 &&
                    this.mFirstCheckPattern.m_nRowIndex - 1 == this.mSecondCheckPattern.m_nRowIndex)
                    isAdjacent = true;
                else if (this.mFirstCheckPattern.m_nRowIndex + 1 < this.m_nMatrixRow &&
                    this.mFirstCheckPattern.m_nRowIndex + 1 == this.mSecondCheckPattern.m_nRowIndex)
                    isAdjacent = true;

            }
            //       


            //DIAGONAL SELECTION
            else if (this.mFirstCheckPattern.m_nRowIndex + 1, this.mFirstCheckPattern.m_nColIndex - 1 && this.mSecondCheckPattern.m_nRowIndex, this.mSecondCheckPattern.m_nColIndex)

            {
                isAdjacent = true;
            } else if (this.mFirstCheckPattern.m_nRowIndex - 1 == this.mSecondCheckPattern.m_nRowIndex && this.mFirstCheckPattern.m_nColIndex - 1 == this.mSecondCheckPattern.m_nColIndex) {
                isAdjacent = true;
            }




            if (isAdjacent) {
                this.mCheckMarkSpr.setPosition(-1000.0, -1000.0);

                this.swapTwoPattern(this.mFirstCheckPattern, this.mSecondCheckPattern, false);
                this.mFirstCheckPattern = null;
                this.mSecondCheckPattern = null;
            } else {
                this.mCheckMarkSpr.setPosition(this.mPatternsPos[this.mSecondCheckPattern.m_nRowIndex][this.mSecondCheckPattern.m_nColIndex]);

                this.mFirstCheckPattern = this.mSecondCheckPattern;
                this.mSecondCheckPattern = null;
            }
        }
    }
},

【问题讨论】:

  • 请正确使用标点符号,避免使用多个标点符号(即....或!!)并用点结束句子,不要忘记使用段落/换行符。我清理了它以使其更具可读性。

标签: javascript cocos2d-x cocos2d-js


【解决方案1】:

线

            else if (this.mFirstCheckPattern.m_nRowIndex + 1, this.mFirstCheckPattern.m_nColIndex - 1 && this.mSecondCheckPattern.m_nRowIndex, this.mSecondCheckPattern.m_nColIndex)

我觉得很可疑。

有点缩写,你正在写类似的东西

            else if (a + 1, b - 1 && c, d)

由于JavaScript comma operator 的工作方式,它被解释为else if (d),因此如果d 不为零,isAdjacent 将被设置为true

试试

            else if (this.mFirstCheckPattern.m_nRowIndex + 1 == this.mSecondCheckPattern.m_nRowIndex && this.mFirstCheckPattern.m_nColIndex - 1 == this.mSecondCheckPattern.m_nColIndex)

改为。

【讨论】:

    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 2021-04-17
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多