Search code examples
iosanimationcocos2d-iphoneccsprite

Make CCSprite follow another CCSprite when touched?


What I want to accomplish is to make my CCSprite follow another CCSprite when it is touching it. Now what I mean by follow is, lets say there is an animation of another CCSprite moving up the screen. So if this sprite hits my main sprite, my main sprite should move up the screen with it. This other sprite will be a platform, so technically in the end I would want the sprite to be on top of the other sprite but it would be moving along the top of the platform CCSprite as if the platform was carrying the main sprite.

Now I know how to do the collision detection part and make the other sprite animate but how would I just make my platform 'carry' my main CCSprite also kind of like how an elevator works?


Solution

  • I think you can place flag to check the condition "when to follow" in update: method you can reposition the second sprite according to position of first Sprite.

    -(void)update:(CCTime)delta
    {   
       if(condition == YES)
       {
          secondSprite.position = ccp ( firstSprite.position.x + xOffSet , firstSprite.position.y + yOffSet);
       }
    }
    

    Try this according to your code.. Basic logic is here..