SFML 2.1 - 检测单个鼠标单击并更改纹理 [英] SFML 2.1 - Detecting a single mouse click and changing a texture
问题描述
我想做的是:
我有一个sprite。这个sprite有3个纹理。第一个纹理,让我们称之为未访问,是最初的一个。现在,当我点击sprite它应该改变它的纹理为点击,意味着精灵被点击。这个纹理应该保留,直到我点击屏幕上的其他地方。当我这样做,纹理应该改变为第三个,已访问。如果我再次点击sprite它应该改变它的纹理为点击...
What I'm trying to do is: I have a sprite. And this sprite has 3 textures. First texture, let's call it "not-visited", is the initial one. Now, when I click on the sprite it should change it texture to "clicked" meaning that the sprite was clicked. And this texture should remain until I click somewhere else on the screen. When I do this, the texture should change to the third one, "visited". And if I click on the sprite again it should change its texture to "clicked"...
所以,我应该有一个bool函数名为ClickDetected代码应该这样做:
So, I should have here a bool function called "ClickDetected" and its code should do this:
if (event.type == Event::MouseButtonReleased && event.mouseButton.button == Left)
if (mouse_over_sprite)
return true;
对不对? OK,那么,我有一个类Node。这个类有一个方法Clicked:
Right? OK, then, I have a class Node. This class has a method "Clicked":
bool Clicked {
if (ClickDetected) { return true; }
}
我知道这可能是不必要的,但让它留下来。接下来,在此类的Update()方法中:
I know it may be unnecessary, but let it stay for now. Next, in Update() method of this class:
if (Clicked) { change_texture_to_"clicked"; if (!visited) visited=true; }
else {
if (!visited) change_texture_to_"not-visited";
if (visited) change_texture_to_"visited";
}
但这不行如何应该。当我点击sprite纹理更改为点击。当我释放鼠标按钮点击纹理保持...但是当我移动鼠标纹理更改为已访问。它应该保持点击,直到我点击别的地方。我试图在这里使用一个while循环,但它没有工作。我可以做什么?
But this doesn't work how it should. When I click on sprite texture changes to "clicked". When I release the mouse button the "clicked" texture stays... But when I move the mouse texture changes to "visited". And it should remain "clicked", until I click somewhere else. I tried to use a while loop here, but it didn't work. What can I do?
推荐答案
我把这个代码转换到Graph类(方法Update())来管理许多节点。我改变了一点,现在它工作正常。
I've put this code to Graph class (method Update()) to manage many Nodes. I've change it a little bit and now it works fine.
这篇关于SFML 2.1 - 检测单个鼠标单击并更改纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!