开发者

CCSprite not rendering when made in a method from an external class

I'm sure this is really obvious to someone, but this simple thing is really frustrating me.

I have a class I made called Class_Sprite, which is a sub-class of CCSprite.

I have a method in this class that is supposed to both create the texture for any given instance of Class_Sprite, and then move it to (200,200).

The program runs in the sim but all I get is a black screen.

I was able to render the sprite directly from the layer class.

Here are the files.

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

Class Sprite header:

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


@interface Class_Sprite : CCSprite {

}

-(void)make:(id)sender;

@end

HelloWorldLayer.m (where the method is being called)

@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) ini开发者_JS百科t
{
    // 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

And finally the header file for 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

Thanks for your time


Try changing to this in 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

And use it in HelloWorldLayer as follows:

    Class_Sprite *pc = [[Class_Sprite alloc] init];
    [pc make:self];
    [pc release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜