#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

# define x_position depending on 2 variable (& break loop)

Hi,
I have another problem. I hope I'm not bothering you!

So, I have an rectangle on a sketchpad. The sketchpad is a loop and the x_position of the rectangle depends on the iteration of the loop. So for the first iteration through the loop it is -350, for the second iteration it is -300 and so on.

Question 1: I want to make it possible for the participant to end the loop when s/he feels that the rectangle is at the right position (and save that position in a variable). Each individual iteration through the loop is ended with a keypress (and then the next iteration starts), i.e. the duration of the sketchpad is keypress. But how can I end the loop and save the position?

Question 2: In the next loop the participants can define the width of the rectangle. With each iteration of the loop the width gets bigger. Here I also have to have the possiblity for the participants to stop the loop if they feel it is the correct width. But my question for this is primarily another one: The x_position depends on the position of the previous loop. I.E. if the participant feels that 300 is a good position it should be x=300. That is no problem, but now I increase the with and it has to add with on both sides of the rectangle equally. So in order to achieve that (because width automatically is added on the right) I add 2pixel of with and move the rectangle one px to the left at the same time. So that it looks like the width was added on the left and on the right. But: I add 2 px in the first iteration of the loop, another 2 in the second iteration (so 4 in total) and so on, so the x-position depends on 2 variables: The position that was defined in the first loop and the iteration of the second loop. So if the x-position was defined as 300 in the first loop and it is the second iteration of the second loop it should be 300-2. So it depends on two variables: The variable of the first loop x_pos and the variable of the second loop x_pos_change. How can I combine those 2 variables?
I tried with an inline-script in which I wrote:
`x_pos_new = exp.get(var.x_pos + var.x_pos_change)`
But this doesn't work as it tells my x_pos_new does not exist when I define the x-position as `x=[x_pos_new]`

• Hi Pia,

Question 1:

When waiting for a keypress allow two different keys. One (say `space`) will flip the `sketchpad` and show the next reactangles. The other (let's say `return` will also make the `sketchpad` disappear, but on top of that, it will break the `loop`. How to implement it depends on how you implemented the other parts of your experiment.

Question 2:

I think what you meant to do, is `exp.set()` not `exp.get()`. So try this: `exp.set('x_pos_new',var.x_pos + var.x_pos_change)`.
Maybe it would be easier if you save the middle x coordinate instead of the top left point. Then you don't have to do these funny manual things with adding things to the left and the right. Once you have the middle_x, you can draw a rectangle easily by `canvas.rect(x-0.5*width,y,width,height)`

Eduard