I have a game project built upon Cocos2D XNA and MonoGame. I wanted to add a little bit of custom logic into CCSprite class, so I created a class which inherits from CCSprite. I added a dummy auto property and tried to use this class, but for some reason sprites being created as instances of my custom sprite class are not shown on the layer, while sprites which are instances of CCSprite class - are completely OK.
Code looks like this:
public class Sprite: CCSprite {
public string SomeProp {get; set;}
}
...
line1: var mySprite1 = new Sprite("texture.png");
line2: var mySprite1 = new CCSprite("texture.png");
AddChild(mySprite1);
If I use line1 and comment out line 2, then mySprite 1 is not shown. Otherwise - if mySprite is an instance of CCSprite - it works well.
What could be the source of this problem?
You are not calling the constructor of the CCsprite with your own Sprite class.
Sprite:CCSprite{
public Sprite():base()
{
//blabla
}
}
the base() is calling the constructor of CCSprite the class you are inheriting if you want to pass through parameters then do something like this:
Sprite:CCSprite{
public Sprite(string imgpath):base(imgpath)
{
//blabla
}
}
Now I've passed a string through the contructors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With