gpt4 book ai didi

ios - 将 CCSprite 的子类添加到场景中

转载 作者:行者123 更新时间:2023-11-28 20:38:50 24 4
gpt4 key购买 nike

我最近决定对我的 Sprite 进行子类化,但我对如何将它们添加到场景中一无所知。目前,我已经创建了我的 CCSprite 子类,使用 New File>Cocos2d>CCNode>Subclass of... CCSprite。然后,我在 Sprite.h 文件中制作了我的 Sprite :

@interface Mos : CCSprite {
CCSprite *mos;
}

完成后,我在 Sprite.m 中编写如下代码:

@implementation Mos

-(id) init
{
if( (self=[super init])) {

mos = [CCSprite spriteWithFile:@"sprite_mos.png"];

}
return self;
}

我想知道的是如何将这个 Sprite 添加到我的游戏场景中。

最佳答案

下面是如何正确地将 CCSprite 子类化为 documentation说:

@interface Mos : CCSprite {
// remove the line CCSprite *mos;
}

@implementation Mos

// You probably don't need to override this method if you will not add other code inside of it
-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect
{
if( (self=[super initWithTexture:texture rect:rect]))
{

}
return self;
}

+ (id)sprite
{
return [Mos spriteWithFile:@"sprite_mos.png"];
}

@end

然后在你的代码中,就可以正常使用Mos了:

Mos *mos = [Mos sprite];
[scene addChild:mos];

关于ios - 将 CCSprite 的子类添加到场景中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9426682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com