0

私はこれに数週間取り組んできましたが、それを機能させることができます。それはほとんどそこにあります。私が見逃しているものがあると思います。私は基本的に、ブロックとボールの間の衝突検出を、私が望むように機能させることができません。

SWF のダンプ

ボールには、数秒以内に大量のブロックを耕すチャンスがまだあります。同じような悩みを抱えている方のお話をお聞きしたいです。

コードは次のとおりです。

private function checkCollision():void
    {
        grdx = Math.floor((ball.x) / 28);
        grdy = Math.floor((ball.y) / 14);
        ngrdx = Math.floor((ball.x + dx) / 28);
        ngrdy = Math.floor((ball.y + dy) / 14);


        if (grdy <= level.length - 1 && ngrdy <= level.length - 1 && grdy >= 0 && ngrdy >= 0) 
        {   

            if(level[grdy][ngrdx] > 0) 
            {

                level[grdy][ngrdx] = 0;
                bnm = "Block_" + grdy + "_" + ngrdx;    
                if (this.getChildByName(bnm) != null)
                {

                    this.removeChild(this.getChildByName(bnm));
                    dx *= -1;   
                    totalBreaks++;

                    trace("Hit on X");
                    trace("Block: " + totalBreaks + " / " +  totalBlocks);
                }


            }
            else if(level[ngrdy][grdx] > 0) 
            {               

                bnm = "Block_" + ngrdy + "_" + grdx;
                level[ngrdy][grdx] = 0; 
                 if (this.getChildByName(bnm) != null)
                {

                    this.removeChild(this.getChildByName(bnm));
                    dy *= -1;
                    totalBreaks++;
                    trace("Hit on Y");
                    trace("Block: " + totalBreaks + " / " +  totalBlocks);
                }
            }
            if(level[ngrdy][ngrdx] > 0) 
            {               

                bnm = "Block_" + ngrdy + "_" + ngrdx;
                level[ngrdy][ngrdx] = 0;    
                if (this.getChildByName(bnm) != null)
                {

                    this.removeChild(this.getChildByName(bnm));
                    dy *= -1;
                    dx *= -1;
                    totalBreaks++;
                    trace("hit on X,Y");
                    trace("Block: " + totalBreaks + " / " +  totalBlocks);
                }

            }
        }

    }
4

1 に答える 1

0

これが正しいかどうかはわかりませんが、あなたのif else if発言に1つの問題があると思います.

あなたの例から、への呼び出しはyourとvars をcheckCollision()2回変更する可能性があり、その結果、ボールがレンガにぶつかっている間、ボールは同じ方向に進み続けます。dxdy

私が正しければif、次のようにステートメントをリファクタリングできます。

if (level[ngrdy][ngrdx] > 0)
{
    [...]
}
else if (level[grdy][ngrdx] > 0)
{
    [...]
}
else if (level[ngrdy][grdx] > 0)
{
    [...]
}

さらに、単純なテストを括弧で囲むことは良い習慣です。したがって、それを書く代わりに:

if (grdy <= level.length - 1 && ngrdy <= level.length - 1 && grdy >= 0 && ngrdy >= 0)

私はこれを書きます:

if ((grdy <= level.length - 1) && (ngrdy <= level.length - 1) && (grdy >= 0) && (ngrdy >= 0))

したがって、コードが読みやすくなります。

良い仕事ですが、続けてください!

編集:あなたのコメントはそれが正しい答えではないことを示唆しているので、もう少し掘り下げて、いくつかのブール値を使用して両方の軸で方向を反転する必要があるかどうかを確認する別の提案があります。また、少しリファクタリングも行いました。

private function checkCollision():void
{
    grdx = Math.floor((ball.x) / 28);
    grdy = Math.floor((ball.y) / 14);
    ngrdx = Math.floor((ball.x + dx) / 28);
    ngrdy = Math.floor((ball.y + dy) / 14);

    var flipX:Boolean = false;
    var flipY:Boolean = false;


    if ((grdy <= level.length - 1) && (ngrdy <= level.length - 1) && (grdy >= 0 && ngrdy >= 0))
    {
        if (testBlock(grdx, ngrdy))
        {
            flipY = true;
        }
        if (testBlock(ngrdx, grdy))
        {
            flipX = true;
        }
        if (testBlock(ngrdx, ngrdy))
        {
            flipX = true;
            flipY = true;
        }

        dx *= flipX ? -1 : 1;
        dy *= flipY ? -1 : 1;
    }
}

private function testBlock(xPos:int, yPos:int):Boolean
{
    if (level[yPos][xPos] > 0)
    {
        trace("hit on X,Y");
        level[yPos][xPos] = 0;
        breakBlock("Block_" + yPos + "_" + xPos);
        trace("Block: " + totalBreaks + " / " + totalBlocks);
        return true;
    }
    return false;
}

private function breakBlock(blockName:String):void
{
    if (this.getChildByName(blockName))
    {
        this.removeChild(this.getChildByName(blockName));
        totalBreaks++;
    }
}

それがたまたま正しい解決策だった場合、私はこの答えをきれいにします...

于 2013-03-19T01:53:50.193 に答える