rahul raj
rahul raj

Reputation: 597

How to add sprite over another sprite in cocos 2d?

I would like to have a sprite over another sprite and would want to give animation for the sprite on top. Can any one suggest me a method? I've tried this

    CCLayer *foreground = [CCLayer node];
    CCSprite* background = [CCSprite spriteWithFile:@"background.png" ];
    background.position = ccp( 150, -120 );

    seeker1 = [CCSprite spriteWithFile: @"wave.png"];
    seeker1.position = ccp( 180, 420 );
    [seeker1 addChild:background z:-1];

    [foreground addChild:seeker1];

    [self addChild:foreground z:1];


    id waves = [CCWaves actionWithWaves:5 amplitude:15 horizontal:YES vertical:YES grid:ccg(15,10) duration:5];
    id a1 = [CCMoveBy actionWithDuration:3 position:ccp(0,-200)];

    id action2 =
    [CCSequence actions: [[a1 copy] autorelease], [a1 reverse], nil];

    id action = [CCSpawn actions:
                 action2,
                 waves,
                 nil];
    [seeker1 runAction:action];

But, when I run the program, it gives animation for the whole layer. Can any one give animation only for seeker1?

Upvotes: 1

Views: 899

Answers (1)

Bijoy Thangaraj
Bijoy Thangaraj

Reputation: 5546

That is because you have added background sprite as a child of seeker1 sprite.

I assume you intend to add the background as a child of the foreground layer. Something like this:

[foreground addChild:background z:-1];

Upvotes: 1

Related Questions