2011-11-13 21 views
-2

私は、爆発を描画する描画メソッドを持つスプライトクラスを持っています。 このクラスには、描画される画像として使用されるimageプロパティがあります。それぞれのタイマーティックでオブジェクトのイメージプロパティを更新するC#

5秒ごとに描画される画像が変更されるようにしたいと思います。

私の質問は次のとおりです:タイマーコントロールを使用して、イメージプロパティが毎回別のイメージに変更されるように、どのようにコード化できますか?

たとえば、画像 '5秒'>画像2 '5秒'>画像3 '5秒'>画像を削除します。

public void DrawExplosion(Graphics graphics) 
    { 
     graphics.DrawImage(explosion_, xPos_, yPos_); 
    } 

とクラスのImageプロパティ/指導任意の助け

public Image Explosion 
    { 
     get {return explosion_;} 
     set {explosion_ = value;} 
    } 

ありがとう:ここ

はdrawメソッドです。

+0

Timerイベントハンドラはどのように見えますか? – sq33G

答えて

1

にイベントをクリックして同点。私はそれが間違っているとは思わないが、私はそれが問題を複雑にすると思う。他の誰かが役に立つと思った場合に備えてそこに残したかったのです。しかし、簡単な試みがここにあります。これは、Spriteクラス、ボタン、およびパネルの外部のタイマーを使用します。これが参考になることを願っています。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace CustomControl 
{ 
public partial class Form1 : Form 
{ 
    Sprites Explosion = new Sprites(); 


    public Form1() 
    { 
     InitializeComponent(); 
     //insert your images in this next line 
     Explosion.Images=new Image[]{Properties.Resources.explode1,Properties.Resources.explode2,Properties.Resources.explode3}; 
     Explosion.ImageIndex = 0; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     timer1.Enabled = true; 
     //Draw the first image right away 
     Explosion.Draw(panel1.CreateGraphics(), 0, 0); 
     //set the index to the next image 
     Explosion.ImageIndex++; 
    } 


    private void timer1_Tick(object sender, EventArgs e) 
    { 
     //if the Imageindex is not greater then the number of images 
     if (Explosion.ImageIndex < Explosion.Images.Count()) 
     { 
      Explosion.Draw(panel1.CreateGraphics(), 0, 0); 
      Explosion.ImageIndex++; 
     } 
     //if it is greater then reset the imageIndex and turn off the timer 
     else 
     { 
      Explosion.ImageIndex = 0; 
      timer1.Enabled = false; 
      //Clear your images, however you need to do that 
      panel1.CreateGraphics().Clear(Color.White); 
     } 


    } 
} 


public class Sprites 
{ 
    public Image DisplayedImage { get { return Images[ImageIndex]; } } 
    public int ImageIndex; 
    public Image[] Images; 

    public void Draw(Graphics graphics, int x, int y) 
    { 
     graphics.DrawImage(DisplayedImage, x, y); 
    } 


} 

} 
0

私の推測が正しいとあなたがゲームを書いているなら、おそらくここにタイマーが必要ないでしょう。あなたのシナリオに合ったゲームループのプログラミングに関する記事がたくさんあります。

基本的に、スクリーン要素(ここではスプライト)の論理更新をレンダリングから分離し、OSフックを使用してパフォーマンスを高く維持します。

0

EDIT:下記の別の回答を追加しました。私はこの質問を複雑にする答えを見つけました。

JRoughan正しいことがありますこれを行うには良い方法があるかもしれません。しかし、あなたの要求に応じて、私はあなたが供給し、一度に1つずつグラフィックスオブジェクトに描画し、次にグラフィックスオブジェクトをクリアするクラスを作成しました。

私はクラスをメソッド内でインスタンス化しました。したがって、スコープから外れて完全に実行される前に収集されないという保証はありません。これを防ぐにはグローバル化する必要があります。新しいイメージでそれを使いたいしかし、それを2度呼び出すと、最初のものが喪失するので、この方法がより良く見つかった、コンテキストに依存し、一度に何種類のアニメーションを使用するかなど... ImageindexChangedイベントを追加しました。画像がクラスの外でいつ変化するかを知るために使用されます。楽しい!私はこれがあなたの探しているものだと願っています。

ボタンとパネルを追加しました。ボタンは、私は私の答えははるかに尋ねた簡単な質問のために複雑に気づいたのButton1方法

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace CustomControl 
{ 
public partial class Form1 : Form 
{ 

    public Form1() 
    { 
     InitializeComponent(); 
     //insert your images in this next line 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 

     Image[] explodingImages = new Image[] { Properties.Resources.explode1, Properties.Resources.explode2, Properties.Resources.explode3 }; 
     //initialize the explosion with the images from above 
     Sprites Explosion = new Sprites(explodingImages,panel1.CreateGraphics()); 
     //the timer will start and it will draw all images from the above collection, then clear itself, then dispose of itself. 
     Explosion.ImageIndex = 0; 
    } 

} 


public class Sprites 
{ 
    private Image _DisplayedImage; 
    public Image DisplayedImage 
    { 
     get { return _DisplayedImage; } 
    } 
    private Image[] Images{get;set;} 
    private int _ImageIndex = 0; 

    public event EventHandler ImageIndexChanged; 

    public int ImageIndex 
    { 
     get { return _ImageIndex; } 
     set 
     { 
      //Changes thh ImageIndexNumber 
      _ImageIndex = value; 
      //Updates the Displayed Image with the current Image as per index 
      _DisplayedImage = Images[ImageIndex]; 
      //fires the ImageIndexChanged Event (if required) 
      if (ImageIndexChanged != null) 
       ImageIndexChanged(this, EventArgs.Empty); 
      //draws the explosion to the graphics object 
      DrawExplosion(G, 0, 0); 
      //starts the timer 
      changeImageTimer.Enabled = true; 
     } 
    } 
    //local variable for the graphics object 
    private Graphics G; 
    //creats a timer for changing the Images 
    Timer changeImageTimer = new Timer(); 
    //Constructor 
    public Sprites(Image[] images,Graphics g) 
    { 
     //set the starting Images 
     Images = images; 
     //set the interval time to 5 seconds- in my opinion this number is rediculously high, but if that is what you want 
     changeImageTimer.Interval = 5000; 
     //Get the Tick event of the timer 
     changeImageTimer.Tick += new EventHandler(changeImageTimer_Tick); 
     //set the Graphics that you want to draw on 
     G = g; 
    } 

    void changeImageTimer_Tick(object sender, EventArgs e) 
    { 
     int temp=ImageIndex+1; 
     //if there are no more pictures to display stop the timer 
     if (temp > Images.Count() - 1) 
     { 
      //stop the timer 
      changeImageTimer.Enabled = false; 
      //Clear the image, replace with your own background color, or draw an image or whatever you need to do to clear the images 
      G.Clear(Color.White); 
     } 
     //if there are more pictures select the next picture 
     else 
      //Changeing the imageindex fires the draw method and the imageindexchanged event 
      ImageIndex++; 
    } 

    public void DrawExplosion(Graphics graphics,int x, int y) 
    { 
     graphics.DrawImage(DisplayedImage, x, y); 
    } 
} 
} 
関連する問題