在计时器内部使用方法

时间:2019-01-30 22:56:44

标签: c# winforms timer

我开始写游戏,但我仍在学习面向对象的编程。
我正在使用VS 2015(Win7)。

我创建了一个名为Ship的类,该方法让我可以创建一个仅带有两个参数的飞船:位置XY在地图上。

Ship[] submarine = new Ship[100]; 
// create first ship with only two
submarine[0].create_ship(140, 200);
parameters: position X and Y on future map

我想使用方法Ship和计时器使move()每秒移动一次。

我的代码:
(也在PasteBin上)

public partial class Form1 : Form
{
    class Ship
    {
        private double ship_posX; // position X on map
        private double ship_posY; // position Y on map
        private int ship_heading; // current heading
        private int ship_speed_max; // max speed of a ship
        private int ship_current_speed; // current speed of a ship
        private string ship_class; // surface or submarine
        private string ship_foe; // friend or enemy?
        private string is_controllable; // is controllable by player?
        private int depth; // current depth of a submarine (if a submarine)

        public void move(int heading, int speed)
        {
            if (heading == 0)  ship_posY -= speed; // ship is heading NORTH
            if (heading == 45) // ship is heading NORTH-EAST
            {
                ship_posX += speed;
                ship_posY -= speed;
            }
            if (heading == 90) ship_posX += speed; // ship is heading EAST
            if (heading == 135) // ship is heading SOUTH-EAST
            {
                ship_posX += speed;
                ship_posY += speed;
            }
            if (heading == 180) ship_posY += speed; // ship is heading SOUTH
            if (heading == 225) // ship is heading SOUTH-WEST
            {
                ship_posX -= speed;
                ship_posY += speed;
            }
            if (heading == 270) ship_posX -= speed; // ship is heading WEST
            if (heading == 315) // ship is heading NORTH-WEST
            {
                ship_posX -= speed;
                ship_posY -= speed;
            }
        }

        public void create_ship(double posx, double posy)
        {
            ship_posX = posx;
            ship_posY = posy;
            // only a few parameters for now. Later will be name, speed_max, class, friend or foe and more
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // forms load == game starts
        Ship[] submarine = new Ship[100]; 
        submarine[0].create_ship(140, 200); // create first ship with only two parameters: position X and Y on future map
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        submarine[0].move(90, 15); 
        // ERROR: compiler shows "The name 'submarine' does not exist in the current context
    }
}

什么是导致此错误的代码?:

private void timer1_Tick(object sender, EventArgs e)
{
    submarine[0].move(90, 15); 
    //ERROR: compiler shows "The name 'submarine' does not exist in the current context
}

默认情况下启用计时器,其时间间隔设置为1000,修饰符为Private

我在做什么错?

它在Timer事件中不起作用,但是如果我在Form.Load中使用它:

private void Form1_Load(object sender, EventArgs e)
{
     // forms load == game starts
     Ship[] submarine = new Ship[100]; 
      // create first ship with only two parameters: position X and Y on future map
     submarine[0].create_ship(140, 200);
     submarine[0].move(90, 15);
}

它工作正常(但只能运行一次-所以我想使用计时器)。

我希望帖子的主题是可以理解的,我的问题已经足够好描述了。

我试图在某些地方修改公共/私有,但没有帮助。

1 个答案:

答案 0 :(得分:0)

  

变量scope决定了变量对其余变量的可见性   程序。在整个C#基础知识教程的示例中,   变量已在方法中定义。以这种方式创建后,   变量的范围是声明后的整个方法

在您的情况下,您可以在submarine方法中定义并声明Form1_Load,根据上述定义,submarine仅在Form1_Load方法的范围内可用。

如果要以submarine类的不同方法使用Form1,可以将其作为“类字段”进行介绍。

这意味着:

public partial class Form1 : Form
{
    private static Ship[] submarine;

    .
    .
    .

    private void Form1_Load(object sender, EventArgs e)
    {
        // forms load == game starts

        submarine = new Ship[100]; 
        submarine[0].create_ship(140, 200); // create first ship with only two 
        parameters: position X and Y on future map
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        submarine[0].move(90, 15); // ERROR: compiler shows "The name 'submarine' 
        does not exist in the current context
    }
  

现在,您再次遇到错误

,这是因为您没有在数组中声明ship的任何实例: 在ship类中创建一个名称为Initial

的静态方法
public static void Initial()
{
    for(int i = 0; i < 100; i++)
    {
       submarine[i] = new ship();
    }
}

并在Form1_load()中使用它:

private void Form1_Load(object sender, EventArgs e)
        {
            // forms load == game starts

            submarine = new Ship[100]; 
            ship.Initial();
            submarine[0].create_ship(140, 200); // create first ship with only two 
            parameters: position X and Y on future map
        }

完成这些更改后,我认为它可以正常工作。