2011-11-13 27 views
-2

我有一個sprite類,它有繪製方法,繪製爆炸。 該類具有用作將要繪製的圖像的圖像屬性。更新一個對象的圖像屬性每個計時器滴答C#

我希望擁有它,以便每5秒左右更改繪製的圖像。

我的問題是:使用計時器控件,我該如何編碼它,以便Image屬性更改爲每個刻度不同的圖像?例如,圖像1'5秒'>圖像2'5秒'>圖像3'5秒'>刪除圖像。

這裏是繪製方法:

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

和一流的圖像性能

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

感謝任何幫助/指導。

+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

編輯:我在下面添加了另一個答案。我發現這個問題的答案很複雜。

JRoughan可能是正確的可能有更好的方法來做到這一點。但根據你的要求,我寫了一個類,它會提供你提供的圖像,並一次將它們繪製到圖形對象上,然後清除圖形對象。

我在一個Method中實例化了Class,所以不能保證它不會超出範圍並在完全運行之前收集,爲了防止這種情況發生,您可能希望使其成爲全局的,然後在每次重新實例化時想要使用它,與新的圖像。但是如果你調用它兩次,第一次會失敗,所以我發現這個方法更好,它取決於上下文,你打算一次使用多少不同的動畫等......我還添加了一個ImageindexChanged事件,可以用於瞭解圖像在課堂之外何時發生變化。請享用!我希望這是你想要的。

我剛剛添加了一個按鈕和麪板。追平了按鈕單擊事件到Button1_Click方法

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); 
    } 
} 
} 
相關問題