CCSprite не рендерится при создании метода из внешнего класса - PullRequest
0 голосов
/ 02 октября 2011

Я уверен, что для кого-то это действительно очевидно, но эта простая вещь действительно расстраивает меня.

У меня есть класс, который я создал, который называется Class_Sprite, который является подклассом CCSprite.

У меня есть метод в этом классе, который должен создать текстуру для любого данного экземпляра Class_Sprite, а затем переместить ее в (200 200).

Программа работает в симе, но все, что я получаючерный экран.

Мне удалось отобразить спрайт непосредственно из класса слоя.

Вот файлы.

Class_Sprite:

#import "Class_Sprite.h"


@implementation Class_Sprite

-(id)init
{
    if ((self = [super init]))
    {
    }
    return self;
}
-(void)make:(id)sender
{
    sender = [Class_Sprite spriteWithFile:@"Icon.png"];
    [sender setPosition: ccp(200, 200)];
}
@end

Заголовок класса Sprite:

#import <Foundation/Foundation.h>
#import "cocos2d.h"


@interface Class_Sprite : CCSprite {

}

-(void)make:(id)sender;

@end

HelloWorldLayer.m (где вызывается метод)

@implementation HelloWorldLayer

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value

    if( (self = [super init])) {
        Class_Sprite *pc = [[Class_Sprite alloc] init];
        [pc make:self]; //here is where I call the "make" method
        [self addChild:pc];
        [pc release];




    }
    return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)

    // don't forget to call "super dealloc"
    [super dealloc];

}
@end

И, наконец, файл заголовка для HelloWorldLayer

#import "cocos2d.h"
#import "Class_Sprite.h"

// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
{
}

// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;

@end

Спасибо за ваше время

1 Ответ

0 голосов
/ 02 октября 2011

Попробуйте изменить это в Class_Sprite.m:

@implementation Class_Sprite

-(id)init
{
    if ((self = [super initWithFile:@"Icon.png"]))
    {
    }
    return self;
}
-(void)make:(CCNode *)sender
{
    [self setPosition: ccp(200, 200)];
    [sender addChild:self];
}
@end

И используйте его в HelloWorldLayer следующим образом:

    Class_Sprite *pc = [[Class_Sprite alloc] init];
    [pc make:self];
    [pc release];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...