Hoi allen,
heb een class:
en een Form:
Maar nu krijg ik bij deze line: game.Draw(e.Graphics); de volgende error:
Object reference not set to an instance of an object.
Alsvast enorm bedankt voor jullie hulp!!
Niels
heb een class:
PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace StarInvader2
{
public class Game
{
private Form1 form1;
private int score;
private int livesLeft = 2;
private int wave = 0;
private int framesSkipped = 0;
private Rectangle boundaries;
private Random random;
//private List<Stars> stars = new List<Stars>();
private Bitmap[] StarAnimation;
//private Direction invaderDirection;
//private List<Invader>invaders;
//private PlayerShip playership;
//private List<Shot> playerShots;
// private List<Shot> invaderShots;
private Stars stars;
public Game(Form1 form1)
{
this.form1 = form1;
form1.game = this;
InitializeImages();
}
public static Bitmap ResizeImage(Image ImageToResize, int Width, int Height)
{
Bitmap bitmap = new Bitmap(Width, Height);
using (Graphics graphics = Graphics.FromImage(bitmap))//Method FromImage
{
graphics.DrawImage(ImageToResize, 0, 0, Width, Height);
}
return bitmap;
}//End method
public void InitializeImages()
{
StarAnimation = new Bitmap[4];
StarAnimation[0] = ResizeImage(Properties.Resources.star1, 10, 10);
StarAnimation[1] = ResizeImage(Properties.Resources.star2, 10, 10);
StarAnimation[2] = ResizeImage(Properties.Resources.star3, 10, 10);
StarAnimation[3] = ResizeImage(Properties.Resources.star4, 10, 10);
PictureBox starPicture = new PictureBox();
starPicture.Size = new Size(10, 10);
}//End method InitializeImages
public void Draw(Graphics g)
{
g.FillRectangle(Brushes.SkyBlue, form1.ClientRectangle);
}
public void Twinkle()
{
}
private int animationCell = 0;
private int Frame = 0;
public void AnimateObject()
{
Frame++;
if (Frame >= 6)
{
Frame = 0;
}
switch (Frame)
{
case 0: animationCell = 0; break;
case 1: animationCell = 1; break;
case 2: animationCell = 2; break;
case 3: animationCell = 3; break;
case 4: animationCell = 2; break;
case 5: animationCell = 1; break;
default: animationCell = 0; break;
}
form1.Invalidate();
}//End method
}
}
PHP:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace StarInvader2
{
public partial class Form1 : Form
{
//Game game = new Game(Game);
public Game game;
public Form1()
{
InitializeComponent();
//public Game game;
}
private void Form1_Load(object sender, EventArgs e)
{
//Color color1 = Color.Black;
// this.BackColor = Color.Black;
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show(e.Location.ToString());
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
game.Draw(e.Graphics);
}
}
}
Maar nu krijg ik bij deze line: game.Draw(e.Graphics); de volgende error:
Object reference not set to an instance of an object.
Alsvast enorm bedankt voor jullie hulp!!
Niels