我有一个基于 Cocos2D XNA 和 MonoGame 的游戏项目。我想在 CCSprite 类中添加一些自定义逻辑,所以我创建了一个继承自 CCSprite 的类。我添加了一个虚拟自动属性并尝试使用此类,但出于某种原因,作为我的自定义 Sprite 类实例创建的 Sprite 未显示在图层上,而作为 CCSprite 类实例的 Sprite 完全没问题。
代码如下所示:
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);
如果我使用第 1 行并注释掉第 2 行,则不会显示 mySprite 1。否则 - 如果 mySprite 是 CCSprite 的一个实例 - 它运行良好。
这个问题的根源是什么?
最佳答案
您没有使用自己的 Sprite 类调用 CCsprite 的构造函数。
Sprite:CCSprite{
public Sprite():base()
{
//blabla
}
}
base() 正在调用您正在继承的类的 CCSprite 的构造函数 如果你想传递参数,那么做这样的事情:
Sprite:CCSprite{
public Sprite(string imgpath):base(imgpath)
{
//blabla
}
}
现在我已经通过构造函数传递了一个字符串。
关于c# - cocos2d-xna : sprite is not drawn if using instance of a class inherited from sprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22765583/