将Int传递给方法,然后将该方法调用到线程中 [英] Passing an Int to a method, Then calling the method into a thread

查看:89
本文介绍了将Int传递给方法,然后将该方法调用到线程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#制作一个射击游戏.我希望能够在每次拍摄后都调用一个新线程,以便在创建新镜头时,该线程继续检查检测,直到进行检测为止,或者它不在屏幕上.

I'm making a a little shooter game in c#. I want to be able to call a new thread every time a shot has been made, so that when a new shot is created, the thread continues to check for detection until detection has been made, or it goes off screen.

班级;

线程ColLaser1;

Thread ColLaser1;

当按下空格键时,它将启动该线程;

When the space bar is hit, it starts this thread;

     公共无效ThreadLaser1()
      {
           int tempShot = currentLaser + 1;
           timeup = false;
           for(currentLaser = tempShot -1; currentLaser< tempShot; currentLaser ++)
           {
                             laser [currentLaser] = new Laser(this,ref backgroundTexture);
                             Components.Add(laser [currentLaser]);
                             laser [currentLaser] .position.X = player.position.X;
                             laser [currentLaser] .position.Y = player.position.Y;
                             lasercount ++;
                             //问题ColLaser1 = new Thread(Collision(currentLaser));
                             ColLaser1.Start();
                             Thread.Sleep(350);
                             timeup = true;
           }
       }

       public void ThreadLaser1()
       {
            int tempShot = currentLaser + 1;
            timeup = false;
            for (currentLaser = tempShot -1; currentLaser < tempShot; currentLaser++)
            {
                laser[currentLaser] = new Laser(this, ref backgroundTexture);
                Components.Add(laser[currentLaser]);
                laser[currentLaser].position.X = player.position.X;
                laser[currentLaser].position.Y = player.position.Y;
                lasercount++;
                //Problem ColLaser1 = new Thread(Collision(currentLaser));
                ColLaser1.Start();
                Thread.Sleep(350);
                timeup = true;
            }
        }

我的碰撞方法;

      私人虚空碰撞(int currentShot)
       {
          如果(laser [currentShot] .position.Y> 0)
           {
                             //检查碰撞
                             bool hasCollision = false;
                             hasCollision = ePlasma [0] .CheckCollision(laser [currentLaser] .GetBounds());
                   如果(hasCollision)
                    {
               b //删除对象
                    }
           }
       }

        private void Collision(int currentShot)
        {
            if (laser[currentShot].position.Y > 0)
            {
                // Check collisions
                bool hasCollision = false;
                hasCollision = ePlasma[0].CheckCollision(laser[currentLaser].GetBounds());
                    if (hasCollision)
                    {
                        //Remove Object
                    }
            }
        }

 

我正在阅读表格,但是我真的很难把这个委托"的标题拍下来.有人可以帮忙吗?

I'm reading forms but I'm really having a hard time rapping my head around this "delegate". Can anyone help?

 

推荐答案

您需要进行一些改动,因此可以将int传递为"state" ;对象,然后在线程内将其取消装箱:

You need to change things around a tiny bit, so you can pass the int as the "state" object, and then unbox it inside the thread:

 


  public void ThreadLaser1()
    {
      int tempShot = currentLaser + 1;
      timeup = false;
      for (currentLaser = tempShot -1; currentLaser < tempShot; currentLaser++)
      {
        laser[currentLaser] = new Laser(this, ref backgroundTexture);
        Components.Add(laser[currentLaser]);
        laser[currentLaser].position.X = player.position.X;
        laser[currentLaser].position.Y = player.position.Y;
        lasercount++;
        ColLaser1 = new Thread(Collision);
        // pass "currentLaser" as state
        ColLaser1.Start(currentLaser);
        Thread.Sleep(350);
        timeup = true;
      }
    }

// New method for collision ;

    private void Collision(object state)
    {
      int currentShot = (int) state; // unbox the integer here now...
      if (laser[currentShot].position.Y > 0)
      {
        // Check collisions
        bool hasCollision = false;
        hasCollision = ePlasma[0].CheckCollision(laser[currentLaser].GetBounds());
          if (hasCollision)
          {
            //Remove Object
          }
      }
    }

 


这篇关于将Int传递给方法,然后将该方法调用到线程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆