首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在鼠标单击位置检测对象

在鼠标单击位置检测对象
EN

Stack Overflow用户
提问于 2015-11-18 15:27:21
回答 1查看 591关注 0票数 0

我有大量不同的图像刺激呈现在屏幕上的不同位置。

当参与者单击刺激时,我需要该刺激项目的名称,以便在脚本的其余部分中使用。例如,您可以使用SlideState.HitTest(x, y)方法在example中实现这一点,其中x和y是鼠标坐标。

我所看到的唯一类似的东西是mouse.isPressedIn(shape)方法。但是,由于必须提供一个特定的对象作为参数,所以似乎每个刺激都需要一个if子句,这看起来很麻烦(特别是对于数量更多的项目)。

有更好的方法吗?我还在学习所以我可能遗漏了什么。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-18 18:13:49

不,我想不是。但是,如果您只是将所有对象添加到一个列表中并对它们进行循环,那么代码就足够整洁了。

代码语言:javascript
复制
# List of (pointers to) stimulus objects
shapes = [shape1, shape2, shape3, shape4, shape5]

# Loop over them and check if mouse is pressed on each one of them
for shape in shapes:
    if mouse.isPressedIn(shape):
        # Set this variable to point to the latest pressed shape
        pressed_shape = shape

# Now you can do stuff with this object
pressed_shape.fillColor = 'red'
pressed_shape.draw()
print pressed_shape.name

请注意,如果鼠标单击有两个对象的位置,则pressed_shape是该解决方案列表中的最新内容。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33783997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档