2011-02-03 34 views
0

timerpictureBoxという特定の場所から画像を移動するコードを記述しました。タイマーで画像を移動

しかし、画像が希望のパス上を移動しないため、コードが正しく動作しません。

私が欲しいのです:

- create images from points (10,Y0). 
- move horizontally right to reach point (500,Y0). 
- move vertically down or up to reach point (500,750) and stop here for example 7 second. 
- then, move again horizontally right and go to point (700,750) and stop here for example 8 second. 
- then, move again horizontally right and go to point (750,750). 
- at this point, if (complete == true) the `pictureBox` must hide and back to (10,Y0) 
- if (complete == false) then 
     if (up == true) 
      - move vertically up to reach point (750,900) and and 
      - move horizontally right and go to (900,900) and stop here for example 10 second. 
      - then, then, move again horizontally left and go to point (500,900). 
      - then, move vertically down to reach point (500,750). 
     else if (down == true) 
      - move vertically down to reach point (750,600) and and 
      - move horizontally right and go to (900,600) and stop here for example 10 second. 
      - then, then, move again horizontally left and go to point (500,600). 
      - then, move vertically up to reach point (500,750). 

私はいくつかのコードを書いてきたが、私が言ったように、それは...正しく動かない

もう一つのことだ:私は実装することができますどのように待機?? (exapmle(900,600)に達するときには、10秒待つか、画像が動くように外側の何かを待たなければなりません)。私は実際に私が提供するつもりはないやっているものを完全にはよく分からないので

private int k = 0; 

    void timer_Tick(object sender, EventArgs e) 
    { 
     k++; 

     int x = p.Location.X; 
     int y = p.Location.Y; 

     if (k <= 250) 
      p.Location = new Point(x + 2, y); 

     else if (k <= 400) 
     { 
      // if we are moving up of point (500,750) 
      if (y < 750) 
       p.Location = new Point(x, y + 1); 

      // if we are moving down of point (500,750) 
      if (y > 750) 
       p.Location = new Point(x, y - 1); 
     } 

     // *** wait HERE. 

     else if (k <= 500) 
      p.Location = new Point(x + 2, y); 

     // *** wait HERE. 

     else if (k <= 550) 
      p.Location = new Point(x + 2, y); 

     else if (k <= 700) 
     { 
      if (complete == true) 
       p.Location = new Point(x, y + 1); 
      else 
       p.Location = new Point(x, y - 1); 
     } 

     else if (k <= 800) 
      p.Location = new Point(x + 2, y); 

     // ***** wait HERE 

     else if (k <= 1200) 
      p.Location = new Point(x - 2, y); 

     else if (k <= 1400) 
     { 
      if (complete == true) 
       p.Location = new Point(x, y - 1); 
      else 
       p.Location = new Point(x, y + 1); 
     } 
     else 
      timer1.Stop(); 
    } 

} 

    private void button1_Click(object sender, EventArgs e) 
    { 
     timer1.Start(); 
     timer1.Interval = 15; 
     timer1.Tick += new EventHandler(timer_Tick); 
    } 
+0

あなたのテクノは何ですか? –

+0

答えを分かりやすくするために、代わりに何をしますか? – Gidon

+0

メインタイマーを一時停止せずにX秒間実行する「待機タイマー」を開始する理由を待ちたいとき。待ち時間タイマーがきたら、最初のタイマーを続行します。 – Gidon

答えて

2

は、正直に言うと:

ここ

は、これまでのところ、私のコードです...助けてくださいピクチャボックスソリューション。私の経験上、アニメーションやタイミングなどは、タイマを使用して実装することは決して簡単ではありません。なぜなら、使用しているタイマの種類が、正確に起動することを保証していないからです。通過するタイマーメッセージ。それは不可能でも何でもないわけではありません。アニメーションでの私の最初の仕事はタイマーでも行われました。

あなたが書いているコードは、その後、あなたは私がするよりも寛大な問題や誰かを解決しようとするの一環として、これを習得する必要がありますコンピュータアニメーション(すなわち宿題?)で、いくつかの運動を満たすことであるならばあなたに答えを与えることは間違いありません。

私の場合、WPFでは代わりにこれを行い、そのanimation and storyboard機能を使用してください。

このようにして、タイムラインとパスを指定することができます(線形パスはWPFで簡単です)。次に、イベントを決定に使用できます。実際のアニメーションを管理することは、もはやあなたの問題ではなくなりました。あなたは、物事がどこにあるか、どれくらいの時間がかかるべきかを指定するだけです。

WPFはゲームで使用されているような「適切な」レンダリングループを使用するため、最終結果は画面上ではるかに優れている可能性があります。

この回答は役に立ちません。

関連する問題