# How do I loop while "Touch - On hold gesture"?

Get help using Construct 2

### » Tue Oct 20, 2015 2:31 pm

Hello everyone, my name is Brenda and I'm from Argentina.

I'm new using Construct 2 and I find it fascinating!

However I run into a problem when I tried to use the while loop for my mobile game.

What I'm trying to accomplish is to move my sprite to the left or to the right while the user is touching the screen.
So for example, if he touches the left side of the screen, then the sprite would move to the other side.
And it has to stop when the user stops touching the screen.

Here's what I've got so far:

It looks very logical to me but I'm clearly missing something.
When I run my project, everytime I touch the screen, the game freezes and it won't let me do anything else.

Could you help me to find what I'm doing wrong?

Thanks in advance (sorry for my english)
Brenda
B
3
Posts: 3
Reputation: 183

### » Tue Oct 20, 2015 2:45 pm

You have to use
Touch Is in Touch

Simple example moving only on X

So Touch| is in touch
system| touch.x < Layoutwidth/2 Sprite|Set X to Self.x-2

And do same for opposite side but inversed
So Touch| is in touch
system| touch.x > Layoutwidth/2 Sprite|Set X to Self.x+2
You think you can do these things, but you can't, Nemo!
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,753

### » Tue Oct 20, 2015 2:56 pm

Thanks DUTOIT!

If I do it that way, it works perfect as you said!

But, what I want to do is to gradually accelerate the sprite while the user is touching the screen.
That's why I was using the While loop, to increment the acceleration on every cycle
B
3
Posts: 3
Reputation: 183

### » Tue Oct 20, 2015 3:07 pm

touch.x<layoutwidth/2 is a loop so
Put your custommovement accelerate 300 in, and put self.x-2 and self.x+2 respectively.

It loops as long as that condition isn't met.
See
You think you can do these things, but you can't, Nemo!
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,753

### » Tue Oct 20, 2015 3:14 pm

You could do that with a variable what increase or decrease.
And I would use ViewportRight not LayoutWidth.
Code: Select all
`Global number acceleration =0Touch| is in touch  System| add 0.1 from acceleration     system| touch.x < ViewportRight(0)/2  Sprite|Set X to Self.x-acceleration    system| touch.x > ViewportRight(0)/2  Sprite|Set X to Self.x+acceleration                                      Touch| on any touch end   System|set acceleration to 0`
B
26
S
3
G
1
Posts: 482
Reputation: 2,294

### » Tue Oct 20, 2015 3:21 pm

Thanks DUTOIT, it worked really good!

Thank you also, Asmodean! I'll try what you said too.

Brenda
B
3
Posts: 3
Reputation: 183