Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using c# for winapp. Not for Unity or whatever.

C#
my class
{
        Lasers laser1 = new Lasers();
        Lasers laser2 = new Lasers(); 


        public Screen()
        {
            InitializeComponent();
            DoubleBuffered = true;
            this.Paint += new PaintEventHandler(Screen_Paint);

            Timer timerforLaser = new Timer();
            timerforLaser.Tick += new EventHandler(Timer_forLaser_Tick);
            timerforLaser.Interval = 100;
            timerforLaser.Start();

            ....
  
            laser1.Initialize();
            laser2.Initialize();
        }
 
        void Screen_Paint(object sender, PaintEventArgs e)
        {
            ........

            e.Graphics.DrawImage(laser1.Img0, laser1.Rectangle);//LaserPaint
            e.Graphics.DrawImage(laser2.Img0, laser2.Rectangle);//LaserPaint
        }
        void Timer_forLaser_Tick(object sender, EventArgs e)
        {
            laser1.ShootUp();
            laser2.ShootUp();
            this.Refresh(); //very Important -without him here nothing works
        }

        private void Screen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                laser1.New(Player.X, Player.Y);
                laser2.New(Player.X, Player.Y);
            }
            this.Refresh(); //very Important -without him here nothing works
        }
}


From the code i post, when I press Space key, it will create 2 lasers images on the screen and update them in the timer event.
The problem is that is creating them at once, and overlapped.
I want them created 1 after another. Like a burst. With a short time between each other. And no overlaps.
I imagine i must put a timer between the starting point of each laser somehow, as long as i keep pressed the Space key. Or a delay of some sort. And if i have to create 100 lasers, i will probably create an array that will contain all the lasers? I have no idea how to make this thing work.

Thank you kindly.

What I have tried:

everything
everything
everything
everything
everything
everything
Posted
Updated 11-Aug-17 19:23pm

1 solution

Your code is doing exactly what you asked it to do ... draw both at the same time. If you want to draw each one individually, then you need a variable to track which laser is drawn for that timer cycle and then alternate... Something like...
C#
private bool isAlternate;

C#
void Screen_Paint(object sender, PaintEventArgs e)
{
    // ........
 
    if(isAlternate)
        e.Graphics.DrawImage(laser1.Img0, laser1.Rectangle);//LaserPaint
    else
        e.Graphics.DrawImage(laser2.Img0, laser2.Rectangle);//LaserPaint
}

void Timer_forLaser_Tick(object sender, EventArgs e)
{
    laser1.ShootUp();
    laser2.ShootUp();
    this.Refresh(); //very Important -without him here nothing works
    isAlternate = !isAlternate;
}
 
Share this answer
 
Comments
_Q12_ 12-Aug-17 1:28am    
heh, the laser is changing color if i put 2 diferent colors to see the effect. As the laser flies up, the color is changing per tick. Nice effect though :) the stroboscopic laser :)
I changed the laser2.Img0 into Img1 which is another color image.
_Q12_ 12-Aug-17 1:42am    
i corrected it.
if (e.KeyCode == Keys.Space)
{
if (isAlternate)
laser1.New(Player.X, Player.Y);
else
laser2.New(Player.X, Player.Y);
isAlternate = !isAlternate;
}
now it shoots different lasers color every time i press space.
BUT....
if i keep pressed the Space key, it is not shooting the controlled burst i want. How to do that?
_Q12_ 12-Aug-17 2:02am    
your answer is good and it helped.
But i want another variant if anyone can share it's thoughts.
_Q12_ 12-Aug-17 11:24am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900