I am using code components in builder to allow participants to click and drag various stimuli to locations on a grid. I need it so that only one stimulus can be dragged at a time, and the participant can drop it within an 8x8 grid and the stimulus will snap to the spot in the grid. I have successfully coded this, but there is a bug. If the stimulus is dragged too fast, the mouse drops it. I was hoping someone could give me a suggestion on how to get rid of this bug by making my code more efficient or changing the way it works. Here’s what I have in the begin routine:
stimuli = [stim_1, stim_2, stim_3, stim_4, stim_5, stim_6, stim_7, stim_8] current_stimulus = -1 clicked_stimulus = None current_dest = None
and in every frame:
mouse_down = False selected = np.zeros(8, dtype = int) -1 for i, stimulus in enumerate(stimuli): if mouse.isPressedIn(stimulus): selected[i] = i clicked_stimulus = stimulus if i == current_stimulus: stimulus.setPos(mouse.getPos()) mouse_down = True break if not mouse_down and clicked_stimulus is not None: for dest in [a1_2, a2_2, a3_2, a4_2, a5_2, a6_2, a7_2, a8_2, b1_2, b2_2, b3_2, b4_2, b5_2, b6_2, b7_2, b8_2, c1_2, c2_2, c3_2, c4_2, c5_2, c6_2, c7_2, c8_2, d1_2, d2_2, d3_2, d4_2, d5_2, d6_2, d7_2, d8_2, e1_2, e2_2, e3_2, e4_2, e5_2, e6_2, e7_2, e8_2, f1_2, f2_2, f3_2, f4_2, f5_2, f6_2, f7_2, f8_2, g1_2, g2_2, g3_2, g4_2, g5_2, g6_2, g7_2, g8_2, h1_2, h2_2, h3_2, h4_2, h5_2, h6_2, h7_2, h8_2]: if dest.contains(mouse.getPos()): current_dest = dest clicked_stimulus.pos = current_dest.pos clicked_stimulus = None if selected.sum() == -8: current_stimulus = -1 elif current_stimulus == -1 or selected[current_stimulus].all() == -1: current_stimulus = selected[selected > -1].min() stimuli[current_stimulus].setPos(mouse.getPos())
The “snap to location” part of the code is fine, it only checks to see if the position of the stimulus overlaps with a location within the grid once the mouse press is released, so the problem lies somewhere in the code for while the mouse is pressed in. Thank you in advance to anyone who can help with this!