Whenever I call the method Draw in Sprite, it won’t draw it because X, Y, Width and Height are 0. 🙁
code:
class Sprite
{
protected int Y;// { get; set; }
protected int X;// { get; set; } { get; set; }
protected int Width;// { get; set; } { get; set; }
public int Height;// { get; set; } { get; set; }
protected Image image;// { get; set; } { get; set; }
public Sprite()
{
}
public void Draw(Graphics drawArea)
{
Image sImage = Image.FromFile("alien.jpg");
drawArea.Clear(Color.White);
drawArea.DrawImage(sImage, X, Y, Width, Height);
}
}
class User:Sprite
{
public User()
{
}
public User(int width, int height, int userWidth, int userHeight)
{
Sprite sprite = new Sprite();
Image UserImage = Image.FromFile("alien.jpg");
X = width;
Y = height;
Width = userWidth;
Height = userHeight;
image = UserImage;
}
}
ps: sprite.Draw is declared in another method in another class, but that all should work just fine.
Thanks for helping and probably saving me hours of time 🙂
Nick
EDIT
here is the subclass which gives the parameter and other stuff.
Alien mAlien;
User mUser;
protected int mLevel;
public gameLogic()
{
}
public gameLogic(int width, int height, int level)
{
mUser = new User(width / 2, height - 30, 30, 30);
mAlien = new Alien(width / 2, 5, 30, 30, "alien.jpg", 10 * level);
mLevel = level;
}
public void drawAll(Graphics drawArea)
{
Sprite sprite = new Sprite();
sprite.Draw(drawArea);
}
Im sorry for all these errors that’ll probably occur, Im a new student 🙂
Try this:
In order to access the fields of the sprite, you have to specify which
Spriteobject you are modifying. This is done by writing the name of the variable followed by.EDIT: Just realized that there is another problem – your
Userclass is inheriting from theSpriteclass. Apparently your users are sprites, according to the comments, so you’ll want to never instantiate aSprite, and just use theUserclass instead:Then instead of calling draw on a sprite in the other file, use the following:
The key here is to make sure you are using
new User...and notnew Sprite– even if you are assigning to aSpritevariable e.g.Sprite s = new User(...);.Then just make sure you call draw on the same object – the
user.drawline. If you want to check that you’re doing it right, try makingSpriteabstract–abstract class Sprite– that way there will be compile errors if you try to instantiateSpriteinstead ofUser.EDIT 2: Ok, all you really need to do is change your calling code: