0

我试图使用箭头键来左右移动俄罗斯方块块并旋转,但是当块落下gameGrid时它不起作用。这是我的代码。

//这是我在计时器处理程序中的游戏周期

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) 
         {  

            mainGameBoard->drawGrid();
            newBlock->draw();
            newBlock->moveDown();

            if(newBlock->canMoveDown()==false)
            {
                                //If block hits bottom or block, add block to grid
                newBlock->addMySelfToGameBoard();

                                //Update the grid by deleting full rows & move
                                //the game grid down
                mainGameBoard->updateGrid();

                                //Create new block
                                //Havnt sorted yet


            }

         }

这是我使用箭头键移动和旋转块的代码,移动方法和旋转方法在​​ TTetrisBlock 类中。

private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) 
             {
                 if(e->KeyData == Keys::Left)
                 {
                     newBlock->moveLeft();
                 }

                 if(e->KeyData == Keys::Right)
                 {
                     newBlock->moveRight();
                 }

                 if(e->KeyData == Keys::Up)
                 {
                     newBlock->rotate();
                 }

             }

//现在,当块在计时器上沿游戏网格向下移动时,按下箭头键时不会移动对象。但是如果我将 newBlock->moveLeft() 放入计时器中,该块将开始向左移动。所以我的 moveLeft() 方法正在工作。请帮忙 - 我是一名学习 C++ 的学生。这是我的 moveLeft() 方法:

void TTetrisBlock::moveLeft()
{
    array<Point>^ temporaryCopy = gcnew array<Point>(4);

    for(int i=0;i<mySquares->Length;i++)
    {
        //Set future move
        temporaryCopy[i].X = mySquares[i].X-1;
        temporaryCopy[i].Y = mySquares[i].Y;
    }

    if(checkBounds(temporaryCopy) == true)
    {
        for(int i=0;i<temporaryCopy->Length;i++)
        {
            mySquares[i].X = temporaryCopy[i].X;
            mySquares[i].Y = temporaryCopy[i].Y;
        }
    }

}
4

1 回答 1

0

Form1_KeyDown 是否注册为表单的回调?这样做检查:

private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) 
             {
                 System::Console::WriteLine(S"KeyDown is working");

             }
于 2012-08-25T03:39:35.560 回答