cocos2dのベストプラクティスでは、initWithTextureを使用してスプライトサブクラスを初期化するという提案がありますが、いくつかの本やチュートリアルでは、initWithSpriteFrameNameも使用できることがわかりました。何故ですか?
1 に答える
4
All of the initialization methods in CCSprite
use initWithTexture:rect
to create the sprite. For example,
-(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;
}
Other functions (initWithFile
, initWithSpriteFrame
, initWithSpriteFrameName
, et cetera) also call initWithTexture:rect
either directly or indirectly. If your CCSprite
subclass has any special initialization that needs to take place (as it ostensibly will, since you're subclassing another class), doing it in initWithTexture:rect
guarantees that it will be run.
于 2012-06-16T13:43:27.343 に答える