| Reference | Downloads | Github

Sequence of images left/right - next/previous

I am trying to create a task where the participant can move from one image to the next using the keyboard (if press right then move to the next image and if press left then move back to the previous image). I have 10 images in total and the participant can spend as much time as h/she likes in each image. The images are sequential, so to one image always follows the same next image. Is there a way I can implement this in Builder? I am not good with programming, but any script that can help me to achieve this goal would be much appreciated. Many thanks

This is a little tricky, as Builder is constructed around the idea of trials always advancing in one direction. This sort of design is easy to implement if you write the code yourself, but it kind of breaks Builder’s model (Builder’s ease of use comes at the expense of some reduced flexibility).

This could be relatively simply implemented with some custom code snippets in Builder though, as long as the images are the only thing that needs to vary from trial to trial. Are there other variables that are being controlled via your loop? i.e do you currently have columns in your conditions file for something other than the image file names?

Hi Michael, many thanks for your response. I don’t have columns in my condition, so no other variables to control for. It would be great if you could help with a custom code.

OK, good. So surround your routine with a loop and give it some unfeasibly large number for repetitions that will well exceed the 10 images, to allow for additional trials as a result of repeating the image (say, an nReps value of 100).

Then insert a code component in your routine (from the “custom” component panel). This allows you to insert some code that will run at certain times in the experiment. In the “begin experiment” tab, put something like this:

# use the actual filenames of your images:
images = ['image_01.jpg', 'image_02.jpg', 'image_03.jpg', …, 'image_10.jpg']

# randomise their order:

# keep track of which image should be shown:
image_number = 0

Then something like this in the “begin routine” tab:

current_image = images[image_number]

# record things in the data:
thisExp.addData('image_file', current_image)
thisExp.addData('image_number', image_number)

Make sure your image component is below your code component so that it can refer to the latest values, and put $current_image in its “image” field, and set that field to update every repeat.

Now we need to control which image will be shown on the next trial. This is done with code in the “end routine” tab, because you need to see what key was pressed in your keyboard component (which should be set to force the end of the routine, and only accept the ‘left’ or ‘right’ keys), and at that point decide to show the next stimulus, the previous one, or after all have been shown, end the loop.

if 'left' in your_keyboard_component.keys:
    image_number = min(0, image_number - 1) # can't go below 0
else: # 'right' is the only other possible response
    image_number = image_number + 1

if image_number == 10: # the last image was 9 (counting from 0), so end.
    your_loop_name.finished = True

The above code assumes that your images are in the same folder as your .psyexp Builder file. If you’ve tidied them away nicely in a subdirectory called, say, images, then you could do something like this instead:

current_image = 'images/' + images[image_number]

None of this tested of course, so come back to us with any issues.