Updating the image property of an object each timer tick C#

I have a sprite class which has a draw method which draws an explosion. The class has an image property which is used as the image that will be drawn.

I wish to have it so that the image that is draw is changed every 5 seconds or so.

My question is: Using a timer control, how can I code it so the Image property is changed to a different image each tick?

For example, Image 1 '5 seconds' > Image 2 '5 seconds' > Image 3 '5 seconds' > remove image.

Here is the draw method:

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

and the Image property of the class

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

Thanks for any help/guidance.

Answers


i noticed my answer was far to complex for the simple question asked. While I don't believe it is incorrect I do think it complicates the matter. I wanted to leave it there in case someone else finds it useful. But here is a simplified attempt. It uses a timer external to the Sprite class, a button and a panel. I hope this is helpful.

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);
    }


}

}

Need Your Help

How to scale a QGraphicsEllipseItem with the Zoom Level of QGraphicsView

c++ qt qgraphicsview

I have a QGraphicsView on a widget which im adding a number of items to.

Does LINQ To SQL provide faster response times than using ado.net and oledb?

sql-server database linq-to-sql

LINQ simplifies database programming no doubt, but does it have a downside? Inline SQL requires one to communicate with the database in a certain way that opens the database to injections. Inline...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.