# Moving stimuli changing direction

What are you trying to achieve?:

• 2 circles presented on left or right side of screen. After 1 second, the left circle moves to the right, right moves to left.
• Once the circles reach the edge of the screen (+/-30, 0), I want them to switch directions. The left circle will now move to the left and vice versa.

What did you try to make it work?:

``````#beginning of experiment

t2 = core.Clock()

#beginning of routine

#color change
if Position == [-19.5, 0] and t >= 1 and not leftAnnuli.contains(mouse):
leftAnnuli.lineColor = [1,-0.5,-0.5] #red

elif Position == [-19.5, 0] and t >= 1 and leftAnnuli.contains(mouse):
leftAnnuli.lineColor = [-0.5,1,-0.5] #green

if Position == [19.5, 0] and t >= 1 and not rightAnnuli.contains(mouse):
rightAnnuli.lineColor = [1,-0.5,-0.5] #red

elif Position == [19.5, 0] and t >= 1 and rightAnnuli.contains(mouse):
rightAnnuli.lineColor = [-0.5,1,-0.5] #green

#beginning mvmt
initialMvmt = True
reverseMove = None
originMove = None
reset = False

#each frame

#beginning mvmt
if t > 1 and initialMvmt:
rightAnnuli.pos = [(19.5-(t-1)*5), 0]
leftAnnuli.pos = [(-19.5+(t-1)*5), 0]
if t <= 1:
mouse.setPos((0,0))

#changing directions, both extremes
if rightAnnuli.contains(-30, 0) and not reset:
t2.reset()
reverseMove = True
initialMvmt = False
reset = True

if reverseMove == True:
rightAnnuli.pos = [(-27+(time)*5), 0]
leftAnnuli.pos = [(27-(time)*5), 0]

if rightAnnuli.contains(30, 0) and reset:
t2.reset()
reverseMove = False
originMove = True
reset = False

if originMove == True:
rightAnnuli.pos = [(27-(time)*5), 0]
leftAnnuli.pos = [(-27+(time)*5), 0]
``````

What specifically went wrong when you tried that?:
They change directions, though in a jarring manner (will disappear for a frame or two then reappear, might be my system only). The big issue: after the 3rd changing of direction (right circle hits the left end for second time), the circles switch. Since I have a randomized choice of the ‘correct’ circle for the subject to follow, this is an issue. The ‘correct’ circle, which is red, will switch places with the other circle (white in colour). I don’t know what causes this.

Hi @obeescee, I am not sure what your task is trying to achieve, however you can get the circles to move using Builder, with some polygons as circles and a code component to control the movement.

``````# Start Experiment
moveSpeed = .025

# Each Frame

# Invert moveSpeed when circle at edge of screen
if polygon.pos[0] >= 1 or polygon.pos[0] <= -1:
moveSpeed *= -1

if t > 1:
polygon.pos[0] = polygon.pos[0] + moveSpeed
polygon_2.pos[0] = polygon_2.pos[0] - moveSpeed
polygon._needVertexUpdate = True
polygon_2._needVertexUpdate = True
``````

How does this perform on your machine? Here is the example circles.psyexp (8.6 KB)

1 Like

Hey @dvbridges, thank you! Much, much more sophisticated. I got a little crazy with the booleans. It worked great. Thanks again.

Hi @dvbridges,

Thank you so much for this example. It works perfectly on my computer, but when I tried to run it online with the automatically generated JS code, the circles stop moving. I tried several things but still cannot make the circles move online. Could you help me with this? Thanks a lot!

Hi @Mengfan. try using ‘setPos’ instead, so:

``````# Start Experiment
moveSpeed = .025

# Each Frame

# Invert moveSpeed when circle at edge of screen
if polygon.pos[0] >= 1 or polygon.pos[0] <= -1:
moveSpeed *= -1

if t > 1:
polygon.setPos((polygon.pos[0] + moveSpeed, 0))
``````
1 Like

Hi @dvbridges,

It works! Thank you so much!