I started to write a game, but I am still learning object oriented programming.
I am using VS 2015 (Win7).
I created a class called Ship
, a method which let me create a ship with only two parameters so far: position X
and Y
on map.
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
I want to make Ship
move each second using a method move()
and a Timer.
My code:
(Also on 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
}
}
What is causing an error is this code?:
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
}
The Timer is Enabled by default, its Interval is set to 1000
and modifiers are Private
.
What am I doing wrong?
It does not work inside the Timer event but if I use it inside 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);
}
It works fine (but only once - so I want to use a Timer).
I hope the subject of post is understandable and my problem I described well enough.
I tried to modify public/private in some places but it did not help.