A 'tremida' effect with while?

Asked

Viewed 157 times

0

I’m using the following code inside a Timer:

void dt_Tick(object sender, EventArgs e)
        {
            double x, y;
            int cont = 0;
            x = tt.X;
            y = tt.Y;
            while(cont < 5)
            {
                tt.X += cont;
                tt.Y += cont;
                cont++;
            }
            while(cont > 0)
            {
                tt.X -= cont;
                tt.Y -= cont;
                cont--;
            }
            tt.X = x;
            tt.Y = y;
        }

It would be for a button, change the position of its X and Y to give an effect as if it were shaking, how do I do that? With this code he stands still, taking out the last two lines, he just keeps going up on the screen.

I tried so to make a rather abrupt effect:

 double x, y;
            int cont = 0;
            x = tt.X; //armazana varaivel X para manipulação
            y = tt.Y; //backup da variavel Y

            while (cont < 20)
            {
                if(dt.Interval.TotalSeconds % 2 == 0)
                {
                    tt.X = (20 - cont);
                }
                else
                {
                    tt.X = (20 - cont);
                }

                cont++;
            }

1 answer

1


I’ll give you an idea of how you would do it.

I will do in windows Forms even just so you have an idea.

private void button1_Click(object sender, EventArgs e)
        {
            int x;
            int by;
            int cont = 0;
            x = button1.Location.X; //armazana varaivel X para manipulação
            by = button1.Location.Y; //backup da variavel Y

            //vamos fazer o botão tremer 20 vezes decrementando ele para posição inicial
            //isso vai dar o efeito de tremida mais suave
            while (cont < 20)
            {
                //desloca 20 para a direita, depois vai deslocar 19 e por ai vai até a posição ser a inicial do botão
                button1.Location = new Point(x += (20 - cont), by);
                //aguarda 20 milisegundos para dar tempo de nosso cerebro interpretar e reconhecer o deslocamento
                System.Threading.Thread.Sleep(20);

                //desloca para o centro novamente
                button1.Location = new Point(x -= (20 - cont), by);
                //aguarda 20 milisegundos para dar tempo de nosso cerebro interpretar e reconhecer o deslocamento
                System.Threading.Thread.Sleep(20);

                //desloca 20 para a esquerda, depois vai deslocar 19 e por ai vai até a posição ser a inicial do botão
                button1.Location = new Point(x -= (20 - cont), by);
                //aguarda 20 milisegundos para dar tempo de nosso cerebro interpretar e reconhecer o deslocamento
                System.Threading.Thread.Sleep(20);

                //desloca para o centro novamente
                button1.Location = new Point(x += (20 - cont), by);
                //aguarda 20 milisegundos para dar tempo de nosso cerebro interpretar e reconhecer o deslocamento
                System.Threading.Thread.Sleep(20);

                cont++;
            }
        }

The ideal is not to use System.Threading.Thread.Sleep, but to store your logic inside a timer.

But the important thing is that between one displacement and another you must pause so that our brain can interpret the visual information, otherwise it is so fast that for us it seems that it has not left the place.

And also manipulate the displacements to make the effect of shaking and not simply move N pixels to the X axis and then reverse and good. That doesn’t work.

You can create a windows form ai project and test the code or adapt directly to your project.

  • It didn’t work very well, with Sleep gave the application a slow look, and the effect didn’t work, so I made some changes, the first one he went crazy, going left, the other one he just took a little step and stopped.. I updated the post

  • So as I said I did not do in windows phone the project so Sleep is very expensive for mobile. but the idea is as follows. Move the button 20 pixels to the right (pause +/- 20 milliseconds) turn the button to the center (20 pixels to left) move the button 20 pixels to the left (pause +/- 20 milliseconds) and turn the button to the center (20 pixels to the right). After that decrease 1 and do it all again.

  • If you can not resolve by night I post a reply at night for windows phone.

  • @Leonardovilarinho take a look at this, it doesn’t help you directly but I did because I see that everyone uses the resource the wrong way: http://answall.com/q/86014/101

Browser other questions tagged

You are not signed in. Login or sign up in order to post.