在 cocos 2dx 中获取 Sprite 的纹理

Get texture of Sprite in cocos 2dx

我开发了 cocos 2dx 游戏,我在 运行 animationSprite 中我想获得当前的 texture name我试过以下代码:

CCSpriteFrame *frameN =   fisherManBoat->displayFrame();
frameName = frameN->_textureFilename;

但它给我的错误是 textureFilename 受到保护,所以我该如何解决它?如果它不起作用那么我还能尝试什么?因为屏幕上有一个按钮,我点击它可以运行动画,所以我想让它平滑。所以,如果动画在再次录音之间,它不会再次开始,而是从当前点开始。

如果要访问 _textureFilename 变量,则需要修改 CCSpriteFrame.h 文件。

首先你需要在CCSpriteFrame.h文件

中找到这段代码
protected:
    Vec2 _offset;
    Size _originalSize;
    Rect _rectInPixels;
    bool   _rotated;
    Rect _rect;
    Vec2 _offsetInPixels;
    Size _originalSizeInPixels;
    Texture2D *_texture;
    std::string  _textureFilename;
    PolygonInfo _polygonInfo;

并从这段代码中剪下一行

std::string  _textureFilename;

现在您必须将其粘贴到定义了 Public 范围的 CCSpriteFrame.h 文件之上。

class CC_DLL SpriteFrame : public Ref, public Clonable
{
public:

    std::string  _textureFilename;

希望对您有所帮助。谢谢