【问题标题】:Why the Cocos2D programming guide suggests to use initWithTexture?为什么 Cocos2D 编程指南建议使用 initWithTexture?
【发布时间】:2012-06-16 13:37:07
【问题描述】:

我发现在cocos2d Best Practices 中有一个建议使用initWithTexture 来初始化精灵子类,而不是在几本书和教程中我发现你也可以使用initWithSpriteFrameName。这是为什么呢?

【问题讨论】:

    标签: iphone ios cocos2d-iphone sprite


    【解决方案1】:

    CCSprite 中的所有初始化方法都使用initWithTexture:rect 来创建精灵。例如,

    -(id) initWithFile:(NSString*)filename
    {
        NSAssert(filename!=nil, @"Invalid filename for sprite");
    
        CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage: filename];
        if( texture ) {
            CGRect rect = CGRectZero;
            rect.size = texture.contentSize;
            return [self initWithTexture:texture rect:rect];
        }
    
        [self release];
        return nil;
    }
    

    其他函数(initWithFileinitWithSpriteFrameinitWithSpriteFrameName 等)也直接或间接调用initWithTexture:rect。如果您的 CCSprite 子类有任何需要进行的特殊初始化(表面上会这样,因为您正在继承另一个类),那么在 initWithTexture:rect 中执行它可以保证它会运行。

    【讨论】:

    • 我猜也可以使用 initWithSpriteFrame 或其他功能,对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多