OK I've edited everything in this original post now. I've gone back to visual studio 2010 and created a new project and coded the same thing, except only coded the bare minimum to get the same roadblock I had before. This means I am posting the ENTIRE project code here, but it is also quite short and readable. I've included comments to make it easier to follow, too.
Just a recap: the problem is that I'm not able to successfully call Form1.refreshScore()
from any other class than Form1 itself
public partial class Form1 : Form
{
private Enemy enemy_;
private Graphics paper_;
private bool started_;
public Form1()
{
InitializeComponent();
}
private void timer_Tick(object sender, EventArgs e)
{
pictureBox.Refresh();
}
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
if (started_)
{
paper_ = e.Graphics;
enemy_.Draw(paper_);
}
}
public void refreshScore()
{
label.Text = "TEST"; //doesn't show
enemy_.color = Color.Red; //"Null Reference Exception unhandled" ?
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Space)
{
enemy_ = new Enemy(0, pictureBox.Height-20, 20, 100, 2, Color.Blue);
paper_ = pictureBox.CreateGraphics();
started_ = true; //succeeds
}
if (e.KeyCode == Keys.K)
{
enemy_.Kill();
enemy_.color = Color.Green; //succeeds.
}
}
}
class Enemy
{
private float x_;
private float y_;
private float diameter_;
private float health_;
private float walkSpeed_;
private Color color_;
private Form1 form_ = new Form1();
//Overloader
public Enemy()
{
x_ = 1;
y_ = 50;
diameter_ = 20;
walkSpeed_ = 1;
health_ = 100;
color_ = Color.Blue;
}
//Constructor, called in Form1, keydown event ("Space")
public Enemy(float x, float y, float diameter, float health, float walkSpeed, Color color)
{
x_ = x;
y_ = y;
diameter_ = diameter;
health_ = health;
walkSpeed_ = walkSpeed;
color_ = color;
}
//getter/setter for 'color' property
public Color color
{
get { return color_; }
set { color_ = value; }
}
public void Draw(Graphics paper)
{
SolidBrush brush = new SolidBrush(color_);
paper.FillRectangle(brush, x_,y_,x_ + diameter_, y_ + diameter_);
}
//called in Form1 under KeyDown event ("K")
public void Kill()
{
form_.refreshScore();
}
}