Jump to content
Welcome, welcome! Come in and register, and have some developer coffee. 👨‍💻 ×
  • 0

How to clone and animate multiple 2D sprites?


Go to solution Solved by flashang,

Question

This is the example animation of 2D game, using left arrow, right arrow, up arrow and down arrow to move, spacebar to shoot, and escape to quit the program.

Currently there is one bullet one at a time.

 B.png.0e9da671f9c9494bddf4d7db5008e7fe.png

 

How do I clone multiple instance of it during runtime (and dispose it afterward), and animate multiple of the same 2D sprite at any given time?

This is portion of my code logics in C#:

       private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                if (pictureBox1.Left > 50)
                    pictureBox1.Left -= 10;
            }
            else if (e.KeyCode == Keys.Right)
            {
                if (pictureBox1.Left < (this.Width - 100))
                    pictureBox1.Left += 10;
            }
            else if (e.KeyCode == Keys.Up)
            {
                if (pictureBox1.Top > 50)
                    pictureBox1.Top -= 10;
            }
            else if (e.KeyCode == Keys.Down)
            {
                if (pictureBox1.Top < (this.Height - 100))
                    pictureBox1.Top += 10;
            }
            else if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
            else if (e.KeyCode == Keys.Space)
            {
                if (!timer1.Enabled)
                {
                    pictureBox2.Visible = true;
                    pictureBox2.Left = pictureBox1.Left + 15;
                    pictureBox2.Top = pictureBox1.Top - pictureBox2.Height;
                    timer1.Enabled = true;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox2.Top -= 20;
            if (pictureBox2.Top < (pictureBox2.Height * -1))
            {
                pictureBox2.Visible = false;
                timer1.Enabled = false;
            }
        }
 

As you see below, only one bullet sprite is animated at a time.

G.png.c3436fada419fe4ae21ed44d75b3210a.png

 

Tips will be appreciated much along with example code logic.

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
On 4/15/2021 at 11:19 AM, flashang said:

not sure what is the object type of pictureBox2, a simple working solution is use array of pictureBox.

🙂

 

I think you're right, the easiest and simplest solution will be using array of pictureBox.

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...