Cocos2d on iPhone: Using Sprites Defined in Separate Class Files.

Posted by srikanth rongali on Stack Overflow See other posts from Stack Overflow or by srikanth rongali
Published on 2010-03-13T09:18:26Z Indexed on 2010/03/13 14:05 UTC
Read the original article Hit count: 210

I'm trying to draw two sprites on the same screen. I have defined the two sprite objects in two separate class files.

  1. If I comment out two lines (see "item 1" comment below) then I get a display as [a backgroundimage[background2.jpg] with a sprite[grossini.png] on the left side.
  2. If I uncomment the two lines I do not get the background image and sprite of (gameScreen.m). I get only the sprite [grossinis_sister1.png] defined in (enemy.m).
  3. But what I need is a [backgroundimage[background2.jpg]], sprite[grossini.png] and sprite [grossinis_sister1.png] in one screen.

This is implementation file of my first class:

#import "gameScreen.h"

#import "enemy.h"
@implementation gameScreen

-(id)init
{
    if((self = [super init]))
    {
        CCSprite *backGroundImage = [CCSprite spriteWithFile:@"background2.jpg"];
        backGroundImage.anchorPoint = ccp(0,0);
        CCParallaxNode *voidNode = [CCParallaxNode node];
        [voidNode addChild:backGroundImage z:-1 parallaxRatio:ccp(0.0f,0.0f) positionOffset:CGPointZero];   

        [self addChild:voidNode];

        CGSize windowSize = [[CCDirector sharedDirector] winSize];

        CCSprite *player = [CCSprite spriteWithFile:@"grossini.png"];
        player.position = ccp(player.contentSize.width/2, windowSize.height/2);

        [self addChild:player z:0];
        //eSprite = [[enemy alloc]init]; //<-- see item 1             
        //[self addChild:eSprite];      
    }

    return self;
}

This is my implementation file of my second class:

#import "enemy.h"

#import "gameScreen.h"
@implementation enemy
-(id)init
{
    if ((self = [super init]))
    {
        CGSize windowSize = [[CCDirector sharedDirector] winSize];

        CCSprite *enemySprite = [CCSprite spriteWithFile:@"grossinis_sister1.png" ];
        enemySprite.position = ccp(windowSize.width/2, windowSize.height/2);

        [self addChild:enemySprite];
    }
    return self;
}
@end

© Stack Overflow or respective owner

Related posts about iphone-sdk-3.0

Related posts about cocos2d-iphone