2017-08-14 30 views
0

我试图做到:如何使用SDL切换点击事件?

  • 当用户点击在游戏中,用户点击地板砖就会如果用户点击一次(任何地方)显示边框
  • 边界消失

我到目前为止有:

  • 当用户单击游戏,边框周围出现选择瓷砖

我似乎什么都不能找出(我用尽了一切我能想到的)

  • 如何获得边境接一个地点击

关于我的代码走开:

我有一个MouseInput类,检查是否按下鼠标左键。我使用布尔变量来尝试切换显示瓦片边界的变量(如果单击)或不显示边界(如果再次单击)。我有的代码将允许边框显示,但我无法让它消失另一次点击。我无法真正展示我尝试过的所有事情(一直试着这样做2天,不记得我做了什么)。这是我的代码到目前为止的一个总括:

bool toggle; // Set to false in constructor 
bool justPressed; // Set to false in constructor 
bool justReleased; // Set to false in constructor 

void Mouse::Update() // My custom mouse class Updating function (updates position, etc) 
{ 
    input.Update(); // My MouseInput class Updating function. 

    if (input.Left() && !toggle) // input.Left() checks if left mouse was pressed. True if it is pressed down, and false if it's not pressed. 
    { 
     // So we have pressed the mouse 
     justPressed = true; 
     justReleased = false; 
     printf("UGH FML"); 
    } 
    else if (!input.Left()) // So the mouse has been released (or hasn't clicked yet) 
    { 
     justPressed = false; 
     justReleased = true; 
    } 

    if (justPressed) 
    { 
     toggle = true; 
    } 
} 

我试过了所有我能想到的切换回到错误。现在我的大脑受伤了。可能有一个真正简单的解决方案,但我无法围绕它解决问题。建议?

+2

像'如果(事件)的边界=边界;' – sp2danny

回答

0

我想你要找的是下面的代码块:

if (input.Left() && toggle) { //mouse is pressed and toggle is already true 
    toggle = false; 
} 

你也应该删除下面的代码块,因为它会切换设置为true,如果你按下,无论是否触发已经真:

if (justPressed) { 
    toggle = true; 
} 

相反,您可以直接设置肘内如果对应于初始点击:

if (input.Left() && !toggle) { //mouse is pressed and toggle is false 
    toggle = true; 
} 

正如sp2danny提到的,那些共同的两个块可以被简化为:

if (input.Left()) { //mouse is pressed 
    toggle = !toggle; 
}