Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cocos2d-xna: sprite is not drawn if using instance of a class inherited from sprite

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?

like image 458
DarkDeny Avatar asked Mar 31 '14 15:03

DarkDeny


1 Answers

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.

like image 58
Goos Avatar answered Oct 13 '22 15:10

Goos